PHP String
PHP string is a sequence of characters used to store and manipulate text. PHP supports only a 256-character set and does not offer native Unicode support. There are four ways to specify a string literal in PHP:
- Single quoted
- Double quoted
- Heredoc syntax
- Newdoc syntax (since PHP 5.3)
Single Quoted
A string in PHP can be created by enclosing the text in single quotes. To specify a literal single quote, use a backslash (), and for a literal backslash, use double backslashes (
). Other escape sequences like
n
or t
will not be interpreted.
We can store multiple line text, special characters, and escape sequences in a single-quoted PHP string.
Examlpe 1
Addition Form
<?php
$str = 'Hello text within single quote';
echo $str;
?>
Output
Examlpe 2
<?php
$str1 = 'Hello text
multiple line
text within single quoted string';
$str2 = 'Using double "quote" directly inside single quoted string';
$str3 = 'Using escape sequences n in single quoted string';
echo "$str1 <br/> $str2 <br/> $str3";
?>
Output
Examlpe 3
<?php
$num1=10;
$str1='trying variable $num1';
$str2='trying backslash n and backslash t inside single quoted string n t';
$str3='Using single quote 'my quote' and backslash';
echo "$str1
$str2
$str3";
?>
Output
Double Quoted
Double-quoted strings allow escape sequences and variable parsing.
Examlpe 1
<?php
$str="Hello text within double quote";
echo $str;
?>
Output
Now, you can't use double quote directly inside double quoted string.
Examlpe 2
<?php
$str1="Using double "quote" directly inside double quoted string";
echo $str1;
?>
Output
We can store multiple line text, special characters and escape sequences in a double quoted PHP string.
Examlpe 3
<?php
?php
$str1="Hello text
multiple line
text within double quoted string";
$str2="Using double "quote" with backslash inside double quoted string";
$str3="Using escape sequences n in double quoted string";
echo "$str1 <br/> $str2 <br/> $str3";
?>
Output
In double quoted strings,variable will be interpreted.
Example 4
<?php
$num1=10;
echo "Number is: $num1";
?>
Output
Heredoc
Heredoc syntax (<<<) is the third way to delimit strings. In Heredoc syntax, an identifier is provided after this heredoc <<< operator, and immediately a new line is started to write any text. To close the quotation, the string follows itself and then again that same identifier is provided. That closing identifier must begin from the new line without any whitespace or tab.
Naming Rules
The identifier should follow the naming rule that it must contain only alphanumeric characters and underscores, and must start with an underscore or a non-digit character.
For Example
Valid Example
<?php
$str = < < <Demo
It is a valid example
Demo; //valid code as whitespace or tab is not valid before closing identifier
echo $str;
?>
Output
We cannot use any whitespace or tab before and after the identifier and semicolon, which means identifier must not be indented. The identifier must begin from the new line.
Invalid Example
<?php
$str = < < <Demo
It is Invalid example
Demo; //Invalid code as whitespace or tab is not valid before closing identifier
echo $str;
?>
This code will generate an error.
Output
Heredoc is similar to the double-quoted string, without the double quote, means that quote in a heredoc are not required. It can also print the variable's value.
Example
<?php
$str =< < <Demo
It is the example
of multiple
lines of text.
DEMO;
echo $str;
echo '</br>';
echo < <Demo // Here we are not storing string content in variable str.
It is the example
of multiple
lines of text.
DEMO;
?>
Output
Below are the example with class and their variable
<?php
class heredocExample{
var $demo;
var $example;
function __construct()
{
$this->demo = 'DEMO';
$this->example = array('Example1', 'Example2', 'Example3');
}
}
$heredocExample = new heredocExample();
$name = 'Gunjan';
echo < < <ECO
My name is "$name". I am printing some $heredocExample->demo example.
Now, I am printing {$heredocExample->example[1]}.
It will print a capital 'A': x41
ECO;
?>
?>