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
 
 
LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
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)
 



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:12 PM.

 
     

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