Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP and ?? operator

Tags:

php

Like some people know, C# has very useful ?? operator which evaluates and returns expression on the right if expression on the left is null. It's very useful for providing default values, for example:

int spaces = readSetting("spaces") ?? 5;

If readSetting couldn't find "spaces" and returns null, variable spaces would hold default value of 5.

You could do almost the same in JavaScript and Ruby with || operator, as in

var spaces = readSetting("spaces") || 5;

although you couldn't have 0 as value of spaces in JavaScript in this case and false in both Ruby and JavaScript.

PHP has or operator and although it doesn't work as || in a sense that it doesn't return expression from the right, it still could be useful here:

$spaces = readSetting('spaces') or $spaces = 5;

with the note that "" and "0" are also treated like false in PHP in addition to false, 0 and null in most languages.

The question is, should I use the construction from above? Does it have side effects apart from treating large class of characters as false? And is there better construction that is usually used and recommended by PHP community this task?

like image 676
vava Avatar asked Mar 11 '09 13:03

vava


1 Answers

It is a good idea to be more explicit in cases like this, especially in PHP since it has somewhat confusing type conversion rules (e.g. "0" is false as you pointed out).

If you want to be strict, let your function readSettings return the correct setting or a well-defined value if no setting is found, e.g. null. Then you should use it as:

$spaces = readSettings('spaces');
if (null === $spaces) {
    $spaces = 5;
}

If you want to be more generous and only want $spaces to be non-empty, use:

$spaces = readSettings('spaces');
if (empty($spaces)) {    // or:  if (!$spaces) {
    $spaces = 5;
}

You could shorten it by the cost of a second function call or ugly constructs (not recommended):

$spaces = readSettings('space') ? readSettings('space') : 5;
$spaces = ($x = readSettings('space')) ? $x : 5;  // UGLY!

But watch out if you want 0 to be a valid value for $spaces!

From the Zen of Python:

Explicit is better than implicit.

In your case I would recommend to just add a second paramter $default to readSettings():

function readSettings($key, $default=null) {
    return isset($settings[$key]) ? $settings[$key] : $default;
}

$spaces = readSettings('spaces', 5); 
like image 71
Ferdinand Beyer Avatar answered Nov 08 '22 10:11

Ferdinand Beyer