Solved

PHP - return count variable from function

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

688 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