Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP - return count variable from function

Posted on 2011-03-16
1
Medium Priority
?
276 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

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.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
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.
Suggested Courses

636 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