r/PHPhelp • • Dec 12 '25

Solved String integer comparison

So here I was working the natas 23 web challenge when I encountered a bit of a dilemma. Spoiler alert by the way if anyone cares. Anyways, the code compares a string to an integer, and as I've now learned, when this happens and the string starts with a digit, that number will be used in place of the string for comparison, else the string becomes 0 and evaluation continues. HOWEVER, when using online php compilers that doesn't seem to be happening. Below is the code I've used that is producing a different behavior. In it, the if statement evaluates to true for whatever reason. Does anyone understand what's happening? Because I don't :D:D:D:D :I :I :I :I

$val = "iloveyou";

if(strstr($val, "iloveyou")){

if($val > 10){

echo "All goods";

}

else{

echo "No ten :(";

}

}

else{

echo "No love :( ";

}

6 Upvotes

10 comments sorted by

View all comments

Show parent comment

3

u/dave8271 Dec 13 '25

The person below has given you the explanation about why (string comparison is alphabetical, basically, based on ASCII codes).

Otherwise the only difference is between PHP versions < 8 or >= 8, not the code examples you've posted.

if ('iloveyou' > 10) {
    echo 'true';
} else {
    echo 'false';
}

This will be true on PHP 8, false on PHP 7.