Vaidikalaya

PHP Echo


PHP echo is a language construct that is used to get output. It is used with or without parentheses: echo or echo(). The echo statement can print the string, multi-line strings, escaping characters, variables, array, and results of expressions, etc.

Below is some usage of echo statements in PHP: 

1. Display Strings:

Example
<?php
    echo "This is Vaidikalaya";
    echo("This is Vaidikalaya");
?>

2. Display Multi-Argument Strings:

Example
<?php
    echo "One ","Two ","Three!";
?>

3. Display Multi-Line Strings:

Example
<?php  
    echo "Hello Vaidikalayans
        This is echo
        with multi-line
    ";  
?>  

4. Display Variables:

Example
<?php

  $text = "Hello Vaidikalayans";
  $num1 = 6;
  $num2 = 3;

  echo $text."\n";
  echo $num1 + $num2;

?> 

5. Display Escaping Characters:

Example
<?php
    echo "PHP stands for \"Hypertext Preprocessor\" ";
?>

Echo Use Cases:

1. Echo does not return any value:

When storing the echo in a variable then it gives a syntax error which proves that the echo has no return any value.

Example
<?php
    $res=echo "Hello Vaidikalaya";
?>

2. Multi-Argument Strings gives error within parentheses:

When using a multi-argument string with parentheses then it gives a syntax error.

Wrong Syntax
<?php
    echo("One ","Two ","Three!");
?>


Right Syntax
<?php
    echo "One ","Two ","Three";
?>