php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #62043 new operator
Submitted: 2012-05-15 23:23 UTC Modified: 2015-12-24 21:23 UTC
From: piotrekz5 at wp dot pl Assigned:
Status: Not a bug Package: PHP options/info functions
PHP Version: Irrelevant OS: any
Private report: No CVE-ID: None
 [2012-05-15 23:23 UTC] piotrekz5 at wp dot pl
Description:
------------
Hi,
Let's implement a new operator - '??' (used in c#).

The ?? operator is called the null-coalescing operator and is used to define a default value. It returns the left-hand operand if the operand is not null; otherwise it returns the right operand.


Test script:
---------------
example:
$var1 = null;
$var2 = null;
$var3 = $var1 ?? $var2; //$var3==null
$var4 = $var1 ?? 'default1'; //$var4=='default1'
$var5 = $var1 ?? $var2 ?? 'default2'; //$var5=='default2'
$var6 = $var2 ?? $var3 ?? $var4 ?? 'default3'; //$var6=='default1' since the first non-null value is $var4=='default1'


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-05-16 00:31 UTC] phpmpan at mpan dot pl
---- BEGIN CODE ----
function ifNull($var, $value, $null = NULL) {
    return ($var !== $null)? $var : $value;
}
----- END CODE -----

Or even simpler to use and, unlike previous, working fine with undefined variables:

---- BEGIN CODE ----
function unNull(&$var, $value, $null = NULL) {
    if ($var === $null) {
        $var = $value;
    }
    return $var;
}
----- END CODE -----

If we drop the last argument even multiple operands version can be achieved:
---- BEGIN CODE ----
function nonNull() {
    return array_reduce(func_get_args(), function(&$a, $e) {
        return ($a === NULL && $e !== NULL)? $e : $a;
    });
}
----- END CODE -----
Any reason to not do this in userland?
 [2012-05-16 06:09 UTC] reeze dot xia at gmail dot com
Do you mean this operator?

$a = $a ?: "default value");

http://www.php.net/manual/en/language.operators.comparison.php#language.operators.
comparison.ternary
 [2012-05-18 14:43 UTC] maarten@php.net
Code
----
<?php
// https://bugs.php.net/bug.php?id=62043

$var1 = null;
$var2 = null;
$var3 = $var1 ?: $var2; //$var3==null
$var4 = $var1 ?: 'default1'; //$var4=='default1'
$var5 = $var1 ?: $var2 ?: 'default2'; //$var5=='default2'
$var6 = $var2 ?: $var3 ?: $var4 ?: 'default3'; //$var6=='default1' since the first non-null value is $var4=='default1'

var_dump($var1, $var2, $var3, $var4, $var5, $var6);

----

Already outputs the following on all >= 5.3.0 versions (tested with 5.3.0-5.3.13, 5.4.0-5.4.3) :
----
NULL
NULL
NULL
string(8) "default1"
string(8) "default2"
string(8) "default1"
----
(see http://3v4l.org/rKOqU )

So the ?: operator matches your proposed behaviour.
 [2012-05-18 14:44 UTC] maarten@php.net
Thank you for taking the time to report a problem with PHP.
Unfortunately you are not using a current version of PHP -- 
the problem might already be fixed. Please download a new
PHP version from http://www.php.net/downloads.php

If you are able to reproduce the bug with one of the latest
versions of PHP, please change the PHP version on this bug report
to the version you tested and change the status back to "Open".
Again, thank you for your continued support of PHP.


 [2012-05-18 14:44 UTC] maarten@php.net
-Status: Open +Status: Not a bug
 [2015-12-24 21:23 UTC] ajf@php.net
A variation on this did get in: http://wiki.php.net/rfc/isset_ternary
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 18:01:28 2024 UTC