Tips For Making Your PHP Code More Portable - Databases and tables
(Page 3 of 4 )
Databases and tablesIf 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 SpecificsCreating 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 QuotesJust 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.
Next: Conclusion >>
More PHP Articles
More By Derek Comartin