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,
1 2 3 4 5 6 |
<?php $s = "This is the three-line set of strings."; echo $s; ?> |
Output: This is the
three-line
set of strings.
Here are some easy ways to use the multiline string in PHP,
- Use the HEREDOC string method.
- 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,
1 2 3 4 5 6 7 |
<?php echo <<<EOT My name is BB. I am printing some Text. Now, I am printing SECOND LINE. This should print a capital 'A': \x41 EOT; ?> |
Output:
By Using the Concatenation Assignment Operator
1 2 3 4 5 6 7 8 9 |
<?php $mystring1 = "This is the first line." . PHP_EOL; $mystring2 = "This is the second line" . PHP_EOL; $mystring3 = "This is the third line" . PHP_EOL; $mystring4 = "This is the fourth line" . PHP_EOL; $mystring5 = "This is the fifth line"; $mystring1 .= $mystring2 .= $mystring3 .= $mystring4 .= $mystring5; echo($mystring1); ?> |
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:
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:
- Bootstrap Datepicker- Complete Solution to Integrate
- Concept of CSS Grid Layout With Example
- How to Include Comments in CSS
- Best User Login Page Design Using Neumorphism
Happy Coding..!
[…] How to Use Expression of Multiline String in PHP […]
[…] How to Use Expression of Multiline String in PHP […]