PHP
  Home arrow PHP arrow Page 3 - Debugging and Performance
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Debugging and Performance
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 9
    2006-11-22


    Table of Contents:
  • Debugging and Performance
  • Flattening if Statements
  • Splitting Single Commands Across Multiple Lines
  • One Equal, Two Equals, Three Equals
  • Testing for Resource Allocation

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Debugging and Performance - Splitting Single Commands Across Multiple Lines
    ( Page 3 of 5 )

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

    $db->query
    ("insert into foo(id,bar) 
    values('".addslashes($id).
    "','".addslashes($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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek