Making PHP variables strict?

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

Post Reply
LonelyProgrammer
Forum Contributor
Posts: 108
Joined: Sun Oct 12, 2003 7:10 am

Making PHP variables strict?

Post 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?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Making PHP variables strict?

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

Re: Making PHP variables strict?

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