10 Performance Tips to Speed Up PHP

Even the most experienced programmer can be lazy. After all, what are a few milliseconds of extra load time here or there? Users won’t notice, right?

That sort of thinking is a slippery slope, though. A fraction of a second here or there, measured over thousand of visits, can add up to lost conversions. A little extra processing time per execution, over millions of executions, can add up to significant power consumption and wear and tear on the server. And since most good programming habits require no additional work on the part of the programmer, there’s a real business case to be made against lazy programming.

It’s time to break out of those lazy habits and start coding with performance in mind. Practice these ten PHP performance tips and watch your code go from sluggish to speedy in no time.

  1. Use echo instead of print(). As a language construct rather than a function, echo has a slight performance advantage over print().
  2. Echo with commas, not periods. I’m a repeat offender of this one. If you use periods, PHP has to concatenate the string before it outputs. If you use commas, it just outputs them in order with no extra processing.
  3. Avoid function tests in loop conditionals. 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.
  4. Use include() and require() instead of include_once() and require_once(). There’s a lot of work involved in checking to see if a file has already been included. Sometimes it’s necessary, but you should default to include() and require() in most situations.
  5. Use full file paths on include/require statements. Normalizing a relative file path can be expensive; giving PHP the absolute path (or even “./file.inc”) avoids the extra step.
  6. Favor built-in functions over custom functions. Since PHP has to take the extra step of interpreting your custom functions, built-in functions have a performance advantage. More importantly, there are a lot of useful built-in functions that you may never learn about if you always default to writing your own.
  7. Avoid needlessly copying variables. If the variable is quite large, this could result in a lot of extra processing. Use the copy you already whenever possible, even if it doesn’t look pretty (e.g., $_POST[‘somevariable’]).
  8. Pass unchanged variables to a function by reference rather than value. This goes hand-in-hand with the point about needlessly copying variables. Much of the time, your functions only need to use the values from their parameters without changing them. In such cases, you can safely pass those parameters by reference (e.g., function(&$parameter) rather than function($parameter)) and avoid having to make memory-intensive copies.
  9. Debug with error_reporting(E_ALL). Every warning is a performance improvement waiting to happen, but only if you can see it. Cleaning up warnings and errors beforehand can also keep you from using @ error suppression, which is expensive. Just don’t forget to turn off error reporting when you’re done; warnings and errors are expensive as well.
  10. Ditch double quotes for single quotes. There’s some disagreement, but the common wisdom is that PHP has to do extra processing on a string in double quotes to see if it contains any variables. Concatenation with single quotes is marginally faster.

Sources

Tips from this article were pulled from the following sources.

How to Hotwire WordPress in 7 Steps

First, let me go ahead and set the right expectation for this article. This is a brief guide to gaining legitimate access to a WordPress site for which you do not know the username or password. This is NOT a guide on hacking a WordPress site.

It’s a fine line, but there are legitimate reasons for needing to hotwire WordPress. For example, the client who owns the site may not be available to provide the username and password, or a hacker may have locked you out. Personally, when I need to get into a client site, I find hotwiring is sometimes faster than digging up the password myself and waiting on someone else to find it for me. The point is, there are good, honest reasons to do it.

Now that you’ve listened to my lengthy disclaimer, we can get down business.

  1. Access the database. Most web hosts nowadays offer PHPMyAdmin, which I recommend using. If you don’t know the database credentials, you can get them from the wp-config.php file in the WordPress root directory.
  2. Find the users table. Don’t forget that it might have a prefix; if there’s any confusion, the prefix should also be in wp-config.php.
  3. Find the account you want to hotwire. In my experience, the first user is most often the administrator with all of the best privileges, so that’s the row you’ll want to change. If the user_login is “admin,” like it is in most default WordPress installations, you may want to make a note to discuss changing it as a security precaution, but that’s a different article.
  4. Copy the old user_pass. See that gobbledygook in the user_pass field? Copy it and save it for later. Otherwise, you won’t be able to restore the previous password. In the event that you’re changing the password permanently, it’s not necessary to copy the original value, but that would be more like hijacking that hotwiring. 😉
  5. Insert your temporary user_pass. You’ll want to place an md5-encrypted version of the temporary password you’ll be using in place of the old user_pass. There are plenty of websites out there that can hash it for you; just plug “md5 encrypter” into Google. Put the md5 hash value into the user_pass field and save your changes.
  6. Log into WordPress. You should now be able to log in normally using the username of the account you’re hotwiring and the temporary password that you encrypted.
  7. Restore the old user_pass. Most people don’t like having their passwords changed arbitrarily, so it’s good form to change it back for them. To do this, repeat steps one through three and replace the user_pass field with the original value that you overwrote.