Home arrow PHP arrow Page 3 - Tips For Making Your PHP Code More Portable

Tips For Making Your PHP Code More Portable

One of the big causes of errors in PHP scripts is the lack of a portable code standard. In this article, Derek takes a look at a few little things that we can do to help make our PHP scripts more portable. When other developers comes to use them, they will have more of a chance of working successfully the first time, without the need to perform any kind of system configuration modifications, etc.

Author Info:
By: Derek Comartin
Rating: 3 stars3 stars3 stars3 stars3 stars / 7
February 02, 2002
  1. · Tips For Making Your PHP Code More Portable
  2. · PHP Tags
  3. · Databases and tables
  4. · Conclusion

print this article

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) {




  • 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.
blog comments powered by Disqus

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- 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

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials