Showing posts with label Echo vs print explained. Show all posts
Showing posts with label Echo vs print explained. Show all posts

Comparing echo and print: Understanding the Differences in PHP

In php echo and print both are used for showing output on the user screen.

Here we are going to discuss some differences between both statements.

1. echo Statement
  • we can write echo statement with parenthesis like 'echo()' or without parenthesis 'echo'.
  • in the echo we can pass multiple variable in comma separated form to see the output like 'echo $a,$b,$c;'
  • echo doesn’t return any value
  • echo is faster then print
      Example:

      1.    <?php
                   echo "hello world";
                  // or
                  echo("hello world");
             ?>
      Output: hello world

      2.    <?php
                   $a = "Hi ";
                   $b = "this is ";
                   $c = "Ap here";
                   echo $a,$b,$c;
             ?>
      Output: Hi this is Ap here.

      3.    <?php
                   $a = "Raj";
                   $name = echo $a;             ?>
      Output: Parse error: syntax error, unexpected T_ECHO
    

2. Print Statement
  • we can write print statement with parenthesis like 'print()' or without parenthesis 'print'.
  • in the print we can not pass multiple variable in comma separated form like echo.
  • print statement always returns 1.
  • print is slower than echo
      Example:

      1.    <?php
                   print "hello world";
                  // or
                  print("hello world");
             ?>
      Output: hello world

      2.    <?php
                   $a = "Hi ";
                   $b = "this is ";
                   $c = "Ap here";
                   print $a,$b,$c;
             ?>
      Output: Parse error: syntax error

      3.    <?php
                   $a = "Raj";
                   $name = print $a;
                   echo $name;
             ?>
      Output: Raj
 
So, If you are having any query and doubt regarding this concept then you can ask in comments. 
Thank You !!!