I just turned on notices because开发者_如何学Go it has some important information I need with debugging... with that being said, I find the undefined variables to be a real pain in the butt.
For example, to remove an undefined variable notice I have to turn the following code:
if($the_month != $row['the_month'])
into
if(isset($the_month) && $the_month != $row['the_month'])
Is there another way around this? This solution seems like a time waster to me.
Define the variable before using it. It's the only way to be sure.
A lot of web hosts still have register_globals
enabled. This allows any visitor to inject variables into your script by adding stuff to the query string.
For example, if your script is called as example.php?the_month=5
, the variable $the_month
is automatically filled with 5. This can be very dangerous, because someone might be able to mess with important variables related to security! For this reason, register_globals
is now deprecated.
But that doesn't change the fact that a lot of web hosts still have it enabled, so every PHP developer must define every variable to something safe before using it. Otherwise you have no guarantee that the variable will contain what you think it does.
This answer may help as it is similar:
PHP: printing undefined variables without warning
For example:
You can run it with the error suppression operator @.
echo @$variable;
However, it's best not to ignore unset variables. Unset variables could indicate a logical error on the script, and it's best to ensure all variables are set before use.
Always make sure that $the_month
is set to something, even NULL
.
You must not carelessly access variables that may or may not be defined, it's that simple. Just because PHP lets you do it doesn't mean it's a sane way to write code. Please refer to my extensive answer to a similar question here: How to avoid isset() and empty()
精彩评论