Reputation: 133
Sorry for bad english , used Google.translate
There is a code that returns a value to a int, if set . Otherwise it returns false
if (isset($this->variable))
return intval ($this->variable);
else
return false;
On the receiving side condition
if ($return_value) {
// Here code
}
The problem is that if the returned value is 0, this is false, and the code is executed . But as the value of 0 is also important to me . If returned as a string , it is still treated as false.
define ('false', 'value')
does not work.
Introduced his constant for this , but you have to rewrite a bunch of code for additional testing
(if($return_value! == my_false_constant)
That is not quite satisfied.
What options are there to solve this problem ?
Upvotes: 1
Views: 636
Reputation: 31
This will work:
(if($return_value !== false){
// do work
}
Comparisons:
SO:
Upvotes: 1
Reputation: 505
Use strict comparison with ===. See: https://www.php.net/manual/en/types.comparisons.php
if(1 === true) //returns FALSE
Upvotes: 1
Reputation: 11942
if ($return_value !== false) {
}
Using !== (or ===) instead of just != or == also tests the type of the value.
Upvotes: 6