Re: A 'missing' PHP primitive?

From: Date: Wed, 09 Nov 2016 16:28:05 +0000
Subject: Re: A 'missing' PHP primitive?
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-325831@lists.php.net to get a copy of this message
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

« previous php.general (#325831) next »