PHP performance tips

April 22, 2009

It’s Sunday morning, the weather’s terrific and I’m sitting at the desk in the office at home finishing off some project work that should have been done on Friday. Still, it’ll be done in a jiffy and I can take the dog out to the meadow in St Ives and reflect on why I don’t work at home every day rather than waste 4 hours commuting. It’s more productive, it doesn’t seem like work and I just get more time.

Anyway, I can’t talk too much about the project itself, other than it’s a website developed using PHP which involves extensive natural language parsing and processing. It needed to be performant and scalable so, although I’ve used PHP many times before, I thought it would be useful to revisit PHP performance.

Consequently, I build a small performance testing framework so I could quickly evaluate which PHP methods yielded the best results. Over the duration of the development I’ve compiled the following list which I thought I’d share.

  • Use single quotes over double quotes.
  • Use switch over lots of if statements.
  • Avoid testing loop conditionals with function tests every iteration eg. for($i=0;i<=count($x);$i++){…
  • Use foreach for looping collections/arrays.
  • PHP4 items are byval PHP5 items are byref
  • Consider using the Singleton Method when creating complex PHP classes.
  • Use POST over GET for all values that will wind up in the database for TCP/IP packet performance reasons.
  • Use ctype_alnum,ctype_alpha and ctype_digit over regular expression to test form value types for performance reasons.
  • Use full file paths in production environment over basename/fileexists/open_basedir to avoid performance hits for the filesystem having to hunt through the file path. Once determined, serialize and/or cache path values in a $_SETTINGS array. $_SETTINGS[“cwd”]=cwd(./);
  • Use require/include over require_once/include_once to ensure proper opcode caching.
  • Use tmpfile or tempnam for creating temp files/filenames
  • Use a proxy to access web services (XML or JSOM) on foreign domains using XMLHTTP to avoid cross-domain errors. eg.<–>XMLHTTP<–>
  • Use error_reporting (E_ALL); during debug.
  • Set Apache allowoverride to “none” to improve Apache performance in accessing files/directories.
  • Use a fast fileserver for serving static content (thttpd).,
  • Serialize application settings like paths into an associative array and cache or serialize that array after first execution.
  • Use PHP output control buffering for page caching of heavilty accessed pages
  • Use PDO prepare over native db prepare for statements. mysql_attr_direct_query=>1
  • Do NOT use SQL wildcard select. eg. SELECT *
  • Avoid using SQL directive DISTINCT
  • Use database logic (queries, joins, views, procedures) over loopy PHP.
  • Use shortcut syntax for SQL inserts if not using PDO parameters parameters. eg. insert into sometable (field1,feild2) values ((“x”,”y”),(“p”,”q”));
  • Use Zend – it’s the best PHP library around

Comments on a postcard please.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: