PHP Programmers Club

PHP Programmers Club

Share

02/12/2014

This is an excerpt from the book "BASIC PHP" by June Bancale (coming soon)

Part 7 on PHP Programming Style..

TIPS ON PERFORMANCE

1) Use faster functions and statements.
a) Always use echo instead of print; It is much faster and less confusing
literally.
b) Use echo’s multiple parameters instead of string concatenation.
c) Always use sprintf rather than str_replace. It is palpably faster.
d) Always use sprintf instead of variables contained in double quotes.
e) using undefined variables is slower than pre- initialized variables.
f) Use the more efficient switch..case statement instead of if..elseif
statement.
g) Always use single quotes; double quotes usually takes longer to
to process since php looks for variables inside double quotes.
h) Use require over require_once to ensure fast loading and caching.
i) Use include over include_once to ensure fast loading and caching.

2) Use faster processes, functions and statements.
a) For loop: Put the max value in a variable before using in a for-loop.
b) Database access is slow. If you want to store a small amount of data, try
putting it in text files. Accessing files is faster than accessing databases.
c) Error suppression with @ is discouraged. It is slow.
d) Initialize local variables at the start of a program or function
e) Use error_reporting(E_ALL) during debug; but change to
error_reporting(E_ERROR | E_WARNING | E_USER_ERROR |
E_USER_WARNING ) when shifting to live.

3) Free memory after use.
a) Always assign null to your variables after use, to free memory.
b) Put memory intensive processes inside user defined functions.
Memory is automatically cleared upon function exit.

4) Maintainability and readability is of paramount importance than
speed or performance. Where you have to choose between the
two, choose maintainability; easy to read, easy to debug.

5) When speed is critical, optimize first the hardware. Upgrade on
processor and additional memory.

6) Optimize database queries. More often than not, a query is the
cause of speed degradation of a program.

7) Optimize your web server’s harddisk access. The slowest
component of a computer is the harddisk. So it is only prudent to
minimize harddisk accession.

8) Optimize HTML. Next to queries, inefficient HTML is the culprit
when it comes to speed problems.

to be continued...

Want your club to be the top-listed Nightclub in Malolos?
Click here to claim your Sponsored Listing.

Category

Website

Address


136 Everlasting Street, Bulihan
Malolos
3000