I know the above code outputs 323
But need an explanation on how is it outputting 323
Anybody can pls explain?
Both echo and print are intended to behave more like language constructs and not functions. As such they have a flow of control. What happens here in your code is that you are calling print from inside of a language construct (echo). Meaning that print will send its output first before echo has completed its task (remember you called print from inside of echo).
To show you what's happening a bit more clearly, it actually has nothing to do with operator precedence at all.
echo ('a') . ('b' * (print 'c')); // ca0
// This is the same thing as...
echo 'a' . 'b' * print 'c'; // ca0
Notice the operators have no effect on the order of the characters in the resulting output here.
print always returns 1 so what happens here is that you performed an arithmetic operation on 'b' * 1, which is the stirng b multiplied by the return value of print. Thus why you see the output as c (print sent output before echo has even finished doing it's job), first, and then everything that echo was supposed to print.
Let me elaborate even further with the following example...
echo print 'a' . 'b' . 'c'; // abc1
Notice the 1 at the end of that output. This is because all echo did was output the return value of print and not the string. Instead, print is the one that provided the abc as output (and remember print is able to send output before echo can since echo has to wait to process everything inside of the construct before it can finish).
This makes it even more clear...
echo (print 'a') . 'b' . 'c'; // a1bc
Now the 1 comes right after a.
If you want echo to send the output for each expression individually, you can supply one argument for every expression you want processed and sent to output... So for example:
echo print 'a', 'b', 'c'; // a1bc
echo 2, 3 * print 3; // 233
I hope that clarifies it a bit more for you.
The reason is (print 3) has priority over the previous operators. If you write `echo (8) . (7 * (print 3)); you will get 387, for example.
Here
echo (2).(3*print(3));
Step1:(Starting execution)
echo (2).(3*print(3)); //Output = ''
Step 2:(Print action will take place)
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:(Prints the data)
echo (2).(3); //The . Operator will used to concate the strings in php, thus Output = 323
Print has more priority then echo thus it prints 3 first later 2 and 3
Related
the output of the following code?
echo '1' . (print '2') + 3;
I tested and the result is 214, but why 214?
if I code:
echo (print '2') + 3;
the result is 24
Then, echo '1' . '24'; should be 124.
Confused...
When the expression is parsed, the "print" statement is immediately writing its output. So there's the first 2. By definition its return value is 1.
So then the remaining expression is the character 1, followed by the numeric expression 1+3. Therefore 1 and 4.
214
I was experimenting with weak/dynamic typing properties of PHP in preparation for a test and was completely baffled by the output of this string concatenation. Can someone explain how this is even possible?
<?php echo 1 . "/n" . '1' + 1 ?><br />
output:
2
Analysis:
echo 1 . "/n" . '1' + 1;
is equivalent to
//joined first 3 items as string
echo "1/n1"+1;
is equivalent to
//php faces '+' operator, it parses '1/n1' as number
//it stops parsing at '/n' because a number doesn't
//contain this character
echo "1"+1;
is equivalent to
echo 1+1;
This question already has answers here:
Why does "echo '2' . print(2) + 3" print 521? [closed]
(4 answers)
Closed 9 years ago.
This is related to:
Why does "echo '2' . print(2) + 3" print 521?
I was wondering why
print(99) + print(99) + print(99) + print(99);
shows 99100100100....
I'd like to learn about the quirks of PHP.
Print is not a function, so print(99) is the same as print 99. We can remove the parentheses for clarity.
print 99 + print 99 + print 99 + print 99;
The expression is evaluated from the right, so it becomes
print (99 + print (99 + print (99 + print 99)));
The rightmost print executes first, printing "99" and evaluating to 1.
Output:
99
Code left to be evaluated:
print (99 + print (99 + print (99 + 1)));
Again, the rightmost print is executed and it prints 99+1 ("100") and evaluates to 1.
Output:
99100
Code left to be evaluated:
print (99 + print (99 + 1));
...and so on.
The print function produces output and then (see documentation) returns the value 1. To further complicate things, the print function is a language construct (see the documentation) which does not require the parenthesis.
So, if you had the statement
print(99) + print(99);
What you are actually seeing is the printing of a 99, from the right-most print, followed by the result of print(99) + 1; which, due to the language construct, is the same as print 99 + 1; or print (99 + 1);.
Because, from the docs,
print …
Returns 1, always
Because print() always returns 1. Check the php manual
This is unlike echo() which does not return the value.
Both print() and echo() are not actual functions but language constructs.
We've coded two validation functions, one is like below (it gets all the fields at once):
function check_fields(&$arr,&$msg,$types,$lens,$captions,$requireds) {}
and the other function is something like below:
function is_valid($field=NULL,$type=0 ,$length=0,$required=true) {}
First function has a few code lines, and reduces code lines dramatically (about 30-35 lines or even more), on the other side the second function without reference increases the code lines (about 30-35 lines or even more).
We have to call the second function for every field we want to validate, but the first function (check_fields) is vice versa.
I've read in an article long time ago that functions with reference parameters are bad from a performance point of view.
Now we don't know which function to use. Which one is better from a performance perspective?
use the solution that is simpler to use and easier to maintain.
You are talking about micro optimization, which is pretty much useless.
use references because in your case it is simpler solution and requires less code.
Well I think I got my answer myself after some web searches:
Do not use PHP references
You just remember this, when you called:
<?php
$a = 1 ;
echo "As initial, the real 'a' is..".$a."<br/>";
$b = &$a ; // it's meaning $b has the same value as $a, $b = $a AND SO $a = $b (must be)
$b += 5;
echo "b is equal to: ".$b." and a equal to: ".$a."<br/>";
echo " Now we back as initial, when a=1... (see the source code) <br/>";
$a = 1 ;
echo "After we back : b is equal to: ".$b." and a equal to: ".$a."<br/>";
echo "Wait, why b must follow a? ... <br/> ..what about if we change b alone? (see the source code)<br/>";
$b = 23;
echo "After we change b alone, b equal to: ".$b." and a equal to: ".$a."<br/>";
echo " WHAT ?? a ALSO CHANGED ?? a and b STICK TOGETHER?!! </br>" ;
echo "to 'clear this, we use 'unset' function on a (see the source code)<br/> ";
unset($a);
$b = 66;
$a = 1;
echo "Now, after unset,b is equal to: ".$b." and a equal to: ".$a."<br/>";
?>
And the Output will be:
As initial, the real 'a' is..1
After the Reference operator...(see the source code)
b is equal to: 11 and a equal to: 11
Now we back as initial... (see the source code)
After we back : b is equal to: 1 and a equal to: 1
Wait, why b must follow a? ...
..what about if we change b alone? (see the source code)
After we change b alone, b equal to: 23 and a equal to: 23
WHAT ?? a ALSO CHANGED ?? a and b STICK TOGETHER?!!
to 'clear this, we use 'unset' function on a (see the source code)
Now, after unset,b is equal to: 66 and a equal to: 1
Review:
Why did $a change? That's because & (the ampersand operator) made & as a reference variable, hence it stored in a 'temporary memory'. When you intitialized $b= &$a, see my comment $b = $a and also $a = $b, that means whenever you modified $b, $a was also modified, and vice versa. They are chained! That's the simple meaning of the reference operator.
Maybe at the beginning it feels confusing, but once you become an expert with this, you can handle this operator to make a "switching" function like this:
<?php
function bulbswitch(&$switch)
{
$switch = !$switch;
}
function lightbulb($a)
{
if ($a == true) { echo 'a is On <br/>';}
else { echo 'a is Off <br/>';}
}
$a = true ;
echo 'At the begining, a is On, then.... <br/>';
bulbswitch($a);
lightbulb($a);
bulbswitch($a);
lightbulb($a);
bulbswitch($a);
lightbulb($a);
bulbswitch($a);
lightbulb($a);
bulbswitch($a);
lightbulb($a);
bulbswitch($a);
lightbulb($a);
?>
The output will be:
At the begining, a is On, then....
a is Off
a is On
a is Off
a is On
a is Off
a is On
Here is a simple php program which gives a strange output. Can anyone explain why it is coming like this and how to get the expected output?
<?php
$a=2;$b=3;
echo "<br> ADD:".$a+$b;
echo "<br> SUB:".$a-$b;
echo "<br> MUL:".$a*$b;
echo "<br> DIV:".$a/$b;
?>
Output:
3-3
MUL:6
DIV:0.66666666666667
Expected Output:
ADD:5
SUB:-1
MUL:6
DIV:0.66666666666667
It is because the string concatenation operator . has the same precedence as the add/sub operators, and all of them are left-associative. This means that evaluation proceeds left-to-right, so "<br> ADD:".$a is evaluated first and the result is added to 3. This particular string converts to zero and 0 + 3 = 3. Similar for the subtraction.
Solution: put the arithmetic in parentheses.
echo "<br> ADD:".($a+$b);
echo "<br> SUB:".($a-$b);
On the other hand, mul/div have higher precedence than concatenation so they produce the expected result.