doubledee Posted March 20, 2012 Share Posted March 20, 2012 Is there any reason why I would want to use this... $_SESSION['loggedIn'] = TRUE; versus this... $_SESSION['loggedIn'] = 1; (The first one is easier to read...) Debbie Quote Link to comment https://forums.phpfreaks.com/topic/259355-true-vs-1/ Share on other sites More sharing options...
requinix Posted March 20, 2012 Share Posted March 20, 2012 And it's more correct too. Even though PHP is loosely-typed, being specific helps. I mean for all I know, if loggedIn=1 then maybe loggedIn=2 could mean something else. Normal user versus administrator? Quote Link to comment https://forums.phpfreaks.com/topic/259355-true-vs-1/#findComment-1329541 Share on other sites More sharing options...
AyKay47 Posted March 20, 2012 Share Posted March 20, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/259355-true-vs-1/#findComment-1329557 Share on other sites More sharing options...
scootstah Posted March 20, 2012 Share Posted March 20, 2012 There's no conceivable reason to use 1 or 0 if you actually want a boolean. Since PHP is loosely typed they may appear to do the same thing, but they are not the same thing. Quote Link to comment https://forums.phpfreaks.com/topic/259355-true-vs-1/#findComment-1329558 Share on other sites More sharing options...
KevinM1 Posted March 20, 2012 Share Posted March 20, 2012 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). Quote Link to comment https://forums.phpfreaks.com/topic/259355-true-vs-1/#findComment-1329560 Share on other sites More sharing options...
doubledee Posted March 20, 2012 Author Share Posted March 20, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/259355-true-vs-1/#findComment-1329568 Share on other sites More sharing options...
scootstah Posted March 20, 2012 Share Posted March 20, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/259355-true-vs-1/#findComment-1329571 Share on other sites More sharing options...
doubledee Posted March 20, 2012 Author Share Posted March 20, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/259355-true-vs-1/#findComment-1329574 Share on other sites More sharing options...
KevinM1 Posted March 20, 2012 Share Posted March 20, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/259355-true-vs-1/#findComment-1329581 Share on other sites More sharing options...
doubledee Posted March 20, 2012 Author Share Posted March 20, 2012 Yup. TRUE can't be anything else other than, well, TRUE. If you're aiming for clarity, it's a good way to go. Okay, thanks for the help everyone! Debbie Quote Link to comment https://forums.phpfreaks.com/topic/259355-true-vs-1/#findComment-1329590 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.