4 Aug 2012 21:30
[PHP-DEV] Should we have some sort of isset()-like operator with default?
Andrew Faulds <ajf <at> ajf.me>
2012-08-04 19:30:40 GMT
2012-08-04 19:30:40 GMT
Hi there,
In the "Implict isset in ternary operator" thread, the idea of a special
version of the ternary operator with an isset implied was proposed. I
don't like this idea very much, but from that I want to suggest some
sort of "default" operator.
On IRC I had a little discussion about it with nikic (Nikita), salathe
(IRC realname is Peter) and Tyrael (Ferenc).
Basically, I end up writing code like this a lot:
if (!isset($relations[$prev])) {
$relations[$prev] = [];
}
I don't like isset(), it looks quite ugly to me (although I'm fine with
empty()). And I think a non-keyword operator for it would be more
readable, and save space.
My basic idea is summed up by salathe here: https://gist.github.com/3256826
An operator (let's use ? for the time being, whether that becomes the
actual operator or not) is proposed that functions like isset, so:
$var? // same as isset(var)
This on its own is already shorter than isset(), and imo, nicer to read:
if ($var?) { /* ... */ }
(Continue reading)
RSS Feed