Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use echo or print in php scripts?

Tags:

php

echo

printing

Which statement should I use in php scripts? Echo or Print? What is faster and mostly used? Thanks in advance.

like image 469
james Avatar asked Jul 29 '11 20:07

james


People also ask

What can I use instead of echo in PHP?

The PHP print Statement You can also use the print statement (an alternative to echo ) to display output to the browser. Like echo the print is also a language construct not a real function. So you can also use it without parentheses like: print or print() .

Does echo work in PHP?

In PHP, you will use echo to output one or more expressions. It's important to understand that echo is not a function but is a language construct. The arguments echo accepts are a list of expressions separated by commas. Unlike other language constructs, echo does not have a return value.

Why do we use echo in PHP?

PHP echo statement can be used to print the string, multi-line strings, escaping characters, variable, array, etc. Some important points that you must know about the echo statement are: echo is a statement, which is used to display the output.

What is used to display the output in PHP?

echo is a statement, which is used to display the output. echo can be used with or without parentheses. echo does not return any value.


1 Answers

Both echo and print are language constructs of PHP (not functions). Which is better depends on your priorities. There are three possible priorities I would consider: 1. you mentioned speed; 2. you mentioned widespread usage; 3. I would add flexibility.

  1. Speed: as many others have mentioned, echo is slightly faster (especially when using the multiple-argument syntax, with elements separated by commas), but the difference is so minor that it only matters in code with thousands of loops where speed really, really matters. See http://www.phpbench.com for benchmarks.

  2. Widespread usage: it seems that out of tradition, echo is more popularly used for PHP than is print. This is quite anecdotal, but I think you'll run into the same conclusion when you read PHP code from a wide variety of sources.

  3. Flexibility: I believe print is definitely more flexible than echo in expressing code. Echo has only one "advantage" over print: you can use the following syntax: echo $arg1, $arg2, ... using commas to list your arguments; print does not support the comma syntax. However, you can replace the commas with periods (.) and get exactly the same result in both echo and print: print $arg1. $arg2. .... Thus, this syntax provides zero advantage in flexibility and expression. It is a slight advantage because it results in faster code, as I mentioned in #1, but in 99% of code, this probably doesn't matter.

    In contrast, the one thing that print can do that echo cannot do is to return a value, and so it can fully act as a function. On one hand, it is limited because print always returns a value of 1, no matter what. On the other hand, you can do this with print, but not with echo:

    <?php ($age >= 18) ? print('Can vote.') : print('Cannot vote.'); ?>

    (Example taken from Murach's PHP and MySQL 2010, p. 227)

    Thus, print can express virtually all the same flexibility in code as echo, but echo has one significant use case where it cannot do what print can do: print can act as a function in contexts where this might be useful. (I say "act as a function" because it is not a function; it is a language construct, just like echo.)

    As for the shorthand echo syntax <?=$foo?> (<?php=$foo?> also works as of PHP 5.4: http://us2.php.net/manual/en/function.echo.php), it might be called a shorthand for "echo", but you could just as well call it a shorthand for "print" because it's just a different language construct. There is no logical basis of calling this an "advantage" of echo over print, as some people claim, since this construct is neither an echo nor a print--it is an alternate construct that does the same thing as both.

For me personally, I prefer to pick one and stick with it always. I personally prefer print because of its slightly superior advantage in coding flexibility, and because "print" sounds more intuitive to me--this is purely subjective. I don't care that echo is probably more widely used, because print is equally well understood if others need to read my code. For the 1% of code where print speed really, really matters, then I'll use echo.

like image 69
Tripartio Avatar answered Oct 18 '22 19:10

Tripartio