Over on stackoverflow, I see this issue crop up all the time:
- E_NOTICE ?== E_DEBUG, avoiding isset() and @ with more sophisticated error_handler
- How to set PHP not to check undefind index for $_GET when E_NOTICE is on?
- How to stop PHP from logging PHP Notice errors
- How do I turn off such PHP 5.3 Notices ?
Even Pekka (who offers a lot of solid PHP advice) has bumped against the dreaded E_NOTICE
monster and hoped for a better solution than using isset()
: isset() and empty() make code ugly
Personally, I use isset()
and empty()
in many places to manage the flow of my applications. For example:
public function do_something($optional_parameter = NULL) {
if (!empty($optional_parameter)) {
// do optional stuff with the contents of $optional_parameter
}
// do mandatory stuff
}
Even a simple snippet like this:
if (!isset($_REQUEST['form_var'])) {
// something's missing, do something about it.
}
seems very logical to me. It doesn't look like bloat, it looks like stable code. But a lot of developers fire up their applications with E_NOTICE
's enabled, discover a lot of frustrating "uninitialized array index" notices, and then grimace at the prospect of checking for defined variables and "littering" their code with isset()
.
I assume other languages handle things differently. Speaking from experience, JavaScript isn't as polite as PHP. An undefined variable will typically halt the execution of the script. Also, (speaking from inexperience) I'm sure languages like C/C++ would simply refuse to compile.
So, are PHP devs just lazy? (not talking about you, Pekka, I know you were refactoring an old application.) Or do other languages handle undefined variables more gracefully than requiring the programmer to first check if they are defined?
(I know there are other E_NOTICE
messages besides undefined variables, but those seem to be the ones that cause the most chagrin)
Addendum
From the answers so far, I'm not the only one who thinks isset()
is not code bloat. So, I'm wondering now, are there issues with programmers in other languages that echo this one? Or is this solely a PHP culture issue?