It's inefficient to do that, as from what I understand PHP actually sets the error level to none, processes that line, sets it back, and continues. Also it would suppress any other warnings from that line, which could be unrelated to the supplied argument value.
Instead, I use a 'get or else' function, as in some functional languages. For arrays, it goes
function get_or_else($array,$key,$default=null)
{ return isset($array[$key]) ? $array[$key] : $default; }
This avoids any warnings, and also allows you to specify a default.
I wasn't aware of that implementation detail. Hey, now I respect PHP even less than I did before.
Your solution makes for some good boilerplate, but things like that are exactly why I use Python for everything it makes sense for: these things are baked into the language (in Python it would be array.get(key, default) ).
Yeah, it's part of Scala's type system, too - (Option, and Some or None. Better language designers go to great lengths to make simple things like null values well covered). Python will throw a fatal error and exits on reference to a nonexistent Dictionary key, but PHP blithely fills in the value with something falsy and continues.
I'd rather be using Python too, but for some apps we have to work with a lot of PHP legacy code, so I do what I can.
To be fair, Python doesn't throw a fatal error. It throws an Exception, which most people know to handle. The idiom is use a try/except block, but if you're clever you can usually avoid the need.
Scala borrows the option types from SML (or, at least, SML has them and is much older). I love SML for its strict semantics (if something passes through the type system it most likely works as expected), but sadly design really doesn't inform platform choice (case in point: C++ is still common. I don't hate it as much as some people do, but it can't be said to be particularly well designed).
Instead, I use a 'get or else' function, as in some functional languages. For arrays, it goes
This avoids any warnings, and also allows you to specify a default.