YogeshChauhan . com

The Difference Between the echo and print Commands in PHP

in PHP on January 18, 2020

We have seen echo command used in number of different ways to print everything from PHP files on the server to the browser. Outputs might have been different. For example, you might have just printed simple texts or maybe string concatenation or some variables, maybe output in quotes using multiple lines or using heredoc.

In PHP, there are two ways to print. print and echo. But most of the time we just use echo and not print. Both of them are similar types of commands but still there is a bit difference (otherwise we won't need two commands. right?)

The command print is a function-like construct that takes a single parameter and has a return value. The return value is always 1 by the way. The command echo is a pure PHP language construct.

Both of the commands are constructs so none of them requires parenthesis. 

The echo command will be somewhat faster then print in text output and the reason is obvious: it doesn't set a return value.

The echo command can't be used as a part of more complex expression because it isn't implemented like a function. The print command can be used as a part of more complex expression.

Let's look at the example below:


$var ? print "TRUE" : print "FALSE";

The statement prints whether the value is true or false using print. Something that we can not perform using echo. However there is a way to perform same king of operation using echo. For example, just use shorthand to write IF condition and use echo.

In the print example above, the question mark checks whether the value of $var is true. The colon just divides the outputs to be printed in either condition. If $var is TRUE then the left part of the colon will be printed and if it's FALSE then the right part.

Summary

echo and print are more or less the same. They are both used to output data to the screen.

The differences are small:

enlightened echo has no return value while print has a return value of 1 so it can be used in expressions.

enlightened echo can take multiple parameters (although such usage is rare) while print can take one argument.

enlightened echo is marginally faster than print.

Credit: https://www.w3schools.com/php/php_echo_print.asp

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 7 How to disable right click and drag and drop of images using jQuery? May 7 How to render Lists in React? May 7 What’s the difference between variables in CSS and SCSS (Sass)? May 7 How to define variables in SCSS (Sass)? May 7 How to show and hide an element on click in React? May 5 Use inline if to make a shorter conditional syntax in React

You might also like these

SQL ANY OperatorSQL/MySQLHow services and dependency injection work in Angular?AngularHow to change perception and behavior of a person for acceptance of new systems?MiscSolution to Could not cast value of type ‘NSTaggedPointerString’ to ‘NSNumber’SwiftHow to get previous days or next days in PHP?PHPIs Software Really Eating The World? I don’t think soMisc