Before starting the Expression of Multiline strings in PHP, we must know What is Multiline String and what is the use of this expression in PHP. Staring is the set of characters, where characters are also known as bytes. This means PHP supports 256 characters set in a string.

Introduction to Multiline String in PHP

Basically, a multiline string means we print the big statement (or set of strings) line by line. For example, if we want to print a variable that defines some set of strings and write it line by line into the code. Check the below example,

Output: This is the
three-line
set of strings.

Here are some easy ways to use the multiline string in PHP,

  1. Use the HEREDOC string method.
  2. By using the concatenation assignment operator.

Note: If you want to check the proper output of the above code by using your local server, you have to run the complete code on CMD or XAMPP Shell. If you do not know how to run PHP code on the Command line, you can check here Execute PHP File From Command Line.

Use HEREDOC String Method to Use Multiline String

PHP has two inbuilt methods HEREDOC and NOWDOC to handle multiline strings. HEREDOC has also syntax <<<.

Here is an example of a multiline PHP string using HEREDOC,

Output:

Example of Multiline String in PHP
Example of Multiline String in PHP

By Using the Concatenation Assignment Operator

In the above code, we use PHP_EOL for the next line. PHP_EOL stands for PHP End of line that is majorly used for changing the line of the starting. To check the output of the above code you have to run it on the command line of your OS.

Output:

Example of PHP Concatenation Operator
Example of PHP Concatenation Operator

This is the complete reference of PHP multi-line String with syntax and examples. The above codes are also shown us how we can define a multi-line string in PHP script.

If you get any issues please let me know in the comment.

You can check more about it from PHP’s official site PHP: Strings – Manual.

Some bonus points,

If you run it on the browser it will not be working properly.

EOD = End Of Data,
EOT = End of Text

Also Read:

Happy Coding..!

Was this article helpful?
YesNo

By Bikash

My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP.

2 thoughts on “How to Use Expression of Multiline String in PHP”

Leave a Reply

Your email address will not be published. Required fields are marked *