Explain the output: echo (2). (3 * (print 3));

I know the above 323 code outputs

But you need an explanation of how it infers 323

Can anyone explain?

-one
source share
3 answers

Here

echo (2). (3 * print (3));

Step 1: (Run Run)

echo (2).(3*print(3)); //Output = '' 

Step 2: (print action will be completed)

  echo (2).(3*1); // Will print 3 and returns 1 as per print function. Output = 3 

Step 3: (multiplication operation)

  echo (2).(3); // Multiplication operation willtake place. Output = 3 

Step 4: (Print data)

  echo (2).(3); //The . Operator will used to concate the strings in php, thus Output = 323 

Printing takes precedence over echo, so it prints 3 first later than 2 and 3

+1
source

Both echoes and seals are intended to behave more like language constructs rather than functions. Thus, they have a flow of control. What happens in your code here is that you invoke printing from the internal language structure (echo). This means that the print will send its result first before the echo completes its task (remember that you called printing from within the echo).

To show what is going on a little more clearly, in reality this has nothing to do with operator priority.

 echo ('a') . ('b' * (print 'c')); // ca0 // This is the same thing as... echo 'a' . 'b' * print 'c'; // ca0 

Note that operators do not affect the order of characters in the resulting output here.

print always returns 1, so what happens is that you performed the arithmetic operation on 'b' * 1 , which is the variable b multiplied by the return value of print . Thus, why do you see the output as c (print the output, which before the echo even finished the job), first, and then all that the echo had to print.

Let me elaborate on the following example ...

 echo print 'a' . 'b' . 'c'; // abc1 

Note 1 at the end of this conclusion. This is due to the fact that all echoes output the return value print , not a string. Instead, printing is the one that provided the abc output as output (and remember that printing is capable of sending output before the echo can appear, since the echo must wait to process everything inside the structure before it will end).

This makes it even more understandable ...

 echo (print 'a') . 'b' . 'c'; // a1bc 

Now 1 appears immediately after a.

If you want echo to send the output for each expression separately, you can provide one argument for each expression that you want to process and send to the output ... So for example:

 echo print 'a', 'b', 'c'; // a1bc echo 2, 3 * print 3; // 233 

I hope this clarifies you again.

+6
source

The reason (print 3) takes precedence over previous statements. If you write `echo (8). (7 * (seal 3)); you get 387 for example.

+2
source

All Articles