PDA

View Full Version : Just a thought, to deal with undefined variables.



djr33
09-16-2007, 01:12 AM
Undefined variables or indexes of arrays can be a real pain if you have a strong error level setup.

isset($var)?$var:'';
can be very tedious if you have a long script.

So, here's my thought--

function valueof($var) {
return isset($var)?$var:'';
}

Simple, though seems to be convenient.

Much faster than the other options.


That should be included in PHP by default, I think.

Twey
09-16-2007, 07:28 AM
Also known as:
@$var

djr33
09-16-2007, 10:26 AM
Ah, good point. I've used that before, too.
I guess I just don't like error suppression.
Is there really no downside to suppressing errors in terms of performance, etc?
I like having valid code in PHP, so everything is smooth, rather than just bypassing something that is wrong.
Though, in this case, the error message for an undefined variable is quite stupid in a lot of cases.

Twey
09-16-2007, 11:07 AM
I like having valid code in PHP, so everything is smooth, rather than just bypassing something that is wrong.
Though, in this case, the error message for an undefined variable is quite stupid in a lot of cases.The message for an undefined variable isn't an error, it's a warning: it exists to warn you that you're doing something that's quite often a sign of a bug in the script. If you know what you're doing and know that it's not caused by a bug, there's nothing wrong with suppressing it, and it's not "invalidating" your code to do so.
Is there really no downside to suppressing errors in terms of performance, etc?It has less of a performance hit than your function call and two variable accesses, certainly.