PHP
  Home arrow PHP arrow Page 3 - Tips For Making Your PHP Code More Portabl...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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? 
PHP

Tips For Making Your PHP Code More Portable
By: Derek Comartin
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 7
    2002-02-02

    Table of Contents:
  • Tips For Making Your PHP Code More Portable
  • PHP Tags
  • Databases and tables
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


    Tips For Making Your PHP Code More Portable - Databases and tables


    (Page 3 of 4 )

    Databases and tables

    If you use PHP to manipulate a database, then never hard code the names of the databases or tables in a query. Provide the option for someone to change the name of a database or table in a configuration script. For example, if I download John Doe's eCommerce script and the installation file said to create a table named 'users' in a MySQL database and I already had a table called users, then all I would have to do is call it customers, and then go back into the configuration file and change the necessary field.

    Here's an example of a configuration file:

    // Database Table Names

    $conf['tbl']['users'] = 'customers';

    $conf['tbl']['basket'] = 'shopping_basket';


    So now when we're querying a database, we can use the table names defined in our configuration file to work with and manipulate data:

    $query = 'UPDATE '.$conf['tbl']['users'].' SET FOO=\'BAR\'';

    $query = 'UPDATE '.$conf['tbl']['basket'].' SET FOO=\'BAR\'';


    Version Specifics

    Creating an application that works with both PHP versions three and four isn't too difficult at all. Here are some tips to make code compatibility easier:
    • If you use a function that is only available in PHP4, than create a file and recreate/mimic each PHP4 function you use. Then, include that file when you notice that PHP3 is running.
    • When using the GPC $HTTP_*_VARS arrays, some elements don't exists in PHP3. It might be a good idea to re-create these missing elements. (note that there are a lot more elements than those shown in the example below):

      if (substr(phpversion(), 0, 1) == 3) {

      INCLUDE('my_php3_functions.php');

      $HTTP_SERVER_VARS['PHP_SELF'] = $PHP_SELF;

      $HTTP_SERVER_VARS['QUERY_STRING'] = $QUERY_STRING;

      }
    • PHP3 doesn't support creating a property of an object that is actually an object itself. Here is an example that will compile in PHP4, but not in PHP3:

      $obj->property->method(); // Wont work in PHP3
    Parsing Quotes

    Just because you were testing your application on a dual AMD Athlon XP development server with over a gig of memory, it doesn't mean that the computers of the people buying or downloading your script are also using these exact same system specifications. We all know that every little bit of performance increase helps, and the easiest thing that you can do to get more performance out of your web server is to use single quotes instead of double quotes throughout your PHP scripts. When PHP is parsing your files, it has to do a little extra work when dealing with double quotes instead of single quotes, thus decreasing system performance.

    So, if we had this code:

    $name = "Derek";

    echo "My name is $name"; // outputs: My name is Derek


    ... and we replaced it with this code:

    $name = 'Derek';

    echo 'My name is '.$name; // outputs: My name is Derek


    ... then we would notice a slight performance increase. The performance increase may not be noticeable in small scripts, but if you have 10,000+ lines of PHP script, then you'll notice a definite speed increase in the time that it takes PHP to parse and serve your scripts.

    More PHP Articles
    More By Derek Comartin


     

    PHP ARTICLES

    - Making Usage Statistics in PHP
    - Installing PHP under Windows: Further Config...
    - File Version Management in PHP
    - Statistical View of Data in a Clustered Bar ...
    - Creating a Multi-File Upload Script in PHP
    - Executing Microsoft SQL Server Stored Proced...
    - Code 10x More Efficiently Using Data Access ...
    - A Few Tips for Speeding Up PHP Code
    - The Modular Web Page
    - Quick E-Commerce with PHP and PayPal
    - Regression Testing With JMeter
    - Building an Iterator with PHP
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP







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