PHP Strings
Declaring Strings
There are three ways.
Single Quoted
The easiest way to declare a string is by enclosing it by single quotes. Character: '
$string = 'This is my first string';
What if you needed to add a single quote inside a single quoted string? Just escape that character with a back slash. \'
# This is the Hyvor's PHP Tutorial
$string = 'This is the Hyvor\'s PHP Tutorial';
What if you needed to have \' in a single quoted string? Use \\ to escape the \ character. And, as in the last example, \' to escape the ' character.
# Backslash and Quote: \'
$string = 'Backslash and Quote: \\\'';
Double Quoted
Strings can be declared enclosed by double quotes. Character: ".
In single quotes, only \\ and \' had a special meaning. But, in double quotes, there are more escape sequences.
Escape Sequence | Meaning |
---|---|
\n | Line Break |
\r | Carriage Return |
\t | Tab Space |
\\ | Backslash |
\$ | Dollar Sign |
\" | Double Quotes |
PHP Double Quoted String Example
<pre>
<?php
echo "Hello World";
echo "<br>";
echo "\"Hello World\"";
echo "\n\tHello World\n";
?>
</pre>
Run Example ››Tip: The reason we have echoed out the output from PHP inside <pre> tags is those tags allows you to show the text as it is in the browser. (By default, HTML ignores extra white spaces)
In the table, you may notice the $ (dollar) sign. Why is it there? The reason is that we can add variables inside double quoted texts
Variables within Double Quoted Strings
When you add a variable within a double quoted string, PHP will parse it.
PHP Variables in Double Quoted String
<?php
$name = "Hyvor";
echo "My name is $name";
Run Example ››If we needed to add a s character in front of a variable in the same double quoted string, we will need to wrap the variable with curly braces.
PHP Variables in Double Quoted String - Complex Syntax
<?php
$fruit = 'Apple';
// echo "$fruits"; # there's no variable $fruits
echo "{$fruit}s";
Run Example ››PHP Single vs. Double Quoted Strings' " Will be treated literally, displays exactly what you type Will be interpreted Does not work with escape sequences, except \' and \\ Works with escape sequences Variables inside are not parsed Variables inside are parsed
<?php
// both works
echo "Hello <br>";
echo 'Hello <br>';
// escape sequences are not parsed in single quotes
echo "\"World\" <br>"; // outputs "World"
echo '\"World\" <br>'; // outputs \"World\"
// variables are not parsed in single quotes
$hello = "Hello";
echo "$hello World <br>"; // outputs Hello World
echo '$hello World <br>'; // outputs $hello World
Run Example ››
' | " |
---|---|
Will be treated literally, displays exactly what you type | Will be interpreted |
Does not work with escape sequences, except \' and \\ | Works with escape sequences |
Variables inside are not parsed | Variables inside are parsed |
<?php
// both works
echo "Hello <br>";
echo 'Hello <br>';
// escape sequences are not parsed in single quotes
echo "\"World\" <br>"; // outputs "World"
echo '\"World\" <br>'; // outputs \"World\"
// variables are not parsed in single quotes
$hello = "Hello";
echo "$hello World <br>"; // outputs Hello World
echo '$hello World <br>'; // outputs $hello World
Run Example ››Doc Syntax
Doc Syntax is the next way of declaring a string. It has two types which each works like single quoted and double quoted strings.
- Nowdoc Syntax - Behaves like Single Quoted
- Heredoc Syntax - Behaves like Double Quoted
Doc Syntax
- Starts with <<<.
- Then, add an identifier in the same line enclosed with single quotes or double quotes. (Single quotes for nowdoc and double quotes for heredoc)
- Write the string in a new line. (It can be multiline)
- End with the identifier and a ; (Semi colon) in a new line. (First letter of the identifier should be the first character in that line. It is a must!)
Nowdoc
Nowdoc string is declared using the doc syntax and single quotes for the identifier.
PHP Nowdoc Example
<?php
$string = <<<'MYSTRING'
I'm a nowdoc string
Did you notice that I can write '' without escaping?
I don't have special meaning for \n, \r and \t
I do not parse variables $name
MYSTRING;
echo '<pre>' . $string . '</pre>';
Run Example ››Nowdoc strings behaves like single quoted strings. There is no special meaning for escape sequences. Variables are not parsed.
Heredoc
Heredoc string is declared using the doc syntax and double quotes (or without quotes) for the identifier.
PHP Heredoc Example
<?php
$name = 'Hyvor';
$string = <<<STR
I'm a heredoc
I parse variables. (I love $name)
\t \\t adds a tab space. That means I accept escape sequences
STR;
echo '<pre>' . $string . '</pre>';
Run Example ››Heredoc strings behaves like double quoted strings. It converts escape sequences to their real meaning. And, variables will be parsed.