TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-18-2009, 06:22 AM   #1 (permalink)
The Visitor
 
Join Date: Nov 2009
Posts: 2
Thanks: 0
starterup is on a distinguished road
Default 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.
starterup is offline  
Reply With Quote
Old 11-18-2009, 07:14 AM   #2 (permalink)
The Acquainted
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 136
Thanks: 3
maeltar is on a distinguished road
Default

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)
__________________
Thanks... Simon

Sex, Drugs & Linux Rules

Last edited by maeltar : 11-18-2009 at 07:37 AM. Reason: Added examples
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
Old 11-18-2009, 07:55 AM   #3 (permalink)
The Acquainted
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 136
Thanks: 3
maeltar is on a distinguished road
Default

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.
__________________
Thanks... Simon

Sex, Drugs & Linux Rules
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
Old 11-18-2009, 08:08 PM   #4 (permalink)
The Wanderer
 
Join Date: Sep 2009
Posts: 9
Thanks: 1
afraca is on a distinguished road
Default

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";

?>

Last edited by afraca : 11-18-2009 at 08:08 PM. Reason: code tags
afraca is offline  
Reply With Quote
Old 11-18-2009, 09:52 PM   #5 (permalink)
The Acquainted
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 136
Thanks: 3
maeltar is on a distinguished road
Default

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

Sex, Drugs & Linux Rules
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
Old 11-18-2009, 11:11 PM   #6 (permalink)
The Visitor
 
Join Date: Nov 2009
Posts: 2
Thanks: 0
starterup is on a distinguished road
Default Thank you both

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

Thanks a lot.
starterup is offline  
Reply With Quote
Old 11-27-2009, 10:35 PM   #7 (permalink)
The Contributor
 
Izym's Avatar
 
Join Date: Sep 2007
Posts: 32
Thanks: 0
Izym is on a distinguished road
Default

To test out cases like that you would probably be better off using var_dump.
Izym is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Easy to Modify Login Script with Hierarchical User Permissions and XML Account File Wildhoney Script Giveaway 4 05-04-2011 06:11 AM
How easy is it to create RSS feeds? Brook Absolute Beginners 4 02-03-2008 07:10 PM


All times are GMT. The time now is 11:04 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design