Free Software,Free Society

Monday 25 May 2015

The DifferenceBetween the echo and print Commands

No comments :


Generally you have seen the echo command used in a number of different ways to output text from the server to your browser. In some cases, a string literal has been output.
In others, strings have first been concatenated or variables have been evaluated.


But there is also an alternative to  echo that you can use:  print. The two commands are quite similar, but  print is a function-like construct that takes a single parameter and has a return value (which is always  1), where as  echo is purely a PHP language construct. 
Since both commands are constructs, neither requires parentheses.
By and large, the  echo command will be a tad faster than  print  in general text output, because it doesn’t set a return value. On the other hand, because it isn’t implemented like a function,  echo cannot be used as part of a more complex expression, whereas  print can. Here’s an example to output whether the value of a variable is TRUE or  FALSE using  print, something you could not perform in the same manner with echo, because it would display a Parse error message:

$b ? print "TRUE" : print "FALSE";

The question mark is simply a way of interrogating whether variable  $b  is  TRUE  or FALSE. Whichever command is on the left of the following colon is executed if  $b  is TRUE, whereas the command to the right is executed if $bis FALSE.

I generally, use echo, and I recommend that you do so as well until you reach such a point in your PHP development that you discover the need for using print.

No comments :

Post a Comment