Yogesh Chauhan's Blog

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.


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 add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How to calculate elapsed time in JavaScript?JavaScriptWhat happened to your Google Analytics Tracking ID, can’t find it?MiscellaneousWhat are Identifiers in JavaScript?JavaScriptinclude, include_once, require, require_once in PHPPHPImplicit and Explicit Joins in Oracle SQLSQL/MySQLAdd animation to your skills bar using CSSCSS