[Answered]Question Regarding Use of True/False Values

Greetings, I’m new to this community and relatively new to PHP and I have a question about the possible use for true/false values. I’m working on a login script for my site and I want to display errors if the information entered has a problem.

I’m planning on using the following code that acts if no errors are present: if(!$ID_check->error && !$password_check->error){ $user = new login($ID_check->check_this, $password_check->check_this); $user->login_user(); }
This basically says that if $ID_check->error and $password_check->error are set to false then log the user in. I am fairly confident that the usage of this is proper.
This follows it up://Example 1) if($ID_check->error or $password_check->error){ $error = true; }
I’m assuming that if $ID_check->error or $password_check->error are set to true it will then set the $error value to true as well. This usage however is where my question comes in. Can the code remain this way or would it be better to replace is with: //Example 2) if($ID_check->error == true or $password_check->error == true){ $error = true; }

So is it better to use Example 1 or Example 2 or is there a better way ? I have used Example 1 before and it seems to work fine, but I don’t know if the if() statement is just reacting to whether or not the value is set or what exactly is going on.


Considering your values are ‘always’ initialized, you don’t really have to worry about that. To answer your question in a more general sense: uninitialized variables render to ‘false’ in if statements (which would make sense). In PHP, using if ($somevar) { would be considered equal to if ($somevar == true) {

There’s a catch though ;) Some statements result to ‘false’ in a different sense, for example, value 0 (zero), or “” (empty string), or NULL. In order to use them in if statements and have them validate correctly, it’s best to use if ($somevar === true) { (that’s not a typo, it’s three ===).

However, I’m presuming you’re defaulting your variables to false and then set them conditionally to true. You make sure your conditional statement in your if statement results to a boolean value no matter what and that makes the use of if ($somevar) { very allowed (I would even say encouraged, but I’m afraid it’s a matter of preference).


Thanks for the answer, it makes me feel better about my code as I have been using my first example and was questioning it’s use. I do set variables which will be either true or false to false by default. And I also make sure to only use true or false instead of 1 or 0, I know it’s important to keep the data types the same. Thanks also for pointing out to use === (3x =) for that sort of statement. I knew that using == (2x =) would match the value and = (1x =) would set the variable’s content. I’m guessing that the === (3x =) is used to match value and type (integer, float, string, boolean)?

Thanks again!

Sponsor our Newsletter | Privacy Policy | Terms of Service