views:

27

answers:

5

Hello all,

I made a huge mistake by mixing result with results and it took me around 4 hours to finally find the bug.

So here is the question, in PHP, is it possible that I can enforce PHP to report errors if I use an undefined/uninitialized variable.

thank you

+1  A: 

yes, use error_reporting() and set it to E_ALL, like this:

 error_reporting(E_ALL);
shamittomar
+2  A: 

Set error reporting to E_ALL and ensure that display_errors in php.ini is on.

php.ini

display_errors = On

PHP code

// If you cannot access the php.ini file
// you can do this within your PHP code instead
@ini_set('display_errors' '1');
error_reporting(E_ALL);

The default setting you have right now probably excludes notices, the kind of errors PHP raises on uninitialized variables, which could be something like this:

error_reporting(E_ALL & ~E_NOTICE);
BoltClock
Hello BoltClock,Where is the best place I put the setting? I have a constant.php that is included by almost all php scripts. Is that a good method to insert the setting into that include file?thank you
q0987
You can place it in a config file or constants file that you know will be included in all of your scripts.
BoltClock
+1  A: 

Set error reporting to report all errors. Either in php.ini or at runtime using error_reporting(E_ALL)

BDuelz
+1  A: 

it already does report an error. something like this:

"Notice:  Undefined variable: a in C:\wamp\www\testcenter\index.PHP on line 40"

maybe you didn't go specific enough. but you should try error_reporting(-1); as as if enforces the php to show some recomendations. a piece from the php manual about E_STRICT errors:

Enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code.

just remember that error_reporting(-1); shows more errors than error_reporting(E_ALL); because E_STRICT errors are not included in the E_ALL constraint.

hugo_leonardo
+2  A: 

In a development environment I prefer using error_reporting(-1). Which reports all PHP errors.

Russell Dias
+1 For the benefit of the doubt, this is equivalent to `error_reporting(E_ALL | E_STRICT)` as `E_ALL` by itself *does not* include `E_STRICT`.
BoltClock