PHP String

PHP String is a collection of characters. A string can be consist of alphanumeric characters and used to manipulate text. String is one of the predefined datatype supported by PHP like most of the programming language.

PHP String can hold any characters, letters, numbers, or even special characters. There are 4 ways to specify string in PHP.

  • Single Quoted.
  • Double Quoted.
  • heredoc
  • newdoc

Single Quoted

This is the simplest way to define a string in PHP. In this case, you have to declare text by enclosing single quotes.

You can use escape to specify a literal single quote, use double backslash to specify literal backslash. Other escape characters are as below.

  • \n is replaced by the newline character
  • \r is replaced by the carriage-return character
  • \t is replaced by the tab character
  • \$ is replaced by the dollar sign itself ($)
  • \” is replaced by a single double-quote (“)
  • \’ is replaced by a single double-quote (‘)
  • \\ is replaced by a single backslash (\)

Let’s understand the concept with the help of the following examples.

Example 1

<?php
$my_str='Welcome to Tutorialsbook!';
echo $my_str;
?>

Output

Welcome to Tutorialsbook!

You can store multi-line text, double quotes, and escape sequences in a single quoted PHP string.

Example 2

<?php
$my_str1='Tutorialsbook. Here,
You can find tutorials
on different topics.';
$my_str2='You can use double "quote" directly inside single quoted string';
$my_str3='Escape sequence \n in single quoted string';
echo "$my_str1 <br/> $my_str2 <br/> $my_str3";
?>

Output

Tutorialsbook. Here, You can find tutorials on different topics.
You can use double “quote” directly inside single quoted string
Escape sequence \n in single quoted string

Example 3

<?php
$num=20;
$my_str1='Example of \'single quote\' and \\backslash inside single quoted string';
$my_str2='Example of variable <br/> Even number: $num';
echo "$my_str1 <br/> $my_str2";
?>

Output

Example of ‘single quote’ and \backslash inside single quoted string
Example of variable
Even number: $num

Double Quoted

Another way to define string in PHP is to specify text enclosing with double quotes. In this case, escape sequences and variables are interpretable.

Example

<?php
$num=10;
$str1="Welcome to Tutorialsbook!";
$str2="Tutorialsbook. Here,
You can find tutorials
on different topics.";
$str3="Escape sequence \n in double quoted string";
$str4="Example of variable <br/> Even number: $num";
echo "$str1 <br/> $str2 <br/> $str3 <br/> $str4";
?>

Output

Welcome to Tutorialsbook!
Tutorialsbook. Here, You can find tutorials on different topics.
Escape sequence in double quoted string
Example of variable
Even number: 10

You cannot use double quotes directly inside double quoted string.

Example

<?php
$str="You can use double "quote" directly inside single quoted string";
echo "$str";
?>

Output

Parse error: syntax error, unexpected ‘quote’ (T_STRING) in C:\xampp\htdocs\string.php on line 2

PHP Heredoc

PHP Heredoc is the third way to specify string. This methodology is generally used to generate more complex string compared to double-quotes.

In heredoc syntax, an identifier is used after heredoc (<<<) operator and text are written immediately after the new line. And the string is ended by the same identifier by which it was started.

You have to remember that the closing identifier must begin from the new line without any whitespace or tab.

In heredoc, you can use double quotes inside string without escaping them.

Example

<?php

$tutorial = "PHP & MYSQL";

echo <<<TXT

Welcome to Tutorialsbook.
Here you can find tutorials on $tutorial.

TXT;

?>

Output

Welcome to Tutorialsbook. Here you can find tutorials on PHP & MYSQL.

PHP Newdoc

PHP Newdoc is similar to the heredoc but works similarly to the single-quoted string.

Any kind of parsing is not done in the case of newdoc.

The Newdoc also defined using three less than symbol (<<<) and followed by an identifier which is enclosed with a single quote.

The main difference between Heredoc and Newdoc is that – Newdoc is a single-quoted string whereas Heredoc is a double-quoted string.

Example

<?php

$tutorial = "PHP & MYSQL";

echo <<<'TXT'

Welcome to Tutorialsbook.
Here you can find tutorials on $tutorial.

TXT;

?>

Output

Welcome to Tutorialsbook. Here you can find tutorials on $tutorial.

Please get connected & share!

Advertisement