PHP String Variables: To Quote… or Double Quote?

…that is the question.

Oftentimes i find myself faced with a situation wherein I must decide whether to use PHP’s single or double quotes. If you’re not aware of it by now, then you should STOP everything and go learn a bit about the differences between the two: a single-quoted string, vs a double-quoted string– and how the contents therein is parsed based on the very factor of the type of quote which encapsed the string itself.

Sometimes i use a double-quote technique, among other reasons, for the purpose of using the n Escape Sequence at the end of each line. Using the escape sequence, otherwise known as the “newline” ASCII control-character, results in source-code which is much easier to read. Why? Because, when writing php code, depending upon how it interprets anything put between quotation marks, including such string-elements as the line-feed you see in your text editor when you hit Enter ds — a “Line Feed & Carriage Return” combination in Windows, or a Line-Feed only in Linux– will most likely result in the HTML output for that section of source code spanning further right-wise than those around it. As would any HTML coded without ever hitting Enter (or Return), the viewer would be forced to scroll right to read any lines without a proper LF/CR combination. If you’re intrigued by this concept, I recommend you try to better understand the n (sometimes written differently, depending upon the programming language, and often– the personal preference of the programmer) so that you can make a better decision when writing your own code.

In contrast to the double-quote technique, a single-quoted string has its own unique benefits– especially when it comes to HTML code (though, conversely, some people see a benefit to HTML in the double-quoted string). The first thing anyone who has been writing HTML for a while will notice when he comes new to PHP is that certain characters must be “Escaped” in a variable string– because of their default action-value within the programming language itself. For example, as shown above, we saw that the “backslash-N” character “Escape Sequence” is ITSELF one such special control character. If the “backslash” were not used, we’d simply have a dopey old “n” sitting there in the middle of the page, all off on his own! Similarly, a “String Variable” in PHP may be wrought with mistakes if one is not careful to escape such special control characters when setting the value of that string by way of a double-quoted encapsulation. However, as i had begun, the single-quote technique does NOT interpret the control characters, and therefore– the programmer does NOT need to escape certain common characters of HTML, such as those double-quotes which make up the HTML Element’s attributes.

If a programmer were to use double-quotes when setting a PHP string, any HTML wherein an element, for example, contains an attribute declared using double-quotes (the valid way of declaring XHTML attribute values, of course), he would have to pay attention that each occurrence of a double-quote (e.g. [ ” ] ) is in fact properly escaped, such as

$hello="<p id="hi">";

I want to share with you my own preferred technique for beating the system; why i prefer a single-quoted string in PHP when dealing with a lot of HTML. I’ve heard that it is in poor practice among the more devout classicists to use single-quoted strings to save oneself the bother of all of that character escaping, but i do what works for me. Typically, i use single-quotes, but go back to double-quotes when i want the string perhaps to function in a special way in which i’d need to have my control characters be responsive to me (such as the newline “backslash N”).

Have a look at the following example of how to insert a Newline character in a single-quoted string.

$php_string =  '<dd class="formDD">
 '<ul class="formInputLists">
 '<li class="formInputItem">Insert a Manufacturer&#x27;s name:<br />
 '<input type="text" id="man_name" name="man_name" value="" /></li>
 '<li class="formHidden"><?php formHidden($formId[$fc]); ?></li>
 '<li class="formInputItem">
 '<input type="submit" name="newManSub" id="newManSub"
 value="Enter Manufacturer Info" /></li>

Look closely, and you’ll see that each line ends on the “next-line” where i then close the single-quote, and place the concatenation operator there next to it, then make another new line, and re-open the single-quote string declaration– again, without any special character esacpae sequences.

The new-line character is itself a part of the encapsed string! Instead of specifying with the newline escape-sequence, n to tell the parser “make a new-line here”– all that’s needed from the programmer is to hit “return” (or “enter”) while typing the variable string. In much the same way that the HTML element <pre> preserves any newlines and extra white-space, the single-quoted PHP string preserves Literally (hence the term, ‘String-Literal’) the hidden, special-characters!

If you’re a programmer, give this technique a try when you have a lot of HTML you want to copy into a PHP string.
Note: if your code is already setup for double-quoted strings, you will have to remove the escaped quotes or changing to string-literal double-quotes will break your existing code. I can not take responsibility for any mistakes or misfortune which might come to your resulting application for having followed this suggestion. The best practice is to have a clear knowledge of what you are coding, beginning with the PHP manual section on String Literal Syntax.

It’s also worth learning the Heredoc syntax, if you aren’t familiar with it.

Good Luck to you, and most of all– enjoy what you do with it!

Whatchu do

Leave a Reply

Your email address will not be published. Required fields are marked *