How To Best Utilize Echo In PHP?

Published on Sep 09,2019 5.5K Views

How To Best Utilize Echo In PHP?

edureka.co

Echo In PHP, is a language construct not a function, so you don’t need to use parenthesis with it. In this article we will explore this concept in detail. We would focus on following two pointers,

Table of Content

So let us get started in then,

Echo In PHP

In order to output one or more strings, we can use echo statement. Anything that can be displayed to the browser using echo statement, such as string, numbers, variables values, the results of expressions, etc. It is required to use parenthesis if you want to use more than one parameter. Unlike some other language constructs echo does not behave like a function, so it cannot always be used in the context of a function. The end of echo statement is identified by the semi-colon (‘;’). 

Below example demonstrates the basic usage of echo in PHP

<?php
$content = "PHP course";
$num=50;
$num1=60;
$char='c';
echo $content."<br>";
echo $num."+".$num1."=";
echo $num + $num1."<br>";
echo "this is my statement";
?>&nbsp;

Below example demonstrates the example of using HTML code using PHP

<?php
echo "<h1>This is html.</h1>";
echo "<h2 style='color: blue;'>This is heading with style.</h4>";
?>

Moving on with this article on Echo In PHP

Echo vs Print

echo is similar to print where both are used to output data to the screen. Some of main differences are: 

echo

print

Type

Echo is a type of output string

Print is also a type of output string

Parenthesis

Basically, it is written with parenthesis

It can and cannot be written with parenthesis

argument

echo  ($arg1[,$arg2..])

print($arg)

Functionality

It is faster than print

It is slower than echo

Strings

It can output one or more strings 

It can output only one string at a time and returns 1

As we have already seen the demonstration of echo above, let us see an example of print in PHP

<?php
$content = "PHP course";
$num=50;
$num1=60;
$char='c';
print $content."<br>";
print $num."+".$num1."=";
print $num + $num1."<br>";
print "this is my statement";
?>&nbsp;

With this we come to an end of this article on Echo In PHP. I hope you have learned about echo in PHP and the comparison between echo and print in PHP. If you found this split in PHP blog relevant, check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section and I will get back to you.

BROWSE COURSES