Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:82581 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 42503 invoked from network); 13 Feb 2015 06:04:13 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Feb 2015 06:04:13 -0000 X-Host-Fingerprint: 195.225.93.162 195225093162.olsztyn.vectranet.pl Received: from [195.225.93.162] ([195.225.93.162:5074] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A9/03-23345-BD39DD45 for ; Fri, 13 Feb 2015 01:04:12 -0500 To: internals@lists.php.net,tpunt@hotmail.co.uk Message-ID: <54DD93D9.2040900@php.net> Date: Fri, 13 Feb 2015 07:04:09 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-Version: 1.0 References: In-Reply-To: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Posted-By: 195.225.93.162 Subject: Re: [DISCUSSION] Make empty() a Variadic From: sobak@php.net (Maciej Sobaczewski) W dniu 2015-02-12 o 19:55, Thomas Punt pisze: > Hello PHP Internals! > I'd like to propose to make empty() a variadic, where if any arguments passed in are considered empty, then false is returned - otherwise return true. > My reasoning for wanting this feature is as follows:1)It's a common scenario to want to check multiple expressions for empty values. I frequently see both of the following pieces of code in projects: #1 if (empty($a) || empty($b) || empty($c)) { // error here } > #2 if (!empty($a) && !empty($b) && !empty($c)) { // all good! } > Both of the above examples could be shortened if empty() was made to accept multiple arguments: #1 if (empty($a, $b, $c)) { // error here } > #2 if (!empty($a, $b, $c)) { // all good! } > This creates more compact code that is (in my oppinion, at least) easier to read. > Some code from real-world projects that could benefit from this feature:WordPress (one of many): https://github.com/WordPress/WordPress/blob/master/wp-admin/includes/template.php#L1963OpenCart: https://github.com/opencart/opencart/blob/45fc863fa068d82b5280890e6466a198faa54bff/upload/admin/controller/openbay/ebay_profile.php#L128phpbb: https://github.com/phpbb/phpbb/blob/040d451dcca9ae54d8f4b7bdd2f231033765a8f2/phpBB/phpbb/notification/method/jabber.php#L48 > > 2)Users have brought up the want to pass in multiple arguments into empty() before, such as:http://stackoverflow.com/questions/4993104/using-ifempty-with-multiple-variables-phphttp://stackoverflow.com/questions/10950470/check-if-multiple-strings-are-empty > There have been solutions brought up by users to emulate a variadic empty, like [1][2], however for unset variables their solutions simply don't work. > > So all in all, it seems like a simple feature to add for a short-hand notation of checking multiple expressions for emptiness (which seems like a common use-case for users). It has no BC implications and no real downsides (at least I couldn't think of any). I have created a patch [3], and if the feedback is positive, then I'll create an RFC and submit a PR. > Thanks,Tom > [1] http://stackoverflow.com/a/7798842/4530326[2] http://icoded.it/php-time-saving-function-to-check-multiple-variables-for-empty-values/[3] https://github.com/tpunt/php-src/commit/66c563829775770507147872b98320cdfcb6c51c > I like the idea. It seems consistent with isset() being variadic as well.