Andrew Faulds | 4 Aug 2012 02:54

[PHP-DEV] [proposal+patch] Addition of ?() as shorthand for isset()

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. :)
(Continue reading)


Gmane