Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

PHP character-usage

Sunday, July 13th, 2014 by Servage

BM8T0Escaping characters

Sometimes a string needs to contain characters with special meanings that might be interpreted incorrectly. For example, the following line of code will not work because the second quotation mark (apostrophe) encountered in the word sister’s will tell the PHP parser that the end of the string has been reached. Consequently, the rest of the line will be rejected as an error:

$text = 'My sister's car is a Ford'; // Erroneous syntax

To correct this, you can add a backslash directly before the offending quotation mark to tell PHP to treat the character literally and not to interpret it:

$text = 'My sister\'s car is a Ford';

You can perform this trick in almost all situations in which PHP would otherwise return an error by trying to interpret a special character. For example, the following doublequoted string will be correctly assigned:

$text = "My Mother always said \"Eat your greens\".";

Additionally, you can use escape characters to insert various special characters into strings, such as tabs, newlines, and carriage returns. These are represented, as you might guess, by \t, \n, and \r. Here is an example using tabs to lay out a heading; it is included here merely to illustrate escapes, because in web pages there are always better ways to do a layout:

$heading = "Date\tName\tPayment";

These special backslash-preceded characters work only in double-quoted strings. In single-quoted strings, the preceding string would be displayed with the ugly \t sequences instead of tabs. Within single-quoted strings, only the escaped apostrophe (\’) and the escaped backslash itself (\\) are recognized as escaped characters.

Multiple-Line Commands

There are times when you need to output quite a lot of text from PHP, and using several echo (or print) statements would be time-consuming and messy. To overcome this, PHP offers two conveniences.

A multiline string echo statement

<?php
$author = "Alfred E Newman";
echo "This is a Headline
This is the first line.
This is the second.
Written by $author.";
?>

A multiline string assignment

<?php
$author = "Alfred E Newman";
$text = "This is a Headline
This is the first line.
This is the second.
Written by $author.";
?>

PHP also offers a multiline sequence using the <<< operator, commonly referred to as here-document or heredoc for short. This is a way of specifying a string literaly, preserving the line breaks and other whitespace (including indentation) in the text. Its use can be seen earlier.

Alternative multiline echo statement

<?php
$author = "Alfred E Newman";
echo <<<_END
This is a Headline
This is the first line.
This is the second.
- Written by $author.
_END;
?>

What this code does is tell PHP to output everything between the two _END tags as if it were a double-quoted string. This means it is possible for example for a developer to write entire sections of HTML directly into PHP code and then just replace specific dynamic parts with PHP variables.

It is important to remember that the closing _END; tag must appear right at the start of a new line and must be the only thing on that line—not even a comment is allowed to be added after it (nor even a single space). Once you have closed a multiline block, you are free to use the same tag name again.

NOTE:

Remember: using the <<<_END…_END; heredoc construct, you don’t have to add \n line-feed characters to send a line feed—just press Return and start a new line. Also, unlike either a double-quote- or single-quotedelimited string, you are free to use all the single and double quotes you like within a heredoc, without escaping them by preceding them with a backslash (\).

Sources for further reading

PHP character-usage, 3.7 out of 5 based on 3 ratings
Categories: Guides & Tutorials, Tips & Tricks

Keywords:

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.