As some people know, C # has a very useful operator ??that evaluates and returns the expression on the right if the expression on the left is NULL. This is very useful for providing default values, for example:
int spaces = readSetting("spaces") ?? 5;
If it readSettingcannot be found "spaces"and returns null, the variable spaceswill contain the default value 5.
You can do almost the same thing in JavaScript and Ruby using the operator ||, as in
var spaces = readSetting("spaces") || 5;
although you could not have 0as a value spacesin JavaScript in this case and falsein Ruby and JavaScript.
PHP has an operator or, and although it does not work as ||in a sense that it does not return an expression on the right, it can still be useful here:
$spaces = readSetting('spaces') or $spaces = 5;
with a note that ""both are "0"also considered both falsein PHP in addition to false, 0and nullin most languages.
The question is, should the construction be used from above? Does it have side effects besides treating a large class of characters as false? And is there a better design commonly used and recommended by the PHP community for this task?
source
share