Page 2 of 2

Posted: Wed Sep 13, 2006 4:22 am
by Jenk
It's far more time efficient (and sanity saving) to just code with E_ALL on from the offski.

Posted: Wed Sep 13, 2006 4:58 am
by GM
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:

Code: Select all

$var = false;

if(isset($var)) {
    // enter here, because $var is set and not null
}
with

Code: Select all

$var = false;

if($var) {
    // do not enter here, because $var is false
}
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?" :)

Posted: Wed Sep 13, 2006 10:16 am
by RobertGonzalez
I think empty() checks for isset AND a non-false value. I think.

Posted: Wed Sep 13, 2006 10:47 am
by Jenk
empty() returns true when the variable is not set, or has a value of NULL, 0, "", array() [empty array] or boolean false.

Posted: Wed Sep 13, 2006 11:22 am
by Chris Corbyn
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:

Code: Select all

if (isset($array['key']))
{
    //do something
}
When I could do this:

Code: Select all

if (array_key_exists('key', $array))
{
    //do something
}
I should benchmark to see what's faster but I guess it's the array_key_exists() approach.

Posted: Wed Sep 13, 2006 11:35 am
by MrPotatoes
i turn off notices so i can see real errors. then i turn them on so that i can see the notices as well

Posted: Wed Sep 13, 2006 11:38 am
by Luke
I do what I do because i like being satisfied with a finished product... if I were to leave notices and such on a finished product... I would not be satisfied and therefor I would need to beat somebody up.

Posted: Wed Sep 13, 2006 12:57 pm
by Mordred

Code: Select all

$var = false; 

if($var) { 
    // do not enter here, because $var is false 
}
This cannot be used for $_GET, etc. so what do you do in that case?

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)

Posted: Wed Sep 13, 2006 1:44 pm
by RobertGonzalez
It can be used for get...

Code: Select all

$var = $_GET['somevar'];

if($var) {
    // do not enter here, because $var is false
}
But usually an isset() check does the trick.

Posted: Thu Sep 14, 2006 6:38 am
by GM
I heard that PHP 6 is going to have a kind of "isset_else" function that allows you to do something like:

Code: Select all

$var = isset_else($_POST['somevar'] , "default");
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($_POST['somevar']) ? $_POST['somevar'] : "default";

Posted: Thu Sep 14, 2006 8:19 am
by RobertGonzalez
I have heard that too. And it will be nice.

Posted: Thu Sep 14, 2006 9:02 pm
by feyd
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
}

Posted: Fri Sep 15, 2006 11:05 am
by RobertGonzalez
How does that happen? NULL is not standard?

Posted: Fri Sep 15, 2006 11:19 am
by Jenk
For that, I would simply include an is_null() (or === null)

Code: Select all

if (!isset($foo['bar']) || is_null($foo['bar'])) continue;