error_reporting(E_ALL) - how picky are you guys?
Moderator: General Moderators
Looks like I'm the same as everyone else - I work with E_ALL, and eliminate all notices. I hate seeing any errors, no matter how trivial.
A note to the Original Poster - using isset in that context is not correct in all cases:
Compare:
with
if($var) tests that the value of $var is not null, false or zero, whereas if(isset($var)) tests whether the variable has been declared and assigned a non-null value. These are two different types of test, and replacing one with the other could lead to problems in certain circumstances (as above).
I think that gives you your answer to "How picky are you?"
A note to the Original Poster - using isset in that context is not correct in all cases:
Compare:
Code: Select all
$var = false;
if(isset($var)) {
// enter here, because $var is set and not null
}Code: Select all
$var = false;
if($var) {
// do not enter here, because $var is false
}I think that gives you your answer to "How picky are you?"
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
I think empty() checks for isset AND a non-false value. I think.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
isset(), empty(), array_key_exists(). I'll use whatever is suitable in order to avoid such trivial notices/warnings. I always work with E_ALL errors in development.
Now, after listing those three functions above, I often find myself doing this:
When I could do this:
I should benchmark to see what's faster but I guess it's the array_key_exists() approach.
Now, after listing those three functions above, I often find myself doing this:
Code: Select all
if (isset($array['key']))
{
//do something
}Code: Select all
if (array_key_exists('key', $array))
{
//do something
}- MrPotatoes
- Forum Regular
- Posts: 617
- Joined: Wed May 24, 2006 6:42 am
Code: Select all
$var = false;
if($var) {
// do not enter here, because $var is false
}I personally use functions that return a default value if the var is not set, and convert it to string or int, with some additional (optional) checks like putting the int between min and max, or the string in a given size. It is easy to write that way and the code is more beautiful (=readable)
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
It can be used for get...
But usually an isset() check does the trick.
Code: Select all
$var = $_GET['somevar'];
if($var) {
// do not enter here, because $var is false
}I heard that PHP 6 is going to have a kind of "isset_else" function that allows you to do something like:
if $_POST['somevar'] is set, then assign it's value to $var, else assign "default" to $var.
This would be a nice shortcut over:
Code: Select all
$var = isset_else($_POST['somevar'] , "default");This would be a nice shortcut over:
Code: Select all
$var = isset($_POST['somevar']) ? $_POST['somevar'] : "default";- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
I've been pulling away from the use of isset(). The reason why is due to how some of the systems I interact with use nulls, which can signal a false positive in isset(). For example:
Code: Select all
[feyd@home]>php -r "$foo = array('bar'=>null); var_dump(isset($foo['bar']), array_key_exists('bar', $foo),$foo);"
bool(false)
bool(true)
array(1) {
["bar"]=>
NULL
}- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
For that, I would simply include an is_null() (or === null)
Code: Select all
if (!isset($foo['bar']) || is_null($foo['bar'])) continue;