jphillips67 Posted September 25, 2015 Share Posted September 25, 2015 This has got me really confused, when did setting a variable = 0 make everything true in an if then statement (except for numbers) or has this always been the case and I just never ran across it. Is there a possible setting in php? The code below should not echo Hello world but it does. <?php $test = 0; if ($test == "Y") { echo "Hello world"; } ?> This code will echo Hello World and I do not believe it should either.... <?php $test = 0; if ($test == "TEST") { echo "Hello world"; } ?> Comment out the var $test and the code work as expected does not echo out Hello World //$test = 0; if ($test == "TEST") { echo "Hello world"; } ?> Set var = "0" and it works as expected does not echo out Hello World <?php $test = "0"; if ($test == "1") { echo "Hello world"; } ?> Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted September 25, 2015 Share Posted September 25, 2015 It is because PHP is a loosely typed language. Because you are comparing an integer to a string PHP will be converting the strings, "Y" and "TEST" to be integers, which will result in them being truncated to zero. Therefore your if statements always evaluate to true. What you need to use is === which checks whether both values are equal and are of the same type. See the following for reference http://php.net/manual/en/language.types.type-juggling.php http://php.net/manual/en/types.comparisons.php Quote Link to comment Share on other sites More sharing options...
jphillips67 Posted September 26, 2015 Author Share Posted September 26, 2015 Thanks did not know this. Will make this part of my plan moving forward. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted September 27, 2015 Share Posted September 27, 2015 If that already confused you, how about this:? $str_1 = "150e3"; $str_2 = " 150000"; var_dump($str_1 == $str_2); // true So PHP not only converts operators with different types. It also converts operators with the same type into another type when it has the chance to (in this case, it regards the strings as numbers in exponential notation). To get around this madness, be explicit and don't rely on PHP doing the right thing. It won't. If you want to compare strings, use strcmp() or the === operator as suggested by Ch0cu3r. Of course you may take advantage of some automatic conversions (that's what PHP is for, right?), but then you should really understand what's going on internally. Quote Link to comment Share on other sites More sharing options...
venkatpvc Posted September 27, 2015 Share Posted September 27, 2015 (edited) while Ch0cu3r said works perfectly. for learning purpose you can try below too. <?php /*$test = 0; if ($test === "Y") { echo "Hello world"; }*/ //or you can set type explicitly and test $test = 0; settype($test, "integer"); if ($test == TRUE && $test == "Y") { echo "Hello world"; } ?> Edited September 27, 2015 by venkatpvc Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted September 27, 2015 Share Posted September 27, 2015 No, this doesn't make any sense. The settype() function doesn't actually “set a type”. PHP is not a statically typed language like Java, it doesn't have fixed variable types. What this function does is convert the value of the variable, but since that value already is an integer in your case, the function has no effect whatsoever. Checking for $test == true is the same as checking for $test, so there's no good reason for the extra “== true”. Last but not least, this approach is really complicated and confusing. If I didn't know the context, I probably wouldn't even understand what you're trying to do there. So you shouldn't do this, not even for learning. Make your code explicit and easy to understand. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.