Re: A 'missing' PHP primitive?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 09.11.2016 at 17:17, Josh Stern wrote:

>>> Hi, I'm wondering if there is any built-in PHP primitives to perform the
>>> same task as this simple, almost trivial function:
>>>
>>> function get(&$var) {
>>>       return $var;
>>> }
>>>
>>> ?
>>>
>>>
>>> It is useful for accessing arrays where some fields may be unset.  For
>>> example:
>>>
>> As of PHP 7.0.0 there is the null coalescing opertor, so you can write:
>>
>>    $arr[1] ?? NULL
>>
>>> It seems like there should be a built in primitive called 'get' or
>>> 'ifset' that does this task.
>> See
>> <https://nikic.github.io/2014/01/10/The-case-against-the-ifsetor-function.html>.
> 
> Thanks, perfect answer.  The PHP 7 operator is the built in way to go. 
> Unlike my function, or the ones described in the link, it DOES NOT
> silently create NULL keys in the fields it checks.  So the other methods
> are inferior unless one actually wishes to do that.

In which case the null coalescing assignment operator[1] (which is
scheduled for PHP 7.2.0) could be used:

  $arr[1] ??= NULL;

[1] <https://wiki.php.net/rfc/null_coalesce_equal_operator>

-- 
Christoph M. Becker


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux