Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explain the output of : echo 'to'.print('2')+2;

Tags:

php

I run this and it will produce 4to1

But need an explanation on how is it outputting 4to1

Anybody can pls explain?

like image 814
Pankaj katiyar Avatar asked May 13 '16 08:05

Pankaj katiyar


People also ask

Where does PHP echo output?

Echo sends output to the output buffer, If it's embedded in HTML (which is also being sent to the output buffer) then it appears that it writes that to the "source".

What is the output of PHP echo I am a PHP script?

It output one or more strings. We can use 'echo' to output strings, numbers, variables, values, and results of expressions. Below is some usage of echo statements in PHP: Displaying Strings: We can simply use the keyword echo followed by the string to be displayed within quotes.

What is echo and print command?

echo and print are more or less the same. They are both used to output data to the screen. 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.

What is echo in PHP with example?

void echo ( string $arg1 [, string $... ] ) PHP echo statement can be used to print the string, multi-line strings, escaping characters, variable, array, etc. Some important points that you must know about the echo statement are: echo is a statement, which is used to display the output.


1 Answers

print('2')+2 this part of code will print the number 4 to start of the line. Print will be resolved before echo.

Then function echo will be resolved and echo contain to as first and if you use print() inside echo, it will print the function parameter as first and then number 1.

As Andreas Scheibleger mentioned in comment, the 1 comes from the return value of print which is always 1, because it is not possible to echo a print() function.

For example:

echo print("");     // 1
echo print("test"); // test1
like image 97
pes502 Avatar answered Oct 15 '22 08:10

pes502