![]() |
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:
PHP Code:
$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() statement but if you accidently type a single equals sign instead of two equals signs:PHP Code:
Code:
Parse error: syntax error, unexpected '=' in ...Alan |
I've wondered whats the different between using this condition or a
strcmp |
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 |
Quote:
|
I always use three equal signs, which is a lot faster. And when I do a typo I still have two equal signs ;).
|
Thanks for that Alan :)
|
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"; } |
PHP Code:
|
i also tried that one but still get the echo correct even if the pwd is not equal to variable b
|
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 |
Post your entire script then. ;-)
PHP Code:
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