TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Tips & Tricks (http://www.talkphp.com/tips-tricks/)
-   -   Tip when comparing variables (http://www.talkphp.com/tips-tricks/2392-tip-when-comparing-variables.html)

Alan @ CIT 03-01-2008 11:04 PM

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 :-D)

Alan

Orc 03-01-2008 11:37 PM

I've wondered whats the different between using this condition or a strcmp

Alan @ CIT 03-01-2008 11:42 PM

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

Orc 03-01-2008 11:43 PM

Quote:

Originally Posted by Alan @ CIT (Post 11798)
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.

sjaq 03-02-2008 12:15 PM

I always use three equal signs, which is a lot faster. And when I do a typo I still have two equal signs ;).

Shauny_B 03-02-2008 01:05 PM

Thanks for that Alan :)

vungom 03-03-2008 01:35 PM

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

ReSpawN 03-03-2008 01:46 PM

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.

vungom 03-03-2008 01:56 PM

i also tried that one but still get the echo correct even if the pwd is not equal to variable b

vungom 03-03-2008 01:58 PM

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

ReSpawN 03-03-2008 04:08 PM

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. :-P


All times are GMT. The time now is 05:15 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0