Jump to content

php $test = 0


jphillips67

Recommended Posts

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";
}
?>
 
 
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by venkatpvc
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.