Skip to content
Advertisement

How can I check if vars are equal 0? [closed]

On php documentation we can see:

"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)

If I have for example $var1 = 0; and $var2 = 0; on php I can do:

if (empty($var1)){
echo "empty";
}

But I cant do this:

if (empty($var1 && $var2)){
echo "empty";
}

How can I do for check if the two vars are equal to 0 on the same if?

Advertisement

Answer

You can run

if( $var1 === 0 && $var2 === 0 ) {
   // both empty
}

to see if those two variable are both holding the integer zero.

If you want a more loose type check (see if the variables are zero, or false, or null, for example), you can do:

if( empty($var1) && empty($var2) ) {
   // both empty
}

If (based on your question) you wanted to see if the variable holds the string "0", you would do it like this:

if( $var1 === "0" && $var2 === "0" ) {
   // both are the string "0"
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement