Home arrow PHP arrow Page 5 - Debugging and Performance

Testing for Resource Allocation - 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



One of the most common mistakes that causes code to become unreliable consists of using external resources without ensuring that they are available. For example, look at the following code:

$res = mysql_query("select foo from bar");
while ($row = mysql_fetch_array($res)) 
print $row['foo']."<br>";

See what's wrong? The author doesn't test for the query's failure before moving on to perform other tasks that use the resource returned by mysql_query(). The query could fail for a number of reasons, even though it is syntactically correct—for example, the server might be unavailable, or there could be a network interruption. What's worse in this particular case, the MySQL extension does not cause a fatal error if a query cannot be executed. Therefore, the script moves on, and a cascade of additional problems could be caused by this initial blunder.

If, on the other end, error conditions are properly tested for, this issue doesn't even present itself:

if (!$res = mysql_query("select foo from bar")) 
* no valid result, log/print error,
mysql_error() will tell you */ } else { while ($row = mysql_fetch_array($res)) { print $row['foo']."<br>"; } }

It's undoubtedly hard to write an if statement every time you execute a query—but also necessary if you are serious about error management. To make things a bit easier on yourself (and your entire team), you could adopt one of the many abstraction layers available or write one yourself. This way, the actual error management can be performed in a centralized location (the abstraction layer), and you won't have to write too much code.

It's important to keep in mind that this process is required whenever you interact with an external resource, be it a database, a file, or a network connection.

Starting with PHP 5, you can use other error-control structures known as exceptions. However, remember that these are not available in PHP 4 and, therefore, cannot be used to solve a problem that appears in the exam.

Ternary Operators and if Statements

if statements are necessary control structures for all but the simplest of PHP scripts. As a result, sometimes they will tend to be very complex, even if you nest them on various levels.

Luckily, the ternary conditional operator that you saw in Chapter 1 can be used to simplify the use of if statements by embedding them directly in a larger expression. For example, consider the following snippet of code:

function is_my_country($country) 
if (strlen($country) == 3) 
return 1;
return 0;

It could also be written as

function is_my_country($country) {
return (strlen($country)==3) ? 1 : 0;

As you can see, the function is much shorter than the if statement in the preceding example. This can be very valuable if you're dealing with a complex piece of code such as the following:

$db->query(sprintf("insert into foo(f1,f2,f3)
values('%s','%s','%s')", (isset($_SESSION['foobar'])) ? 'yes' : 'no', (isset($_POST['myfoo']) && $_POST['myfoo']!='')
? $_POST['myfoo'] : 'no', 'foo'));

A call such as the preceding one would have been a lot more complex if it had been written using traditional if statements—not to mention that you would have needed either a number of new variables to hold the information, or a different set of function calls for each possible scenario.

Please check back next week for the conclusion of this article.

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