?
Solved

PHP - return count variable from function

Posted on 2011-03-16
1
Medium Priority
?
275 Views
Last Modified: 2012-05-11
Hi,
I am trying to keep count of the number of times 2 fields passed to my function match and then display the total,
I dont know what I am doing wrong but values are coming back as zero?

You comments and feedback would be appreciated.

<?php
 $match_yes = 0;
 $match_no = 0;

function check_match($profile_details, $user_details){
                  global $match_yes, $match_no;
                  
                  if ($profile_details == $user_details){
                  $match_yes++;
                  return '<img src="images/tick_sml.png" width="15" height="15" alt="Perfect Match" />';
                  return $match_yes;
                  
                  } else {
                  
                  $match_no++;            
                    return '<img src="images/cross_sml.png" width="15" height="15" alt="No Match" />';
                  return $match_no;      
            }

}
?>


<?php echo "You matched $match_yes times and did not match $match_no times" ;?>

<div class="user_details_heading">Gender:&nbsp;</div>
<div class="user_details_match"><?php echo check_match($row_view_users['match_gender'],$row_view_logged_in['gender']); ?></div>

<div class="user_details_heading">Hair Color:&nbsp;</div>
<div class="user_details_match"><?php echo check_match($row_view_users['match_hairColor'],$row_view_logged_in['hairColor']); ?></div>

<div class="user_details_heading">Hair Length:&nbsp;</div>
<div class="user_details_match"><?php echo check_match($row_view_users['match_hairLength'],$row_view_logged_in['hairLength']); ?></div>


0
Comment
Question by:sabecs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 6

Accepted Solution

by:
ramelong earned 2000 total points
ID: 35154034
Try performing the global declaration at the begining (autside of the function):
<?php
global $match_yes, $match_no;
$match_yes = 0;
$match_no = 0;

function check_match($profile_details, $user_details){
      ...
}
?>

Open in new window


Additionally: the "second" returns do nothing, because the first one already returned from the function, those lines must be deleted:
return $match_yes;
return $match_no;

Open in new window


Furthermore: you are trying to show the values of $match_yes and $match_no, before the calculations (this will be always 0).
One of the follows:
1) Put this line at the end.
<?php echo "You matched $match_yes times and did not match $match_no times" ;?>

Open in new window

2) You use the function before and store the values into variables:
<?php 
global $match_yes, $match_no;
$match_yes = 0;
$match_no = 0;

function check_match($profile_details, $user_details) {
      if ($profile_details == $user_details) {
            $match_yes++;
            return '<img src="images/tick_sml.png" width="15" height="15" alt="Perfect Match" />';
      } else {
            $match_no++;            
            return '<img src="images/cross_sml.png" width="15" height="15" alt="No Match" />';
      }
}

$gender=check_match($row_view_users['match_gender'],$row_view_logged_in['gender']);
$hairColor=check_match($row_view_users['match_hairColor'],$row_view_logged_in['hairColor']);
$hairLength=check_match($row_view_users['match_hairLength'],$row_view_logged_in['hairLength']);

php echo "You matched $match_yes times and did not match $match_no times" ;
?>

<div class="user_details_heading">Gender:&nbsp;</div>
<div class="user_details_match"><?php echo $gender; ?></div>

<div class="user_details_heading">Hair Color:&nbsp;</div>
<div class="user_details_match"><?php echo $hairColor; ?></div>

<div class="user_details_heading">Hair Length:&nbsp;</div>
<div class="user_details_match"><?php echo $hairLength; ?></div>

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

766 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question