Online Book Reader

Home Category

Facebook Cookbook - Jay Goldman [49]

By Root 704 0
since the browser has shipped, support will certainly follow shortly if it isn’t already provided). Facebook has promised to support Internet Explorer 8 when it ships.

Starting Out in PHP


—James DeFillippo (see his bio in Contributors)

Problem


I’m new to PHP and want some general tips on optimizing my application so that it can scale gracefully.

Solution


One of the easiest and most overlooked ways to optimize a PHP application is the proper use of double and single quotes when handling strings. Using double quotes only where they are entirely necessary can give your app the runway it needs to get off the ground.

For example, you wouldn’t want to use:

echo "This is a simple piece of text with a single $variable";

when this is faster to execute with much lower memory requirements:

echo 'This is a simple piece of text with a single ' . $variable;

Discussion


When dealing with strings in PHP, there are two ways of using quotation marks to wrap your strings. One involves using single quotes (') and the other uses double quotes ("). There is a major difference—not only in functionality, but also in speed—when it comes to using the two different options.

PHP will take anything wrapped in a matching set of single quotes and treat that as a string literal. Literally anything in between single quotes is a string and should be treated as such, with no interference from the parser. If you wrap the string in double quotes, though, PHP will parse every element in that string looking for variables for reassignment. Even if none are present, it still has to look, which means if you are wrapping any piece of text in double quotes that does not have a variable in it, you’re throwing compute cycles away and making your application slower for no reason.

On smaller and low-load applications, the difference in speed is not noticeable, which is why it’s often overlooked during testing and ignored as a potential bottleneck. As your application grows in size, complexity, and traffic, the difference can be extremely troublesome and can hinder your ability to serve your clients or customers.

A recent application that I rearchitected to use proper quoting for strings saw an immediate speed increase of 35%! And all we had to do was make sure that the right quotes were used for the right job.

When assigning plain text strings, common practice is to use something like this:

$error_message = "Hey $username! Something's broken. Try again.";

Since there is a variable in the assignment, common thinking is to wrap the entire assignment in double quotes and call it a day. This works just fine from a functional standpoint, but from a speed standpoint you’re wasting time and memory by having PHP look at every other word in that string to see if it also is a variable. The preferred method for assigning a string with a variable is the "." concatenation method. For example:

$error_message = 'Hey ' . $username . '! Something\'s broken. Try again.';

By breaking the line into two string literals and a simple variable replacement, we’ve taken the burden off of PHP when it comes to finding out exactly what each element of that assignment is. You will also note the \' in the previous example. When you’re dealing with string literals and single quotes, you need to remember to escape your containing characters. The same thing goes for double quotes inside of a double-quoted string.

And quotes don’t just matter when you’re doing variable assignments or echos; they also matter when dealing with arrays, ifs, elseifs, and almost anything else you can use a string for. The following are a few of the more common things I’ve seen that can easily sneak into your code if you’re used to using double quotes for all your string needs.

When manually initializing an array, you should always use single quotes for any strings:

// Don't

$foo = array("a", "b", "c");

// Do

$foo = array('a', 'b', 'c');

When accessing a named array slice, always (and I mean always) use single quotes:

// Don't

$foo = $array["slice"];

// Do

$foo

Return Main Page Previous Page Next Page

®Online Book Reader