Home arrow PHP arrow Page 3 - Debugging and Performance

Splitting Single Commands Across Multiple Lines - 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 great things about PHP is that it doesn't require you to write a single statement all on one line of code. In fact, any statement can be split across an arbitrary number of lines without any change in its functionality—provided, of course, that the split doesn't take place in the middle of a text string. This is particularly useful when you have a complex line of code that spans a large number of characters:

$db->query("select foo,
mybar as foobar
from tbl1
where tbl1.mybar='foo'");

This database query is split over several lines. The main advantage here is that you can immediately see what the query does, which tables are involved, and which conditions you are placing in the where clause. If the same query had been placed all on the same line, understanding its purpose would have taken a lot more effort, and the risk of introducing new bugs by modifying it would have been greater.

Concatenation Versus Substitution

If you are inserting data into a long string—such as a database query—you can use the concatenation operator, but doing so often limits your ability to read the query properly:

("insert into foo(id,bar) 

On the other hand, you could just use one of the printf() functions to do the job for you:

$db->query(sprintf("insert into foo(id,bar)
values('%s','%s')", addslashes($id), addslashes($bar) ));

As you can see, the entire statement is now a lot easier to read, although you will lose some performance by switching to sprintf() from the concatenation operator, which is native to the PHP interpreter and doesn't require the execution of any external libraries. The literals in the string passed to sprintf() will be substituted with the values of the parameters passed afterwards in the order in which they appear in the call. Combined with the ability to split your commands over several lines, this approach can enhance readability to a large degree.

Choose Your Opening Tags Carefully

Mixing PHP and HTML code is one of the characteristics of PHP that make it both easy to use and powerful, although it's easy to abuse this capability and come up with code that is difficult to read.

When writing code for an application that could run on heterogeneous systems, it's always a good idea to be very careful about which opening tag styles you use. In Chapter 1, "The Basics of PHP," we mentioned that there are several of them, but only the canonical tags <?php ?> are fully portable. Short tags (which include the echo tag <?=) and ASP tags can all be turned off through PHP configuration directives.

Thus, the following

<?php print "Testing 1 2 3" ?>

is longer than

<?= "Testing 1 2 3" ?>

But not quite as portable. Note, also, that there is a subtle difference between print and echo. Although they are both language constructs, the former acts as a function—meaning that it actually returns a value (always a Boolean True)—whereas the latter does not. Thus, the following code is valid, although quite pointless:

<?php echo print (10) ?> 

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