Undefined php variable change workaround?

I just turned on notifications because it has some important information that I need with debugging ... with that said, I find that undefined variables are a real pain in the butt.

For example, to remove the notification of an undefined variable, I need to rotate the following code:

if($the_month != $row['the_month']) 

in

 if(isset($the_month) && $the_month != $row['the_month']) 

Is there another way? This decision seems to me time.

+4
source share
4 answers

Define a variable before using it. This is the only way to make sure.

Many web hosts still have register_globals . This allows any visitor to enter variables into your script, adding material to the query string.

For example, if your script is called as example.php?the_month=5 , the variable $the_month automatically populated with 5. This can be very dangerous because someone might encounter important security related variables! For this reason, register_globals now deprecated.

But this does not change the fact that many web hosts are still enabled, so every PHP developer must define each variable before something is safe before using it. Otherwise, you have no guarantee that the variable will contain what you think.

+8
source

This answer may help as it looks like:

PHP: printing variables undefined without warning

For instance:

You may run it with the error suppression operator @.

echo @$variable;

However, it is best not to ignore undo variables. Fatal variables could indicate a logical error in the script, and it is best to ensure the variables are set before use.

+6
source

Always make sure $the_month set to something, even NULL .

+3
source

You should not carelessly access variables that may or may not be defined, it's simple. Just because PHP allows you to do this does not mean that it is a smart way to write code. Please refer to my extensive answer to a similar question: How to avoid isset () and empty ()

+1
source

All Articles