php echo and print differences

There are some differences between echo and print in php, echo and print are not functions.
First, you can pass more than one parameter to echo, print doesn’t allow more than one parameter.
Example:

  1. <?php
  2. $a = 5;
  3. $b = 6;
  4.  
  5. echo $a; // will output 5
  6. echo $a, $6; // outputs 56
  7.  
  8. print $a; //will output 5
  9. print $a, $b; // error, print doesn't allow more than one parameter
  10. ?>

Now, let’s see something interesting…

  1. <?php
  2. $a = 5;
  3. echo " \$a is:" . print $a; // will output 5 $a is 1
  4. ?>

First, you use \ (backslash) character to treat $a as a string not variable. Then, this is important, the code will be executed in this manner: first is executed print then echo. You may ask but why it will output “1″… Let’s see the next example:
Remember that print($arg) will return 1!

  1. <?php
  2. $a = 5;
  3. echo (print($a)); // will output 51
  4. ?>

As you can see, it will output 51 because php print is executed first (5) and with echo returns 1.

No related php tutorials.

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark
tabs-top

Leave a Reply