PHP echo statement

The PHP echo statement is used to output some data on the screen or on the web. For example:

<?php
   echo "fresherearth.com";
?>

The output of the above PHP example is:

php echo

The same (previous) example can also be created in this way:

<?php
   $txt = "fresherearth.com";
   echo $txt;
?>

Let me create the same example in another way:

<?php
   $txt = "fresherearth.com";
   echo($txt);
?>

It produces exactly the same output as the previous example because ($txt) is evaluated as ("fresherearth.com"), which is a valid expression.

Note: But let me tell you one thing: use parentheses only when you need to give priority to an expression being executed first. For example:

<?php
   echo(1+4) * 8;
?>

The output of the above PHP example is:

php echo statement

In the above example, if you remove the parentheses, then the output should be 33. This is the situation where we need to use parentheses. Otherwise, "echo" does not require any parentheses to output the data on screen.

PHP echo variable

<?php
   $x = 10;
   echo $x;
   echo "<hr>";
   
   $x = 12.43;
   echo $x;
   echo "<hr>";
   
   $x = "Hey!";
   echo $x;
   echo "<hr>";
   
   $x = "Hey,<br>What's Up?";
   echo $x;
?>

The output of the above PHP example is:

php echo example

PHP echo HTML

<?php
   echo "<h1>About Me</h1>";
   echo "<p>Hey!<br>I am Lucas from Frankfurt, Germany.</p>";
   echo "<p>I am able to handle multiple tasks on a daily basis.</p>";
?>

The output of the above PHP example is:

php echo statement example

PHP echo with multiple parameters

<?php
   echo "I ", "use", " a creative approach to solve a problem";
   $x = "use";
   $y = "a creative approach to solve a problem";
   
   echo "<br>";
   echo "I $x $y";
   
   echo "<br>";
   $a = "I ";
   $b = "use";
   echo $a . $b . $y;
   
   echo "<br>";
   echo $a . $b . " " . $y;
   
   echo "<br>";
   echo $a, $b, " ", $y;
   
   $x = 10;
   $y = 20;
   $z = 25;
   echo "<br>";
   echo $x + $y - $z;
?>

The output of the above PHP example is:

echo statement in php

PHP echo statement example

Let me create one more example of an echo statement in PHP that may enhance your skill towards echo.

<?php
   echo "codes";
   echo "cracker";
   echo "<hr>";
   echo "codes", "cracker";
   echo "<hr>";
   echo "Hey,
      I just love
      coding.
      I hope you too.";
   echo "<hr>";
   echo "The value is: ", 2*43;
?>

The output of the above PHP example is:

echo php

Advantages of echo statement in PHP

Disadvantages of echo statement in PHP

PHP Online Test


« Previous Tutorial Next Tutorial »