PHP Double Quoted Strings and Heredoc
PHP Strings – Part 1
Foreword: In this part of the series, I talk about PHP Double Quoted Strings and PHP Heredoc.
By: Chrysanthus Date Published: 13 Aug 2013
Note: the output of all the code samples of this tutorial series are sent to the browser.
Note: in this article, if you cannot see any text or piece of code or if you think something (e.g. an image) is missing or link does not operate, or just want to comment, contact me at email@example.com .
There are certain topics you should have covered before reaching here, in order to understand this series. You can click the link, “PHP Course” below to see what you should have studied before reaching here.
Double Quoted String
An example of a double quoted string literal is,
"I like what I am seeing"
The double quote used at the start and end of the string is that of the text editor and not of the word processor. That of the word processor may result in an error message when the PHP code is run.
A double quoted string can be assigned to a variable as in the following code, which you should read and try.
$var = "I like what I am seeing";
Assume that you are typing in a text editor. If you want more than one consecutive spaces, you press the keyboard spacebar the corresponding number of times. If you want a newline, you press the Enter key. If you want a number of consecutive newlines, you press the Enter key the corresponding number of times. The result is an overall formatting with spaces and newlines.
PHP can send this overall formatting to the console or a text editor file, but on condition that you use the Heredoc or Nowdoc syntax (see below). This would also send the overall formatting to the web page at the browser, but the web page would not display consecutives spaces and it would not display newlines. This is because the web page needs but a number of “ ” for consecutive spaces and <br> for a new line.
Each web page displayed at the browser has a corresponding text editor-like page where it shows the code of the web page. To open the text editor-like page of a web page, right-click within the web page. In the pop-up menu that appears click “View Page Source” or “View Source” or a similar command to open the text editor-like page.
The above overall formatting can be seen in its entirety (all code) in the text editor page. The output of the code samples below are sent to the web page. So in order to see the effect of the Heredoc syntax, you will have to open the text editor-like page of the web page (browser).
Consider the following script
$str = <<<EOD
some spaces yes space
a line and yes a line
another line and spaces
The heredoc syntax begins with <<<. This is followed by an identifier without $. Then you press the keyboard Enter key. You then type the string content giving any number of consecutives spaces you want and just pressing the Enter key at the end of each line. The last line of the syntax, is the same identifier, and should be typed at the beginning of this last line. So, the heredoc string has an opening identifier and a closing identifier, which are the same, e.g. EOD above. You can choose any name you want for the identifier. The heredoc string above is assigned to the variable, $str. This variable is echoed (printed – sent to the browser). Read and try the code, if you have not already done so.
Escaped Sequences or Escaped Characters are special characters. A character here actually consists of two characters with the first one being the backslash. Common escaped sequences and their meanings are:
\r carriage return
\t horizontal tab
\v vertical tab
\f form feed
\$ dollar sign
Double Quoted Strings and Heredoc with Escaped Characters
An escaped character in a double quoted string has its effect at the output. For example, \t would produce a horizontal tab (indentation); \n would send the text on its right to the next line; \v would produce a vertical tab. Watch out for single and double quotes (see below). Read and try the following script (you will not see the effects at the web page of the browser; you will see the effects in the text editor-like page opened):
$var = "This is the first line.\nThis is the second line with \" a double quote.\n\tStart the third line with horizontal tab.";
Note: in a string delimited by double quotes, if you want a double quote within the string you have to escape it with a backslash, otherwise there will be conflict between the double quote and the delimiters and the script will not run and it will issue an error message.
Read and try the following code for a heredoc example:
$str = <<<EOD
some spaces yes space\na line, " yes a line with double quote\n\tanother line \$ with spaces and escaped characters
You do not escape a single quote while typing it within a double quoted string; if you do that the backslash will appear in the output. A single quote typed within a double quoted string does not produce any delimiting conflict. To have a double quote within a double quoted string, you have to escape it to prevent delimiting conflict. With heredoc also, you type a single quote without escaping; if you escape, the backslash will appear at the output.
A variable within a double quoted string or heredoc string, is expanded; this means that the value of the variable is obtained at the output; the value also replaces the variable in the string when the script runs. Read and try the following code:
$var = "music";
$str1 = "I like $var; it makes me feel good.";
echo $str1, "<br>";
$str2 = <<<EOD
You should like, $var; it is good.
echo $str2, "<br>";
That is it for this part of the series. We stop here and continue in the next part.
Related LinksBasics of PHP
PHP Directory Function Basics
Understanding PHP Reference
PHP Function Arguments
Understanding Variable Scope in PHP
Object Oriented Programming in PHP
PHP Data Types Simplified
Exception Handling in PHP
PHP Regular Expressions
Sending Email with PHP
Date and Time in PHP
PHP String Functions for Website Design
PHP Variable Scope
Advanced Features of PHP Function
Array in PHP
PHP Two Dimensional Arrays
Understanding Object Oriented Programming in PHP
Advanced PHP Regular Expressions
Some features of PHP Entities
PHP Web Application
More Related LinksMajor in Website Design
Web Development Course