Hello. If i say something like

if (thing == 5){

};

And "thing" is not defined, i will get an error and my whole program will crash.

But say Its only not defined because it hasn't been defined yet. I can do a workaround like this:

if (thing){

if (thing == 5){

};

};

Then that code is only run if "thing" has been defined. Which is what I want.

But if im dealing with a boolean, such as

if (thing){

thing = true;

};

Its now no longer going to check if the variable exists, now its just going to check if its true. Therefore, if its already false, then the variable wont be converted to true! And if its not defined, it also wont get defined! This is NOT what i want. I already have the tools necessary to check to see if something is true or false, so why do these existence checks turn into boolean checks? Its useless and just injects bugs.

Is there any good way around this? 1s and 0s dont work because it reads it the same way, which is superbly annoying when i have number variables that are sometimes 0 or 1. [Mod: Spurious irrelevant link removed.] Am i forced to do something like use boolean-esque strings ('true' and 'false') in order to get the desired functionality? Can someone explain more into why it does this?

Not sure what your question is (what is the "desired functionality"?). Your second code sketch will also run if "thing" is not defined — it'll just complain that it's not defined twice. If you want to know if a variable is defined or not (assuming "thing" is supposed to be a PHP variable) you should (a) write your code so that you can tell by looking at it, or if you can't do that, (b) use isset to see if the variable is defined.

In short, your existence checks are turning into boolean checks because you're turning them into boolean checks. Stop doing that.

Since the post was infected by spam, I'm going to guess there won't be a coherent reply.

    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.

    sneakyimp $my_var = isset($_GET['my_var_']) ? $_GET['my_var_'] : NULL;

    Starting with PHP 7 you can write this as $my_var = $_GET['my_var_'] ?? NULL;.

    Write a Reply...