Seems super trivial, but can't find a solution to this specific case on SO
A function may return a value of 0 OR another number, which I then want to store in a variable $s
to calculate stuff. But can't find a clean way of doing it.
So for example:
function f() {
$v = "0";
return $v;
}
if($s = f() !== false) {
echo $s;
// ^ I want 0, but the value above is 1 (since it's true)
}
I tried returning it as a string
return "0" instead of a digit, but it doesn't work.
If I do this it will not evaluate to true so nothing will happen
if($s = f()) {
// returns nothing
}
But when I var_dump(f())
, it does show string '0' (length=1)
So I can do
if(var_dump(f()) == 0)
OR
if(f() == 0)
But is there not a cleaner way to do it, so the function may return 0 or another number and I can just capture it in a variable?