Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:46139 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 37507 invoked from network); 21 Nov 2009 05:12:31 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 21 Nov 2009 05:12:31 -0000 X-Host-Fingerprint: 92.139.47.76 ANantes-552-1-72-76.w92-139.abo.wanadoo.fr Date: Sat, 21 Nov 2009 00:12:29 -0500 Received: from [92.139.47.76] ([92.139.47.76:12521] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 17/CC-25743-CB6770B4 for ; Sat, 21 Nov 2009 00:12:29 -0500 Message-ID: <17.CC.25743.CB6770B4@pb1.pair.com> To: internals@lists.php.net User-Agent: Pan/0.133 (House of Butterflies) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Posted-By: 92.139.47.76 Subject: suggestion about ternary operator From: seza@paradoxal.org (Alban) hi all, Since the new conditionnal operator ternary was introduced in php 5.3, I'm little confuse about it. The documentations says : Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise. I think it is not very usefull because most of the time, in PHP, we need to check the existance only of a var or return a default value. $foo = isset($myArray['foo']) ? $myArray['foo'] : 'default'; I can't use the new syntax for that : // raise a warning if $myArray['foo'] not exists and return 'default' $foo = $myArray['foo'] ?: 'default'; // return 'default' if $myArray['foo'] not exists or equals '', 0, false, null $foo = @$myArray['foo'] ?: 'default'; // return true or 'default' $foo = isset($myArray['foo']) ?: 'default'; This is the same thing like using if (isset($var)) instead of if ($var), developpers always use isset() because they known that cause a warning with array and this can be evaluated to false. If they want test if $var equals 0, '' or null, they use empty(). I don't know about you, but personnaly, I use certainly 99 % of the time isset() and 1% empty(). So if the short ternary operator would be more usefull if it just test the existance of a variable. This is not a big problem but if a solution exists, this would be so cool ! Especialy when we have to check existance of twenty or more key in array. Code would be be lighter and clear. Since i use PHP, I always have in my 'common function file' a function like that : function getIssetVar($var, $default) { return ((isset($var)) ? $var : $default); } So is it possible to make a little improvement on this operator or introduce a new operator or a core function which do that ? What's your feeling about it ? -- Alban Leroux seza@paradoxal.org