Cyclone
Cyclone

Reputation: 18295

Tricks to speed up page load time in PHP

I know of these two tricks for speeding page load time up some:

@ini_set('zlib.output_compression', 1);

which turns on compression

ob_implicit_flush(true);

which implicitly flushes the output buffer, meaning as soon as anything is output it is immediately sent to the user's browser. This one's a tad tricky, since it just creates the illusion that the page is loading quickly while in actuality it takes the same amount of time and the data is just being shown faster.

What other php tricks are there to make your pages load (or appear to load) faster?

Upvotes: 1

Views: 8415

Answers (4)

CodePlateau
CodePlateau

Reputation: 391

There are some that can speed your website(code custmoization)

1) If you’re looping through an array, for example, count() it beforehand, store the value in a variable, and use that for your test. This way, you avoid needlessly firing the test function with every loop iteration.

2) use build in function instead of custom function

3) put JavaScript function and files at bottom of file

4) use caching

Upvotes: 1

Damon
Damon

Reputation: 70126

Among the best tricks to speed up PHP page loads is to use as little PHP as possible, i.e. use a PHP cache/accelerator such as Zend or APC, or cache as much as you can yourself. PHP that does not need to be parsed again is faster, and PHP that does not run at all is still faster.

The same goes (maybe even more so) for database. Use as few queries as possible. If you can combine two queries into one, you save one round trip.

Upvotes: 0

Chris
Chris

Reputation: 1579

The best way is to ensure that your script isn't creating/destroying unnecessary variables and make everything as efficient as possible. After that, you can look into a caching service so that the server does not have to reparse specific parts of a page.

If all that doesn't make it as fast as you need it to be, you can even "compile" the php code. Facebook does this to support faster load times. They created something called "HipHop for PHP" and you can read about it at: https://developers.facebook.com/blog/post/358/

There are other PHP compilers you can use to help.

If all this fails, then I suggest you either recode the website in a different language, or figure out why it is taking so long (more specifically, WHAT is causing it to take so long) and change that part of the website.

Upvotes: 2

zerkms
zerkms

Reputation: 254916

It is always better to define a real bottleneck and then try to avoid it.

The way to follow any trick that is supposed to make something faster without understanding whether you have the problem or not - is always a wrong way.

Upvotes: 5

Related Questions