One of the things I like the most of JavaScript is that the logical operators are very powerful:
-
&&can be used to safely extract the value of an object’s field, and will return null if either the object or the field has not been initialized// returns null if param, param.object or param.object.field // have not been set field = param && param.object && param.object.field; -
||can be used to set default values:// set param to its default value param = param || defaultValue;
Does PHP allow this use of the logical operators as well?
PHP returns
trueorfalse. But you can emulate JavaScript’sr = a || b || cwith:Regarding ‘ands’, something like: