![]() |
Comparison Operators - super easy
Hello Everyone,
I have just started learning php as my first programming language yesterday. I am following a tutorial at devzone.zend.com and just covered basic comparisons. When I enter this code Code:
<?phpMy question: is this normal? Every tutorial I've looked at and even the php5 manual says it should result as a Boolean value. Am I doing something wrong? I am running xampp on windows vista x86. My other code on the page (simple addition) works fine. Thank you in advance for your help. |
Hello and welcome, the answer is easier than you think...
1 = True (NULL) = False Try changing the numbers you will see you still get 1 or NULL Which means the variable $less will be 1 or nothing at all Hope that clears it up for you Examples... Code:
<?phpCode:
Code:
if ($less != 1)P.S. I have changed $one to $a and $two to $b I don't believe it's a codeing practice thing, but it's the way I was taught, not to use written numbers as variables as it makes the code harder to read in a way, if you use a similar format to algebra type equations you automatically (sic) can read it easier. (I know I can) |
I may be getting ahead fo you slighty here but the above code would be considered "dirty" as it would interupt code flow..
The following code does exactly the same tests on vars $a and $b then echo's the message to screen.. Code:
<?phpAlways always always, comment your code, makes it much easier to understand, and is a good habbit to get into right from the start. |
Or you can use ternary operators, like this (but for a beginner might be complicated):
Code:
<?php |
Was trying to keep to the TRUE/FALSE for him though, and a bit more "readable"
|
Thank you both
It's good to know that true always appears as 1
Thanks a lot. |
To test out cases like that you would probably be better off using var_dump.
|
| All times are GMT. The time now is 07:35 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0