Jump to content

TRUE vs 1


doubledee

Recommended Posts

Typically bool values should be used simply for logical reasons as Requinix mentioned.

Integers should only be used when you actually need an integer value to do something.

Also, if you assign an integer type 1 or 0 for TRUE or FALSE, you have to be careful of which comparison operator you use should you have to compare the value to another.

Adds an extra layer that could potentially cause silly errors that can be avoided.

 

Link to comment
Share on other sites

And, the reason why they're equivalent is because PHP (and other languages) treat 0 as FALSE and any non-negative integer as TRUE.  In terms of boolean logic (meaning, binary logic based on true/false), something like:

 

$_SESSION['blah'] = 1;

if ($_SESSION['blah']) { // the session value is true
   echo "true";
}

 

is fundamentally the same as:

 

$_SESSION['blah'] = 2012;

if ($_SESSION['blah']) { // the session value is true
   echo "true";
}

 

Both will echo 'true'.

 

Now, this isn't the case for all languages.  C#, for instance, is very strongly typed, so it can't convert from a number to a boolean, and it will throw an error if attempted.  The same may be true for Java (not sure).

Link to comment
Share on other sites

See, I thought the opposite of what everyone is saying...

 

I thought that using an Integer was more absolute because PHP *is* a loosely typed language.

 

Somewhere someone got me scared that boolean TRUE is not the same as string 'TRUE' so I started using Integers instead.

 

Guess I need to switch back to the other way?!

 

 

Debbie

 

Link to comment
Share on other sites

That's correct, boolean TRUE is not the same as the string "TRUE". One is a boolean, one is a string.

 

Boolean is not the same as an integer either.

 

So it sounds like this is the best way to do things...

 

$_SESSION['loggedIn'] = TRUE;

 

 

And as long as I don't quote anything, then it is the best way to convey what I mean, right?

 

 

Debbie

 

 

Link to comment
Share on other sites

That's correct, boolean TRUE is not the same as the string "TRUE". One is a boolean, one is a string.

 

Boolean is not the same as an integer either.

 

So it sounds like this is the best way to do things...

 

$_SESSION['loggedIn'] = TRUE;

 

 

And as long as I don't quote anything, then it is the best way to convey what I mean, right?

 

 

Debbie

 

 

 

Yup.  TRUE can't be anything else other than, well, TRUE.  If you're aiming for clarity, it's a good way to go.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.