Welcome Guest, Not a member yet? Register   Sign In
Simple if statement returning unexpected results
#1

[eluser]Lone[/eluser]
Ok so right now I feel like a complete noob asking this but can someone explain why the following happens in an if statement in PHP (the third snippet)... Its probably something simple I am missing but just want to see exactly what it is.

A string is treated as 'true' which is expected
Code:
$test1 = true;
$test2 = 'string';
if ($test1 == $test2) {
    echo 'yes';
}
// Outputs 'yes' as expected


The number 0 is treated as 'false' which is expected
Code:
$test1 = false;
$test2 = 0;
if ($test1 == $test2) {
    echo 'yes';
}
// Outputs 'yes' as expected


So if 0 is meant to be 'false' and a string is 'true' then why does the below statement get treated as true?
Code:
$test1 = 0;
$test2 = 'string';
if ($test1 == $test2) {
    echo 'yes';
}
// Outputs 'yes' - NOT expected


Messages In This Thread
Simple if statement returning unexpected results - by El Forum - 05-08-2008, 05:43 PM
Simple if statement returning unexpected results - by El Forum - 05-08-2008, 08:40 PM
Simple if statement returning unexpected results - by El Forum - 05-08-2008, 09:19 PM
Simple if statement returning unexpected results - by El Forum - 05-08-2008, 10:49 PM
Simple if statement returning unexpected results - by El Forum - 05-08-2008, 11:29 PM
Simple if statement returning unexpected results - by El Forum - 05-08-2008, 11:51 PM
Simple if statement returning unexpected results - by El Forum - 05-09-2008, 09:13 PM
Simple if statement returning unexpected results - by El Forum - 05-09-2008, 09:57 PM



Theme © iAndrew 2016 - Forum software by © MyBB