Tech Lull
 
 
JavaPHPSQLDrupalOpenGLMathematicsRenderings
 
 

using error_reporting to help you

back to PHP tips...
Printer friendlyPrinter friendly

In PHP 5, the default level is E_ALL & ~E_NOTICE. While it is nice to hide the 'Notice' messages, it is better to set the error reporting level to just E_ALL. Notice messages usually tell you best practice things like if a variable has not been initiated. If messages are hard to get rid of, this can indicate underlying issues with program logic. For example:

$GLOBALS["foo"]["path"]["relative"]["bar"]["images"] = 'something'; ... Notice: Undefined index: foo

The question of 'why use a 5 dimensional array?' comes up. (this is just an example)

As developers we should strive to have high quality code and use all the tools we are provided with - one of them being error reporting.

for more on error_reporting function

//instead of error_reporting(E_ALL & ~E_NOTICE); //use error_reporting(E_ALL);

the most common reason for E_NOTICE messages are not initiating a variable or not ensuring that a variable is set before trying to check it's value.

1)  initiate all variables

function a(){ if($b){ $html .= 'something'; }else{ $html .= 'something else'; } return $html; }

will produce the message

Notice: Undefined variable: html ... function a(){ $html = ''; if($b){ $html .= 'something'; }else{ $html .= 'something else'; } return $html; }

is better. this is a practice to get into, for security and other reasons

2) if you get errors on a class variable

if($this->some_attribute){;}

being not defined, then you should define it!

class foobar{ private $some_attribute; }

For $_GET, $_POST, $_SESSION, $_COOKIE, check the variable is set before trying to use it's value

if($_GET['id'])

should be

if(isset($_GET['id'] && $_GET['id'])

3) use quotes on word array indices. PHP is usually smart enough to guess, but do not make it. example:

//bad echo $animal[dog]; //good echo $animal['dog'];
Your rating: None Average: 3 (1 vote)
back to PHP tips...