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 03-01-2008, 11:04 PM   #1 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Bug Tip when comparing variables

Hi all,

Here is a little tip that I wish someone had told me when I started out with PHP

As I'm sure you all know, when comparing two values you would commonly use something like:

PHP Code:
if ($myVar == 'hello')
{
    
// ...

However, if you are anything like me, you will occasionally type a single equals sign instead of double:

PHP Code:
if ($myVar 'hello')
{
    
// ...

Which introduces an anoying little hard-to-find bug in your code as instead of comparing the 2 values, it is actually setting $myVar to 'hello'!

Unfortunately, PHP sees this as perfectly valid so you don't get any error messages and aren't aware of any problems until it causes a bug in your application.

However, there is a little trick you can use to prevent this problem. When comparing values, use:

PHP Code:
if ('hello' == $myVar)
{
    
// ...

As you can see, we have reversed the variables. This is still a perfectly valid if() statement but if you accidently type a single equals sign instead of two equals signs:

PHP Code:
if ('hello' $myVar)
{
    
// ...

PHP will throw you a nice error letting you know of the problem:

Code:
Parse error:  syntax error, unexpected '=' in ...
Once you get in to the habbit of writing your comparisons this way it will save you countless hours of debugging anoying little bugs (well, it has for me anyway )

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
The Following 3 Users Say Thank You to Alan @ CIT For This Useful Post:
Orc (03-01-2008), Shauny_B (03-02-2008), wiifanatic (03-09-2008)
Old 03-01-2008, 11:37 PM   #2 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

I've wondered whats the different between using this condition or a strcmp
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 03-01-2008, 11:42 PM   #3 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

No real difference at all. Using strcmp would be slightly slower and doesn't work for numbers but other than that it works fine

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 03-01-2008, 11:43 PM   #4 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Alan @ CIT View Post
No real difference at all. Using strcmp would be slightly slower and doesn't work for numbers but other than that it works fine

Alan
I'd just go with the double equal sign operator.
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 03-02-2008, 12:15 PM   #5 (permalink)
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
sjaq is on a distinguished road
Default

I always use three equal signs, which is a lot faster. And when I do a typo I still have two equal signs ;).
sjaq is offline  
Reply With Quote
Old 03-02-2008, 01:05 PM   #6 (permalink)
The Wanderer
 
Join Date: Mar 2008
Posts: 6
Thanks: 4
Shauny_B is on a distinguished road
Default

Thanks for that Alan :)
Shauny_B is offline  
Reply With Quote
Old 03-03-2008, 01:35 PM   #7 (permalink)
The Visitor
 
Join Date: Oct 2007
Posts: 3
Thanks: 0
vungom is on a distinguished road
Default

i created 2 variables to compare but i think php only compares the first variable
something like this
if($a=='user'&& $b=='pwd')
{
echo "correct";
}
vungom is offline  
Reply With Quote
Old 03-03-2008, 01:46 PM   #8 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

PHP Code:
if ( ( $a == 'user' ) && ( $b == 'pwd' ) ) {
echo 
'correct';

That is what it should be. The && can be referred to as "AND EQUAL" and || can be referred to as "OR". Same goes for the and - and or.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 03-03-2008, 01:56 PM   #9 (permalink)
The Visitor
 
Join Date: Oct 2007
Posts: 3
Thanks: 0
vungom is on a distinguished road
Default

i also tried that one but still get the echo correct even if the pwd is not equal to variable b
vungom is offline  
Reply With Quote
Old 03-03-2008, 01:58 PM   #10 (permalink)
The Visitor
 
Join Date: Oct 2007
Posts: 3
Thanks: 0
vungom is on a distinguished road
Default

btw i am comparing variable to variable from a text file something like this

if(($user==$username) && ($pwd==$pass) )
{
echo "correct"; }

thanks for the quick reply
vungom is offline  
Reply With Quote
Old 03-03-2008, 04:08 PM   #11 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Post your entire script then.
PHP Code:
<?php
    
    $username 
'TalkPHP';
    
$password 'Wildhoney';
    
    
$usd 'TalkPHP';
    
$pwd 'Alan';
    
    if ( (
$username == $usd) && ($password == $pwd) ) {
        echo 
'Both values appear to be equal, so I return it true.';
    } elseif ( (
$username == $usd) || ($password == $pwd) ) {
        echo 
'Either username or password are not equal, but I return it true.';
    } elseif ( !(
$username == $usd) && !($password == $pwd) ) {
        echo 
'I indicate that both are wrong, so I return it false.';
    }
Play around with the values a bit until you get the result you want. Otherwise, you should echo out all 4 variables with, for example, var_dump();. I hope this helps. Kinda finishing up Alan's topic here I guess.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN 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


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

 
     

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