Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if statement and functions

Tags:

php

web

I sometimes see code that looks like this:

if(!Somefunction()){
    // ...
}else{
    // ...
}

I am more of a java developer and the only way I know the above statement holds is if the function returns a true or false at the end.

Does every function that is used in this format have to return a true or false? Or does this mean if the function is successfully executed?

like image 935
alaboudi Avatar asked Apr 26 '26 23:04

alaboudi


1 Answers

No, not necessarily True or False. In PHP any variable can be converted to boolean.

When converting to boolean, the following values are considered False:

the boolean FALSE itself
the integer 0 (zero)
the float 0.0 (zero)
the empty string, and the string "0"
an array with zero elements
an object with zero member variables (PHP 4 only)
the special type NULL (including unset variables)
SimpleXML objects created from empty tags
like image 62
Salvador Dali Avatar answered Apr 28 '26 12:04

Salvador Dali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!