Search code examples
phpstringcomparison-operators

I cannot compare two strings in PHP


    <?php
    $gender = "devilcode";
    if (($gender == "female") || ($gender = "male"))
        {
            echo "ok";
        }
    else echo "no";
    ?>

It should output "no" but it outputs "ok". What am I doing wrong?


Solution

  • You are assigning $gender to be male, rather than testing it for comparison, you need two equal signs:

    $gender = "devilcode";
    if (($gender == "female") || ($gender == "male"))
        echo "ok";
    else 
        echo "no";