Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does print return (int) 1?

Tags:

php

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 ?
like image 750
GusDeCooL Avatar asked May 18 '11 07:05

GusDeCooL


People also ask

What value does the print () function return?

Since this is the purpose of print() , the function doesn't need to return anything useful, so you get None as a return value. Note: The Python interpreter doesn't display None .

Has no return value while has a return value of 1?

The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. echo can take multiple parameters (although such usage is rare) while print can take one argument. echo is marginally faster than print.”

Can a Python print function take no value to print?

One important thing to remember is that Python print() returns no value.

What is the main difference between print () and return?

print just shows the human user a string representing what is going on inside the computer. The computer cannot make use of that printing. return is how a function gives back a value. This value is often unseen by the human user, but it can be used by the computer in further functions.


1 Answers

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");
like image 164
mario Avatar answered Oct 03 '22 08:10

mario