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.
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\'';
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):
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
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.