Creating Strings in PHP

Learn how to create strings in PHP. Looks at single and double quoted strings; variable parsing; escape sequences; multi-line strings; heredoc/nowdoc syntax, and more.

A string in PHP is any sequence of characters. Here are some examples of strings:

  • Hello, world!
  • abc123
  • #@@$%&*%

PHP lets you work with strings in many different ways. For example, you can search a string for a piece of text; extract characters from a string; and format a string to make it easier to read or use.

In this tutorial you learn how to create strings in your PHP scripts. You look at:

  • Creating strings with single quotes and double quotes
  • Avoiding confusion by using braces syntax
  • Escape sequences
  • How to create multi-line strings
  • Using your own delimiters to create strings

Creating strings using single and double quotes

To include a literal string in your PHP script, simply enclose the string in:

  • Single quotes — e.g. 'Hello there!', or
  • Double quotes — e.g. "Hello there!".

If you use single quotes then PHP reads the string exactly as you typed it. Double quotes work in a similar way, but a couple of extra features also come into play:

  • Variable parsing: Variable names within the string are replaced with the corresponding variable values
  • Character escaping: You can use escape sequences to include special characters in the string

Variable parsing in double-quoted strings

Here's an example that shows variable parsing in action:

$myString = 'there'; 
echo 'Hello, $myString!<br/>'; // Displays "Hello $myString!" 
echo "Hello, $myString!<br/>"; // Displays "Hello there!" 

Notice how, with single quotes, the "$myString" text is interpreted as-is, while with double quotes, "$myString" is replaced with the contents of the $myString variable ("there").

Sometimes you run into problems with variable parsing — for example:

$favouriteBird = 'owl'; 
echo "My favourite birds are $favouriteBirds"; // Displays "My favourite birds are " 

Here you want to insert the value of the $favouriteBird variable, but PHP thinks you mean the $favouriteBirds variable (which doesn't exist). The solution is to wrap the variable name (and, optionally, the dollar sign) in curly braces:

$favouriteBird = 'owl';
echo "My favourite birds are $favouriteBirds"; // Displays "My favourite birds are " 
echo "My favourite birds are ${favouriteBird}s"; // Displays "My favourite birds are owls" 
echo "My favourite birds are {$favouriteBird}s"; // Displays "My favourite birds are owls" 

Curly braces also let you include more complex expressions in strings, such as array elements and object properties:

echo "My name is {$myDetails['name']}.";
echo "My name is {$me->name}.";

Character escaping in double-quoted strings

Here's an example that demonstrates character escaping:

echo '<pre>Hi\tthere!</pre>'; // Displays "Hi\tthere!"  
echo "<pre>Hi\tthere!</pre>"; // Displays "Hi      there!" 

With single quotes the \t text is used as-is. However, with double quotes, the \t is recognized as the escape sequence for a tab character, so a tab is inserted into the string.

As well as the "tab" escape sequence (\t) just shown, there are many more escape sequences you can use in double-quoted strings. Here are a few common ones:

Line feed (ASCII 10)
Carriage return (ASCII 13)
Horizontal tab (ASCII 9)
Vertical tab (ASCII 11)
Form feed (ASCII 12)
Literal backslash
Literal $ (dollar) symbol
Literal double quote


echo "Here's a double quote: \"";


Here's a double quote: "

Use \' to include a literal single quote inside a single-quoted string. To include the literal 2-character sequence \', use \\\'.

Creating multi-line strings

If you want to insert a multi-line string in your PHP code, just use newlines to end each line:

$myString = "Line 1\nLine 2\nLine 3\n";

$myString = " 
  In himself he is;
  But in this kind, wanting your father's voice,
  The other must be held the worthier.

Using your own delimiters: heredoc and nowdoc

You can also surround a string with your own delimiters by using PHP's heredoc syntax:

$myString = <<<MY_DELIMITER
  In himself he is;
  But in this kind, wanting your father's voice,
  The other must be held the worthier.

Heredoc syntax is handy if your string contains a mixture of single and double quotes, since it saves you having to escape them:

$myString = <<<MY_DELIMITER
'Then she said "goodbye" and
left,' he continued.

Heredoc syntax behaves like double-quoting — variables in the string are parsed and escape sequences are processed. If you want to avoid this, use nowdoc syntax, which is the equivalent of using single quotes. Nowdoc syntax is the same as heredoc syntax, except that you enclose the first delimiter in single quotes:

$myString = 'there';

$heredocString = <<<END_TEXT
Hello, $myString!<br/>

$nowdocString = <<<'END_TEXT'
Hello, $myString!<br/>

echo $heredocString; // Displays "Hello, there!"
echo $nowdocString;  // Displays "Hello, $myString!"

You now know how to create PHP strings. You've seen how to use single and double quotes to insert string literals in your PHP scripts; how variable parsing and character escaping work; how to create multi-line strings; and how to use your own string delimiters with heredoc and nowdoc syntax. Happy coding!

Learn PHP With Ease!

Written by Matt Doyle — ELATED's resident Web programming expert — Beginning PHP 5.3 is a complete introduction to PHP, covering everything in these tutorials and lots more besides. Find out how to:

  • Set up PHP on your computer
  • Use strings, arrays, functions and objects
  • Create interactive Web forms
  • Handle cookies and sessions
  • Work with files on the server
  • Build database-driven sites with MySQL
  • Send emails from your scripts
  • Create images on the fly with PHP
  • Work with regular expressions
  • Write robust, secure PHP applications

...and lots more!

“What a pleasure it's been spending hours and hours studying PHP with this magical book.” — Lulio, Florida
“The book is not only great for learning, but I find myself using it constantly as a reference as well!” — David A. Stoltz

Buy Beginning PHP 5.3 now from Amazon.comBeginning PHP 5.3 or PHP 5.3.

Follow Elated

Related articles

Responses to this article

There are no responses yet.

Post a response

Want to add a comment, or ask a question about this article? Post a response.

To post responses you need to be a member. Not a member yet? Signing up is free, easy and only takes a minute. Sign up now.

Top of Page