error_reporting(E_ALL) - how picky are you guys?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

It's far more time efficient (and sanity saving) to just code with E_ALL on from the offski.
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post 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?" :)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I think empty() checks for isset AND a non-false value. I think.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

empty() returns true when the variable is not set, or has a value of NULL, 0, "", array() [empty array] or boolean false.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Post 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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post 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";
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I have heard that too. And it will be nice.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
}
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

How does that happen? NULL is not standard?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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;
Post Reply