TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Comparison Operators - super easy (http://www.talkphp.com/absolute-beginners/5117-comparison-operators-super-easy.html)

starterup 11-18-2009 07:22 AM

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:

<?php
$one = 1;
$two = 2;

$less = ($one < $two);
echo $less;
?>

I get the result of "1" instead of "true", and for "false" it doesn't show anything; just a blank.

My 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.

maeltar 11-18-2009 08:14 AM

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:

<?php
$a = 10;
$b = 20;

// Check $a less than $b
$less = ($a < $b);

// If $a is not equal to 1 (TRUE), show a message
if ($less != 1)
        {
        echo "NULL\n";
        // we need to exit to program
        exit();
        }

echo $less . "  TRUE\n";

?>

swapping the numbers round would normally produce nothing for the "echo" statement to write so for an example you could use another boolean operative to display a message if the answer is NULL..

Code:


<?php
$a = 20;
$b = 10;

// Check $a less than $b
$less = ($a < $b);

// If $a is not equal to 1 (TRUE), show a message
if ($less != 1)
        {
        echo "NULL\n";
        // we need to exit to program
        exit();
        }

echo $less . "  TRUE\n";

?>

This snippet would echo "NULL" as the statement
Code:

if ($less != 1)
is true, as $a "is not equal to" 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)

maeltar 11-18-2009 08:55 AM

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:

<?php

// Set vars
$a = 10;
$b = 20;

// Check if $a is less than $b
if ($a < $b)
        {
        // yes it is
        echo "TRUE\n";
        }
        else
        {
        // no it isn't
        echo "FALSE  (or NULL)\n";
        }

?>


Always always always, comment your code, makes it much easier to understand, and is a good habbit to get into right from the start.

afraca 11-18-2009 09:08 PM

Or you can use ternary operators, like this (but for a beginner might be complicated):

Code:

<?php

$var1 = 10;
$var2 = 20;

echo ($var1 >= $var2) ? "var1 is greater or equal to var2" : "var1 is smaller then var2";

?>


maeltar 11-18-2009 10:52 PM

Was trying to keep to the TRUE/FALSE for him though, and a bit more "readable"

starterup 11-19-2009 12:11 AM

Thank you both
 
It's good to know that true always appears as 1

Thanks a lot.

Izym 11-27-2009 11:35 PM

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