S
S
Shirshov Alexander2015-10-08 20:57:34
PHP
Shirshov Alexander, 2015-10-08 20:57:34

A useless question about print(), why does it get what it does?

learned that print() returns 1, decided to play.
If you do
$a = 1 + print("3"); echo ++$a;
that, 33 will be displayed on the screen, everything is correct: the first triple was displayed by print and returned 1, a = 1 + 1, and the second triple was displayed through the increment.
But if you swap 1 and print, then everything will be a little different: it
$a = print("3") + 1; echo ++$a;
will print 42!
Changing the places of terms affects type casting, but what is happening here is still not clear.
Somehow the +1 moved inside print() which I find funny, are there any gurus here who understand how it works?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
kapai69, 2015-10-08
@Keanor

print is not a "real" function (it's a language construct), so it's not necessary to enclose arguments in parentheses.

Quote from the manual.
That is, it will display everything up to the semicolon, and not what it has in brackets. Therefore, the second expression will look like
Hence the result.

O
OnYourLips, 2015-10-08
@OnYourLips

In the first case, everything is obvious: 33.
In the second case, it will be like this: print(("3") + 1). Therefore, 4 will be displayed. Then 2 is also obvious.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question