Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined variable php notice workaround?

Tags:

php

I just turned on notices because 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.

like image 986
willdanceforfun Avatar asked Nov 03 '10 04:11

willdanceforfun


1 Answers

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.

like image 157
kijin Avatar answered Oct 05 '22 22:10

kijin