Wednesday, August 19, 2009

Speed up your PHP application

Hi to all!!

Today I will be talking about handling errors trough our localhost and speeding our application.
Typical error handling when you by default install Apache/PHP is nothing special,
bunch of ugly error messages displayed on your screen :)

Second and the most important thing is speeding up your code, finding all those bottlenecks in your application,
and belive me, you will have them. Especially if you are developing huge application or framework and after a while you
decide to check your coding :)

Now, let's get to work!

First we need to install :

- Xdebug : Wondreful thing for handling errors from PHP. You will definetly love it.

Go to http://www.xdebug.org/ and download it. All you need is to put .dll (Sorry to all that are working on
Linux), but on xdebug site you have all documentation for installing this is just a quick tour (windows) :)
Include your .dll in php.ini and restart apache.

Now let's talk about error handling in general, configuring it to work as we need and when we need error display.
First open your php.ini :

display_errors = On
error_reporting = E_ALL

With this two lines you set up PHP to display every kind of error there is. Now the real question to this, do we realy need
all theese errors. Answer: you will need them occasionally, when you are testing code and application.
Shure you will need to see all those notices, this is very important because this way you will see everything.
First step to speeding application is removing errors :)

But definetly you will need them occasionally, so in your index.php or configuration.php (script that you allways call) set up
this line :

display_errors(0);

Now, every time you need errors you set this to 1, and with xdebug you will have beautifull messages instead of those ugly ones
that you had before.

One thing before we proceed, in php.ini you have to add this line of code

[XDEBUG]
xdebug.profiler_enable = 1
xdebug.profiler_output_dir = "d:\webs\errors"

Change output dir into whatever dir you want, this is the place where xdebug will output his report.

Now one last thing to download :

WinCacheGring



WinCacheGrind is tool that reads xdebug output and displays information about execution of our code.
Now if you open it you can analyze in details every segment of your code, and with function that has been executed
you can see timing (how long does it takes to execute). I don't need to tell you how important is this, how this
will help speed up your coding and realy give you guidance what are you doing wrong or right :)


Cheers!

No comments:

Post a Comment