bumcheekcity.com Header Image

Speed Up Your PHP Code

1. Use an IP address to Access your mySQL Database

Simple, isn't it? Instead of this:

mysql_connect('example.com', 'username', 'password');

You use the following. This saves time spent looking up the hostname from the URL.

mysql_connect('', 'username', 'password');

2. Use mod_gzip to Compress HTML

 The VERY FIRST LINE of your PHP code (i.e. in your header page if you use one), you should put the following line.


If you're already using the object buffer (that's what "ob_" stands for, by the way), then don't have two calls to the ob_start() function, just add 'ob_gzhndler' as the first argument.

What does this do? It tells apache to compress the HTML that you send to the person browsing your website. HTML is very compressible, and this reduces your bandwidth costs, as well as massively speeds up the speed of your website, which will help no end.

If you're using apache, you'll need to enable the mod_gzip module, and add "zlib.output_compression = On" to your php.ini. If you're running a shared server, annoy your host until they do this.

2a. Use mod_gzip to Compress Javascript Files

Change the extension of your css file from file.css to file.php, and then simply change the HTML line to import or link the new file.php. This alone won't work though (probably), as your website will then be trying to look for CSS information in a PHP file, and will probably act as if you hadn't imported a CSS file at all. So, send a header command to the PHP page to make it fake itself as a CSS file:

header("Content-type: text/css");

And viola, you're done. This is especially good if you have a large CSS file for a very complicated website, which can be 10K+.

2b. Use mod_gzip to Compress Javascript Files

Same as before, really, you just need:

header("Content-type: text/css");

You're probably getting the hang of it by now.

3. mySQL Changes

Only ever use what you need. You should NEVER start with the good old SELECT * FROM tablename, ever. Just select what you need. Of course, if you DO need the whole table, the just use *.

Oh, and index your tables. Duh.

4. Watch out for Echoes

A rather simple change. The "echo" command is very expensive in PHP in terms of time. However, concatonating two strings is not. Instead of:

for ($i = 1; $i < 1000000; $1++)
echo $i.' ';


$echo = '';
for ($i = 1; $i < 1000000; $1++)
$echo .= $i.' ';
echo $echo;

Works a treat, speeding up your PHP a substantial amount if you have to print a lot of values off a database somewhere.

5. Use Single Quotes

In PHP, the string 'this is a $foo' is treated literally. If you were to echo it or otherwise use it, the '$foo' would be used as a string. However, if you had "this is a $bar" as your string, then, if you were to echo it to the screen, PHP would search through it, looking for variables. When it found variables, it would then look them up and print them to the screen. This takes time, and IS noticeable.

So, instead, use 'this is a '.$foobar as your string. PHP won't bother parsing the 'this is a ' bit at the start, and won't have to parse the string '$foobar' to know that $foobar is a variable.

This, in combination with 4. above, will really speed up long loops.

Copyright bumcheekcity.com © 2007-2017.
I am not responsible for any damage you cause to your computer, hardware, hands, other body parts or family members. By accessing this website you acknowledge that I own your soul, especially Starchimedes.