Solved

PHP - return count variable from function

Posted on 2011-03-16
1
267 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now