Arty Ziff Posted December 31, 2010 Share Posted December 31, 2010 In many cases, variable have to be explicitly defined for whatever reason. However, often it is not required for a simple web page script. Yet, even in those cases, if a variable is NOT explicitly defined, the web server error log (example: my_domain.com-error_log) will rack up a lot of entries such as this: [Fri Dec 31 01:31:43 2010] [error] [client 123.123.123.123] PHP Notice: Undefined variable: some_variable_name in /home/my_domain/public_html/some_page.php on line 6 A busy site could result in large error logs... What is considered "best practice"? Should all variables always be explicitly defined with the var key word? Quote Link to comment https://forums.phpfreaks.com/topic/223069-variables-to-define-or-not-to-define/ Share on other sites More sharing options...
johnny86 Posted December 31, 2010 Share Posted December 31, 2010 You should always define variables before using them. Every error message that is created will not only create error message to the log. But it also slows scripts down a lot. It is costly to have errors in the script. Quote Link to comment https://forums.phpfreaks.com/topic/223069-variables-to-define-or-not-to-define/#findComment-1153297 Share on other sites More sharing options...
PFMaBiSmAd Posted December 31, 2010 Share Posted December 31, 2010 The var keyword only has meaning in php4 inside of a class and would have no effect on the error you used in your example. Your code should not produce any errors during its normal execution, only for unexpected things. See this recent post for why - http://www.phpfreaks.com/forums/php-coding-help/can%27t-connect-to-mysql-db-from-php-code-on-localhost/msg1505574/#msg1505574 Quote Link to comment https://forums.phpfreaks.com/topic/223069-variables-to-define-or-not-to-define/#findComment-1153298 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.