PHP: Is there an advantage to using echo instead of printing?

Possible duplicate:
How are they different from echo and print in PHP?

As far as I know, the difference between printand echois that it printreturns a boolean value. So whenever I use echo, I can use print. However, in all the code examples I've seen so far (I'm learning PHP), they used echo. Why is this?

EDIT: Maybe the reason is echofaster than print(because it printreturns a value, but echonot)? Although, I think, the difference in speed is not noticeable.

+3
source share
6 answers

print , echo echo ( ). :

, echo to output , print, . echo , print.

alt text

+6

, , , , .

: http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40

1.

  Speed. There is a difference between the two, but speed-wise it should be irrelevant which one you use. echo is marginally faster since it doesn't set a return value if you really want to get down to the nitty gritty.

2.

  Expression. print() behaves like a function in that you can do: $ret = print "Hello World"; And $ret will be 1. That means that print can be used as part of a more complex expression where echo cannot. An example from the PHP Manual:

  $b ? print "true" : print "false";

print , , . , . "," AND, OR XOR .

  • (). : echo expression [, expression [, expression]...] echo (, ) . : echo ( "howdy" ), ( "" ); : echo "howdy", "partner"; ( , .)

, , :

   echo  "and a ", 1, 2, 3;   // comma-separated without parentheses
   echo ("and a 123");        // just one parameter with parentheses

print() :

   print ("and a 123");
   print  "and a 123";
+3

echo , print, .

+2

. . 1, , , , . (4 5).

+1

.

echo , print .

+1

- , , . , . :

  • print , echo . , print : ($foo == true) ? print 'true' : $foo = true, echo
  • echocan take several arguments separated by commas, but printcannot. For example, you would doecho "hello", "world";
  • print always "returns" value 1
+1
source

Source: https://habr.com/ru/post/1785502/


All Articles