-2

Could you explain why 'print' in PHP return 1?

For what? Was it the result of simple creativity? Or maybe, "we have created, and you can do with that whatever you wish"?

For example,

$a = print('4');
var_dump($a);
  • What are you trying to do? What do you expect `$a` to contain? Maybe question should be dup of https://stackoverflow.com/questions/16709267/print-always-returns-1-is-there-a-practical-use – user3783243 May 24 '20 at 14:30

1 Answers1

1

From the docs

Returns 1, always.

Why Should I use print? What kind of Advantage?

Both print and echo are language constructs and echo is even faster than print. But the advantage of print is that it is more flexible than echo and it can do anything that echo can do.

The simplest example is, print can be used in an expression like ternary.

($someValue) ? print('somevalue is True') : print('someValue is false');

This is because as I stated above print always returns 1 and echo doesn't have a return value.

Read Official Documentation Here:

PHP print

PHP echo

Harish ST
  • 1,475
  • 9
  • 23