PHP echo Statement

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

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

The output of above PHP example, is:

php echo

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

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

Let me create the same example in another way:

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

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

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

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

The output of above PHP example, is:

php echo statement

In above example, if you will remove the parentheses, then the output should be 33. This is the situation where we need to use the parentheses. Otherwise, echo does not requires 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 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 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 above PHP example, is:

echo statement in php

PHP echo Statement Example

Let me create one more example of 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 above PHP example, is:

echo php

PHP Online Test


« Previous Tutorial Next Tutorial »

Follow/Like Us on Facebook




Subscribe Us on YouTube