Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62030 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 31538 invoked from network); 4 Aug 2012 00:54:28 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 Aug 2012 00:54:28 -0000 Authentication-Results: pb1.pair.com header.from=ajf@ajf.me; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ajf@ajf.me; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain ajf.me designates 64.22.89.133 as permitted sender) X-PHP-List-Original-Sender: ajf@ajf.me X-Host-Fingerprint: 64.22.89.133 oxmail.registrar-servers.com Linux 2.6 Received: from [64.22.89.133] ([64.22.89.133:43778] helo=oxmail.registrar-servers.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 71/37-23476-3C27C105 for ; Fri, 03 Aug 2012 20:54:27 -0400 Received: from [192.168.0.200] (5ad4bfa0.bb.sky.com [90.212.191.160]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by oxmail.registrar-servers.com (Postfix) with ESMTPSA id 9500CC30001 for ; Fri, 3 Aug 2012 20:54:24 -0400 (EDT) Message-ID: <501C72A8.7060903@ajf.me> Date: Sat, 04 Aug 2012 01:54:00 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 MIME-Version: 1.0 To: PHP Internals Content-Type: multipart/mixed; boundary="------------010701050705090906040603" Subject: [proposal+patch] Addition of ?() as shorthand for isset() From: ajf@ajf.me (Andrew Faulds) --------------010701050705090906040603 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Hi! I've always thought isset() was quite ugly. For something I use quite frequently for checking the existence of array keys, I feel it is too many keystrokes, and is ugly. So, I decided I would create a shorthand for isset(). One option suggested was a new operator, 'expr ??', which I don't like the idea of very much. I would also have liked 'expr ?', but that would conflict with the ternary operator, 'expr ? expr : expr'. Then, I had the idea of '? expr', but that also caused a conflict. So I settled on '? ( expr )', which is used like this: if (?($_POST['credit_card_no'])) { // ... } else if (!?($_POST['use_paypal']) && ?($_POST['bank_acc_no'])) { // ... } And since it doesn't break the ternary operator, like this: $number = ?($x)? intval($x) : 0; I quite like how it combines with !, so !?() tests for non-existence, and ?() tests for existence. It's quite trivial, but it would save me (and probably many others) time, and I think it would add some extra character to PHP, with its quirky syntax. :) Patch attached. Thoughts welcome. -- Andrew Faulds http://ajf.me/ --------------010701050705090906040603 Content-Type: text/x-patch; name="shorthand_isset.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="shorthand_isset.patch" From 24107911374fd9e9dbc731b4564a440dd72f5568 Mon Sep 17 00:00:00 2001 From: Andrew Faulds Date: Sat, 4 Aug 2012 01:37:23 +0100 Subject: [PATCH] Added ?() op as shorthand for isset() - Added ?() - Added test --- Zend/zend_language_parser.y | 1 + tests/lang/shorthand_isset.phpt | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 tests/lang/shorthand_isset.phpt diff --git a/Zend/zend_language_parser.y b/Zend/zend_language_parser.y index c88e9a7..5781cd6 100644 --- a/Zend/zend_language_parser.y +++ b/Zend/zend_language_parser.y @@ -773,6 +773,7 @@ expr_without_variable: | '(' expr ')' { $$ = $2; } | new_expr { $$ = $1; } | '(' new_expr ')' { $$ = $2; } instance_call { $$ = $5; } + | '?' '(' isset_variables ')' { $$ = $3; } | expr '?' { zend_do_begin_qm_op(&$1, &$2 TSRMLS_CC); } expr ':' { zend_do_qm_true(&$4, &$2, &$5 TSRMLS_CC); } expr { zend_do_qm_false(&$$, &$7, &$2, &$5 TSRMLS_CC); } diff --git a/tests/lang/shorthand_isset.phpt b/tests/lang/shorthand_isset.phpt new file mode 100644 index 0000000..9218f89 --- /dev/null +++ b/tests/lang/shorthand_isset.phpt @@ -0,0 +1,21 @@ +--TEST-- +?() (Shorthand isset()) operator test +--FILE-- +