YogeshChauhan . com

2 Ways We Can Write Multiple Line Commands in PHP

in PHP on December 30, 2019

When we think about printing something in PHP, we think about echo or print and most of the time, in fact all the time that's what we use. But PHP has so many built in functionalities that we might just wonder about it's scalability.

There will be times in our projects when we need to type lot of texts and lengthy sentences and in those times, using echo or print becomes a bit messy and time-consuming as well. 

So, in this very short article I'll show you how to write multiple line commands.

To write many lines of code, PHP offers two solutions. 

  1. Just write down all those lines into quotes and print it.
  2. Use heredoc (multiline sequence using <<< operator)

Let's look at the examples one by one.

The first one, you might have used it many times. Let's see.


//code inside PHP file

$author = "Albert Einstein";

echo "I have no special talent. I am only passionately curious.

- $author.";

//output

I have no special talent. I am only passionately curious.

- Albert Einstein.

Just put everything in between the quotes and your lines will be printed just fine.

The second one is the one we don't use much.

PHP has multiline sequence functionality which uses <<< operator. It is known as here-document or heredoc.

It is really helpful as it has many advantages. For example,

  • It specifies the string literal.
  • It keeps the line breaks as it is
  • It also keeps the other white spaces and indentation as it is.

Unlike quotes, we can use single and double quotes in heredoc without any constraints, without escaping them with slash().

Let's look at the code.


//code inside PHP file

$author = "William Shakespeare ";

echo <<<_END
Wisely, and slow. They stumble that run fast.

- $author.
_END;

//output

Wisely, and slow. They stumble that run fast.
  
- William Shakespeare.

In the code above, as you can see, all we need is <<<_END right after echo and then type everything you want in the output and make sure you leave only requires white spaces and indentation. After that just add _END at the end and you're done.

This code <<<_END and _END tells PHP to print everything between those tags. In other words, it works same as quotes in the first example except that quotes in this code don not need to be escaped.

That means we can write down all those HTML code into PHP using this option and wee can also just replace the dynamic variables and the output will be same.

REMEMBER: Closing the _END; tag must be placed in the new line and nothing should be written in that line, not even comments, not even a single space.

dreamhost

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to uninstall Cocoapods from the Mac OS? #4 How to add Read More Read Less Button using JavaScript? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

May 5 Use inline if to make a shorter conditional syntax in React May 4 What is Conditional Rendering in React? May 3 How does @extend rule work in SCSS (Sass)? May 2 How different is Handling Events in React vs HTML DOM? May 2 How to create bouncing balls using HTML canvas and JavaScript? Apr 30 HTML canvas methods

You might also like these

IN and BETWEEN Operators in SQLSQL/MySQLHow to create a flip effect with CSS?CSSHow to enable and disable button using JavaScript?JavaScriptA Quick Comparison of JOIN and Subquery in SQLSQL/MySQLShould we ever delete data from a database?MiscHow to get start index and end index (as int) of substring in Swift?Swift