Home arrow PHP arrow Page 4 - Debugging and Performance

One Equal, Two Equals, Three Equals - PHP

Debugging is an important part of coding. One way to make the debugging process easier is to write quality code to begin with. This article, the first of two parts, will point out some of the most common coding errors, and help you identify problems in your code. It is excerpted from chapter 12 of Zend PHP Certification, written by George Schlossnagle et al (Sams; ISBN: 0672327090).

  1. Debugging and Performance
  2. Flattening if Statements
  3. Splitting Single Commands Across Multiple Lines
  4. One Equal, Two Equals, Three Equals
  5. Testing for Resource Allocation
By: Sams Publishing
Rating: starstarstarstarstar / 9
November 22, 2006

print this article



How often did you write the following code?

if ($a = 5) 
print "a is 5";

If you're like most programmers, the answer is an unfortunate "often." The problem here is caused by the fact that the if statement allows for any operations to take place inside its condition—including assignments. Thus, the preceding line is not technically incorrect, but it's obviously not what the author intended to perform, as it will always be evaluated to true, making the if statement pointless and, what's worse, changing the value of $a.

Clearly, the statement should have been written as follows:

if ($a == 5
print "a is 5";

In this case, the condition is a comparison operator, and it will be evaluated as true only if the value of $a is 5.

There is, luckily, a very easy way to avoid this mistake once and for all, without any possibility of ever slipping again: make sure that the condition is written in such a way that it cannot possibly be misinterpreted:

if (5 == $a) 
print "a is 5";

With this approach, if you mistakenly only use one equal sign instead of two, as in 5 = $a, the interpreter will print out an error because you can't assign anything to an immediate value. If you make a habit of writing all your conditions this way, you will never fall in the assignment trap again!

There's Equal and Equal

As we mentioned in Chapter 1, PHP is a loosely typed language. This means that, under the right circumstances, it will automatically juggle data types to perform its operations according to how programmers are most likely to want it to.

There are scenarios, however, in which this is not a desirable approach, and you want, instead, PHP to be strict and literal in the way it compares data. Consider, for example, what would happen if you were dealing with information coming from a patient's medical record. In this situation, you'll want to make sure that nothing is left to chance and that PHP doesn't attempt to interpret user input in too liberal a way.

Generally speaking, it's always a good idea to use the identity operators (=== and !==) whenever you know that a value has to be of a certain type:

if ($a !== 0) {
echo '$a is not an integer zero';

>>> More PHP Articles          >>> More By Sams Publishing

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: