14

I know echo() and print() do the same thing. but print has a return value of (int) 1.

The question is:

  1. Why it always returning (int) 1 ?
  2. What we can do with returning (int) 1 ?
3

6 Answers 6

11

print is a function and can be used in expressions, while echo is a language construct that can only be used at the start of the line.

 echo print(print(print(print(123)))), print(4); 

The real use case for having print available as a function is to allow it to be used in expressions. For example as debug feature:

 if (($a == $b) and print("It's true") and $c) { 

Or even

 return TRUE and print("right"); 
Sign up to request clarification or add additional context in comments.

3 Comments

"print() is not actually a real function (it is a language construct)" -- PHP Manual
True. It only imitates the behaviour of a function (not e.g. usable as callback). And it's parsed even without parenthesis around the argument.
hmmm seem like this is the logical reason for (int) 1, thank you very much to give real use case example :)
6

Why it always returning (int) 1 ?

Most likely to signify success, so you could interpret it as the value TRUE.

What we can do with returning (int) 1 ?

In future code, instead of doing

$i++; 

you can do

$i = $i + print("Hello World!\n"); 

(Minor side-effects may apply.)

2 Comments

I'd go for $i += print('bohaaa!');
Why in the world would you want to increment a variable with the result of print ?
3

As others already mentioned, print is pseudo-function (returns a value but not a real function), which makes its use valid in expressions. So you can write quirky code like this to confuse the maintainers :)

$success = doSomethingThatCanPossiblyFail(); if ($success || !(print "Failed to do that! Not going to do the follow up")){ //success nowDoTheFollowupThing(); } 

Just make sure the maintainers don't know where you live

Comments

2

It's just a little feature that allows you to use print in conditions, like :

if ((print "angry") && (print "mammoth") || (print "will stomp you")) { // always executed } 

Now what's the use of this ? No idea.

Comments

0

You can the return value in cases where you actually have to count.

Example:

for ( $i = 0; $i < 10; $i += print "$i<br>" ); 

You can combine printing and counting here.

Comments

-1

print always return 1 if the print work successfully. else it will return 0. and in echo we can show the successful or unsuccessful of print function.

echo print(1);// 11

if we write print(1) alone it will print to us 1 .

print(1);// 1

one example for better knowing.


function sum($a,$b) { echo $a+$b; if($a+$b == true) { return 1; } else{ return 0;} } 

// now we call the function

sum(3,1);//4 echo "<br>"; echo sum(3,1);// 41 echo "<br>"; sum('abc','xyz');// 0 echo "<br>"; echo sum('abc','xyz');//00 

this code work like print function

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.