PHP Developer / Blog

February
28th, 2008

10 tips to make your PHP application scream - Part 1 of N

Digg this article · Save to del.icio.us · Stumble it!

I’ll be the first to state that there is no shortcut to making your PHP applications scream. This series aims to break it down into several steps. The first step will be taking a look at the PHP configuration on the server. We’ll be focusing specifically on PHP this article as a later article will cover 3rd party software which are an essential part of scaling your applications. Comments and suggestions welcome!

Also read part 2 of this article.

PHP.ini, getting your hands dirty
Almost all of the PHP configuration options are available in your php.ini file. PHP ships with several defaults which are suited for compatibility and not for performance. Let’s change that! It’s important to understand your application and what you can and cannot change. In some cases you might want to have allow_url_fopen = On even though I would generally recommend against it. Keep that in mind when reading this list.

short_open_tag = Off
memory_limit = 128M
register_long_arrays = Off
register_argc_argv = Off
auto_globals_jit = On
magic_quotes_gpc = Off
always_populate_raw_post_data = Off
include_path = "."
variables_order = "EGPC"
url_rewriter.tags = ""

short_open_tag = Off
Turning off short tags means that <? doesn’t denote an opening PHP block. You must use <?php instead. An additional benefit is that you might save yourself a headache here or there with XML files which open with the <? tag.

memory_limit = 128M
Adjusting memory_limit can go a long way. You need to keep an eye on your application’s memory usage so you can adjust this accordingly taking advantage of every ounce of memory you have.

register_long_array = Off
Long arrays are the old school $HTTP_*_VARS variables we came to love in PHP 3. The only reason I can think of leaving this on is for backwards compatibility. Setting register_long_arrays = Off saves the PHP engine some cpu cycles that it can use elsewhere.

register_argc_argv = Off
register_argc_argv has the same implications as register_long_arrays above so it should be set to Off as well.

auto_globals_jit = On
PHP can save memory by creating server and environment variables only if and when they’re used. Enable this by setting auto_globals_jit = On. This only goes into effect if register_globals = Off, register_long_arrays = Off and register_argv_argc = Off.

magic_quotes_gpc
Magic quotes are the work of Satan and should be turned off not only to save PHP from unnecessary work but also because it should have never been turned on in the first place.

always_populate_raw_post_data = Off
If you don’t ever need to access raw post data or have never heard of it then set always_populate_raw_post_data = Off.

include_path = “.”
File system look-ups are expensive. If you include a PEAR file then by default PHP first checks the directory it’s executing out of. When it doesn’t find the file there it checks the second location in the include path. I’ve seen some long include paths and while they have benefits a better approach might be to set configuration variables and use fully qualified paths for all file inclusions. Set include_path = “.”.

variables_order = “EGPC”
If you don’t use PHP sessions because you roll your own then you can remove the S from variables_order. This will save PHP from registering non-existent session variables. You can remove additional letters if you don’t use their corresponding variables.

url_rewriter.tags = “”
To be honest, I didn’t even know what URL rewriter tags were. But upon reading it I know that it’s not going to help PHP process requests any faster. I’m sure it has its uses but let’s go ahead and set url_rewriter.tags = “” unless there’s a way to disable it altogether.

The Zend 2 engine
An easy step you can take is to make sure you’re running the Zend 2 engine. This was introduced with PHP 5 and adds a robust object model as well as performance enhancements. The robust object model let’s you take advantage of programming principals previously unavailable to PHP developers. We’ll talk more about that in a later article.

Many circumstances require a prior version of PHP and that’s perfectly fine. There are so many tips we’ll be covering that omitting a few won’t be such a big deal.

Conclusion
These are just a few tips I’ve used over time. As always, post your suggestions or additions in the comments.

One Response to “10 tips to make your PHP application scream - Part 1 of N”

  1. Amir Says:

    Hello,
    any chance of some actual memory-usage/timing statistics?
    I know it’s time-consuming but it would help give weight to your suggestions
    Ta,
    Amir

Leave a Reply

Captcha
Enter the letters you see above.


About this site:
This is my (Jaisen Mathai) personal site for potential employers who want to see my resume or portfolio. My ideal job would be to work as a PHP developer on a large scale consumer website. My experience is in using PHP, MySQL, Ajax and JSON. I really enjoy creative brainstorming...taking a problem apart and narrowing 100 solutions down to the best one.

Thanks for stopping by. Be sure to drop me a line.