Solved

PHP - return count variable from function

Posted on 2011-03-16
1
271 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
1 Comment
 
LVL 6

Accepted Solution

by:
ramelong earned 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

679 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