Page 1 of 1
Making PHP variables strict?
Posted: Wed Apr 30, 2008 12:51 pm
by LonelyProgrammer
Hi,
Is there a way to set PHP such that if a variable has never been assigned a value, it will throw a warning? Such as:
Code: Select all
$my_name = "user@userid";
echo "Welcome, $myname!";
And line 2 will throw a warning?
Re: Making PHP variables strict?
Posted: Wed Apr 30, 2008 1:15 pm
by Zoxive
This is the norm of php, but I'm guessing your error reporting is set to not show errors.
Look in your php.ini file and set error_reporting to E_ALL or E_STRICT.
You can also do this in the php file itself (For testing purposes) with
error_reporting();
Re: Making PHP variables strict?
Posted: Fri May 02, 2008 1:30 pm
by RobertGonzalez
Those are warning level notices, so at a minimum you would want error reporting set to E_WARN. For optimal code, set it E_ALL. If you are in PHP 5 set it to E_ALL | E_STRICT (since E_STRICT does not cover E_ALL in PHP 5 though there is talk that the two will be combined in PHP 6).
ANother thing you may want to do, in development at least, is to set display_errors to On so that you can the warnings and error notices instead of constantly having to refer to the error logs.