Jump to content

Variables: To define or not to define?


Arty Ziff

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/223069-variables-to-define-or-not-to-define/
Share on other sites

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.