emmawade
If you try and check variables that don't exist in pretty much any language, you should get some kind of error. In C, your program won't even compile. Even Javascript barfs:
alert(thisIsNotAVar);
Go ahead and try it yourself and you won't get an alert, but you will get an error in the console:
ReferenceError: thisIsNotAVar is not defined
In your code, you have choices. You can make sure your code defines any variables before you check them, and then you can just go ahead and check the variable without a care in the world. If the variables are common globals like $db
or something, you probably want a fatal error if it's not defined. You can define variables like that in some centralized file named db.php and then make sure you include it in every other script you run before you start referring to the $db
variable.
If you are in a situation where you are dealing with user input, like $_GET
or $_POST
values that may or may not be defined. you can do something like this:
$my_var = isset($_GET['my_var_']) ? $_GET['my_var_'] : NULL;
It's quite common to see that bit of code enshrined in a function in various frameworks. Codegniter, for example, offers some methods attached to their request class. If the input key you are looking for has not been defined, it returns NULL.
You should also realize there's a distinction between a boolean var that has been explicitly set and one that has never been defined in the first place.