13

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 ?
Matt Bryant
  • 4,841
  • 4
  • 31
  • 46
GusDeCooL
  • 5,639
  • 17
  • 68
  • 102
  • 1
    that's why echo() better than print() – Yuda Prawira May 18 '11 at 07:49
  • 1
    From the related links: [How are echo and print different in PHP?](http://stackoverflow.com/questions/234241/how-are-echo-and-print-different-in-php) – mario May 18 '11 at 08:12
  • Does this answer your question? [How are echo and print different in PHP?](https://stackoverflow.com/questions/234241/how-are-echo-and-print-different-in-php) – tripleee Mar 19 '22 at 15:14

6 Answers6

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");
mario
  • 144,265
  • 20
  • 237
  • 291
  • 9
    "print() is not actually a real function (it is a language construct)" -- [PHP Manual](http://php.net/manual/en/function.print.php) – Tahir Akhtar May 18 '11 at 07:55
  • 1
    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. – mario May 18 '11 at 08:00
  • hmmm seem like this is the logical reason for (int) 1, thank you very much to give real use case example :) – GusDeCooL May 18 '11 at 13:50
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.)

Sebastian Paaske Tørholm
  • 49,493
  • 11
  • 100
  • 118
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

Tahir Akhtar
  • 11,385
  • 7
  • 42
  • 69
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.

user703016
  • 37,307
  • 8
  • 87
  • 112
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.

fuxia
  • 62,923
  • 6
  • 54
  • 62
-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