In PHP, the multiline string is very easy they can be written in three different ways, such as with the use of escape sequences
, Nowdoc and Heredoc
syntax and concatenation assignment operator
.
In this article, we will talk about how to Multiline a string in PHP with a detailed explanation as well as example programs that could be helpful to your project development. This topic is a continuation of the previous article, entitled PHP basename Function.
How do I create a multiline string in PHP?
The following list below can be used to create a multiline string
:
- Escape sequences
- Nowdoc and Heredoc syntax
- Concatenation assignment operator
PHP Multiline a string using escape sequences
The escape sequences
is a way to output special characters and the backslash n (\n) escape sequences can also be used to produce the multiline string.
Example
<?php //declaring multiple lines using the new line escape sequence $name="Glenn\nMagada\nAzuelo"; echo $name; ?>
Output
Glenn Magada Azuelo
PHP Multiline a string using a Nowdoc and Heredoc syntax
The Heredoc and Nowdoc
syntax in PHP can also be used to write and output multi line strings in PHP directly. But still, there are differences in Heredoc and Nowdoc syntax. The Heredoc
uses double quoted strings
“double” and the parsing of the escape sequences is done inside the Heredoc text. While a Nowdoc
uses a single quoted strings
‘single’ and the parsing of the escape sequence is not performed.
Reminder: The Heredoc and Nowdoc
syntax delimiters should always be at the beginning of the single line, without any spacing or special characters.
Heredoc example
<?php $site = "ITSOURCECODE"; $heredoc1 = <<<HEREA Free source code by, $site Python projects. Java projects and any other programming projects for free."\n" HEREA; $heredoc2 = <<<HEREB Do you have a tutorial, $site? Yes, we have We provide free source code and tutorial. HEREB; echo $heredoc1; echo $heredoc2; ?>
Output
Free source code by, ITSOURCECODE Python projects. Java projects and any other programming projects for free." "Do you have a tutorial, ITSOURCECODE? Yes, we have We provide free source code and tutorial.
Nowdoc example
<?php $site = "ITSOURCECODE"; $nowdoc1 = <<<HEREA Free source code by, $site Python projects. Java projects and any other programming projects for free."\n" HEREA; $nowdoc2 = <<<HEREB Do you have a tutorial, $site? Yes, we have We provide free source code and tutorial. HEREB; echo $nowdoc1; echo $nowdoc2; ?>
Output
Free source code by, ITSOURCECODE Python projects. Java projects and any other programming projects for free." "Do you have a tutorial, ITSOURCECODE? Yes, we have We provide free source code and tutorial.
PHP Multiline a string using concatenation assignment operator
The concatenation assignment operator
(=) can also be used to multiline a string. We just need to concatenate the two strings and the PHP_EOL syntax
so that we can mark the end line of the string.
Example
<?php $fname="Glenn". PHP_EOL;//PHP_EOL marks end of line so that $mname="Magada". PHP_EOL;//next string get concatenated as new line and closing identifier $lname="Azuelo"; $fname.=$mname.=$lname;//concatenating the string into $s1 echo $fname;/PHP code for printing final concatenated string ?>
Output
Glenn Magada Azuelo
Related Articles
- How To Find The Length Of A String In PHP With Example
- PHP Substring Function With Examples
- PHP Convert to String with Example
- PHP String (with Program Examples )
Summary
In summary, you have learned about PHP Multiline String
. This article also discussed on how to create a multiline string, and three different ways to multiline a string such as escape sequences, Nowdoc and Heredoc syntax and concatenation assignment operator.
I hope this lesson has helped you learn a lot. Check out my previous and latest articles for more life-changing tutorials which could help you a lot.