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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.