PHP variables

I have the block of php code below to help me filter results. It doesn’t work even though $match is true. Any ideas what the problem is?

<?php

// My matche Scenario //

if($theyWant == "Men who like women" && $meWant == "Women who like men") {
        
     $match == true;
        
} 

if($match == true) {

    print "You have a match";

}

?>

What exactly is this line of code doing?

$match == true;

Methinks it should be

$match = true;
1 Like

Is $match initialized somewhere before that block?
if not and the the condition for the first IF statement is not met, $match will be undefined when it reaches the second one causing a warning.
Just wondering is the first IF_statement there ONLY to set $match?

if that’s the case I would recommend doing this:

$match = ($theyWant == "Men who like women" && $meWant == "Women who like men");
if($match == true) {
    print "You have a match";
}

thats the most modular solution, tho if you are CERTAIN that you are only using $match that one time in your script, you can simplify the code and get rid of the variable entirely:

if ($theyWant == "Men who like women" && $meWant == "Women who like men") {
    print "You have a match";
}

Thank you so much for your answer. I am testing it right now.

@dresden_phoenix There are 6 scenarios that could result to match, how do I handle it.

Use switch.

Scott

1 Like

do you want the SAME outcome for all six?
if so , and you don’t need $match for anything else later on in the script you could do this:

if ( ( scenario1 condition) || ( scenario2 condition) || ( scenario3 condition) || ( scenario3 condition) || ( scenario4 condition) || ( scenario5 condition) || ( scenario6 condition) ) { …}

otherwise you could consider doing using a SWITCH statement , http://php.net/manual/en/control-structures.switch.php

Great thread! I can use some of the tips you offered here, too.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.