PHP

PHP String

A string is any series of characters enclosed in single (‘) or double (“) quotes, or that you create using special heredoc or nowdoc syntax:

Single quoted string

Single quoted string is defined using the single quote character:

$user = 'Guest';
$welcome = 'Hello $user';
echo $welcome;
//Hello $user

If you need to use single quote or backslash within a string, escape them with a backslash:

$a = 'Someone\'s property';
echo $a;
//Someone's property

$b = 'Trailing backslash\\';
echo $b;
//Trailing backslash\

Double quoted string

Double quoted strings behave similarly to strings enclosed in single quotes but they can return the value of another variable and interpret the special characters, such as new line \n :

$welcome = "Hello $user \nThis will print on next line";
echo $welcome;
/*
Hello Guest
This will print on next line
/*

Single vs double quotes string

The double quoted strings evaluate the value of variables but single quoted strings store whole text as is:

$user = 'Guest';
echo 'Hello $user';
//Hello $user

echo "Hello $user";
//Hello Guest

Double quoted string evaluates special escape characters but single quoted string not:

echo 'hello \n\n world';
//hello \n\n world

echo "hello \n\n world";
/*hello

world*/

Single quoted strings need to escape apostrophe but double quoted string not:

echo 'User\'s profile';
//User's profile

echo "User's profile";
//User's profile

Double quoted strings need to escape double quotes " but single quoted string not:

echo 'Who says, "Hello!".';
//Who says, "Hello!".

echo "Who says, \"Hello!\".";
//Who says, "Hello!".

Both single and double quoted strings need to escape a backslash if followed by special character:

echo '\Backslash \\\' and apostrophe';
//\Backslash \' and apostrophe

echo "\Backslash and \\\" quotes";
//\Backslash and \" quotes

Heredoc string

The heredoc string begin with the <<< operator followed by an identifier and a new line. The identifier can be any combination of alphanumeric characters or underscores that don’t begin with a digit. To close heredoc string write the identifier on a new line followed by a semicolon.

$user = 'Guest';

$body = <<<FIRSTPARA
Welcome back "$user". \n\n
FIRSTPARA;

echo $body;
//Welcome back "Guest".

Heredoc string behaves identically to double quoted strings, except that there is no need to escape quotes in the string itself.

Nowdoc string (PHP 5.3 or higher)

In PHP 5.3 you can create strings using nowdoc syntax which is similar to heredoc syntax, the only difference is that the delimiter is enclosed within single quotes, variables are not parsed inside a nowdoc string:

$body = <<<'LABLE'
Welcome back "$user".\n\n
LABLE;

echo $body;
//Welcome back "$user".\n\n

The nowdoc syntax is useful if you want to embed a block of PHP code within your script, without the code being processed at all.

Heredoc vs Nowdoc

Heredoc is equivalent to double quoted string:

  • Variable names are replaced with variable values
  • Evaluates special characters

Nowdoc works similarly to single quoted string:

  • Variable names remain intact
  • No escaping takes place
  • The string is used entirely as is.

Special / escape characters

\n new line (line feed or LF)
Used as a new line character in Unix and Mac OS X.

\r carriage return (CR)
It means to return to the beginning of the current line without advancing downward. It was used as a new line character in Mac OS before X.

\r\n (CR+LF)
Used as a new line character in Windows.

\$ dollar sign
You need to escape $ character in double quoted and heredoc strings. For example:

$hw = "Hello world";
echo "The \$hw value is $hw";
//The $hw value is Hello world

\' single quote
To escape apostrophe in single quote or nowdoc string. For example $a = 'User\'s';

\" double quote
Use with double quoted string to escape double quotation, for example: $b = "Cold \"Coffee\".";

\\ backslash
Escape backslash if followed by escape character. For example: echo '\Backslash \\\' and apostrophe';or echo "\Backslash and \\\" quotes";

\u{} Unicode characters
Introduced in PHP 7. It’s now possible to write Unicode characters easily by using a double quoted or a heredoc string, without calling any function. For example:

echo "\u{25B6}";
//Prints: ▶

echo "\u{2117}";
//Prints: ℗

echo "\u{00A9}";
//Prints: ©

\t horizontal tab
The tab key on a keyboard.

\v vertical tab
The vertical tab is so rare nowadays. It was used to speed up printer vertical movement.

\e escape
If you make a PHP script which is meant to run from the command line, \e can come in handy when you want to do colors and formatting in terminal emulators.

\f form feed
it is a page separating character. It forces the printer to eject the current page and to continue printing at the top of another.

Advertisement:
Advertisement: