AP81 Posted June 20, 2008 Share Posted June 20, 2008 Hi, Take a look at the following code below: <?php $test = true; if ($test == true) { echo "true"; } else { echo "false"; } ?> Now that looks fine to me. I was reading a book which suggests to always put the constant on the left hand side of an equality/inequality comparison. So the above would become: <?php $test = true; if (true == $test) { echo "true"; } else { echo "false"; } ?> The reason is that if you leave out one of the = signs, the parser will find the error for you. So this script still runs: <?php $test = true; if ($test = true) { // removed one equals sign echo "true"; } else { echo "false"; } ?> And the following fails: <?php $test = true; if (true = $test) { // removed one equals sign echo "true"; } else { echo "false"; } ?> So I can see some merit in this, but it just doesn't look right to me. Most people I know would put the constant on the right hand side, as it seems more logical. What are your thoughts on this? Quote Link to comment https://forums.phpfreaks.com/topic/111038-equalityinequality-testing/ Share on other sites More sharing options...
DarkWater Posted June 20, 2008 Share Posted June 20, 2008 I've read that too in a Java book I think, from ages ago, but I never really do that. I just follow the logic and see where it falls through. Quote Link to comment https://forums.phpfreaks.com/topic/111038-equalityinequality-testing/#findComment-569798 Share on other sites More sharing options...
bluejay002 Posted June 20, 2008 Share Posted June 20, 2008 IMO, I do not think that's necessary. instead, you can use: <?php $test = true; if ($test) { echo "true"; } else { echo "false"; } ?> its neater and parsed perfectly, why? because conditional statements eats boolean... equating a boolean with another boolean is unnecessary. Quote Link to comment https://forums.phpfreaks.com/topic/111038-equalityinequality-testing/#findComment-569800 Share on other sites More sharing options...
whizard Posted June 20, 2008 Share Posted June 20, 2008 Just get in the habit of using double equals signs. When I was still a complete noobie, I had a giant if/elseif statement with about 30 cases (not knowing about switch statements *rollseyes*) and they all had a single equals sign, so the last or first condition or something like that was always selected no matter what I gave the code. Ever since then, I've never forgotten an equals sign on a comparison test. Dan Quote Link to comment https://forums.phpfreaks.com/topic/111038-equalityinequality-testing/#findComment-569802 Share on other sites More sharing options...
AP81 Posted June 20, 2008 Author Share Posted June 20, 2008 Yep, I would do that using boolean values. But if they were strings it would be different. Quote Link to comment https://forums.phpfreaks.com/topic/111038-equalityinequality-testing/#findComment-569807 Share on other sites More sharing options...
DarkWater Posted June 20, 2008 Share Posted June 20, 2008 IMO, I do not think that's necessary. instead, you can use: <?php $test = true; if ($test) { echo "true"; } else { echo "false"; } ?> its neater and parsed perfectly, why? because conditional statements eats boolean... equating a boolean with another boolean is unnecessary. That's not what he was getting at. For example: You needed $foo to be equal to 2 to do something. if ($foo == 2) { } else { } Good. Now what if you accidentally did: if ($foo = 2) { } else { } The IF would always execute because you're setting $foo equal to 2. This may not look incorrect among hundreds of lines of code. But, if you wrote your conditionals as: if (2 = $foo) { } else{ } You can't assign anything to a constant, so it'll error out and you'll know where you went wrong. Quote Link to comment https://forums.phpfreaks.com/topic/111038-equalityinequality-testing/#findComment-569808 Share on other sites More sharing options...
bluejay002 Posted June 20, 2008 Share Posted June 20, 2008 ahaha... i though it was, its because you use boolean on all examples. but for case like that (other than boolean)... use "==" and put constants on the left, its a good practice. Quote Link to comment https://forums.phpfreaks.com/topic/111038-equalityinequality-testing/#findComment-569811 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.