Is there a way that I enforce that PHP reports error if I use an uninitialized/undefined variable?

泪湿孤枕 提交于 2019-12-24 15:46:17

问题


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:


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);



回答2:


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




回答3:


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

 error_reporting(E_ALL);



回答4:


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




回答5:


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.



来源:https://stackoverflow.com/questions/3597766/is-there-a-way-that-i-enforce-that-php-reports-error-if-i-use-an-uninitialized-u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!