0

Consider the following php segment code:-

<?php

#Part-1
$a = 1;
$c = $a + $a++;
echo $c;

#Part-2
$a = 1;
@ $c = $a + $a++;
echo $c;

#Part-3
$a = 1;
$c = $a + $a + $a++;
echo $c;

#Part-4
$a = 1;
@ $c = $a + $a + $a++;
echo $c;
?>

The output of the above code segment is: 3233
Now my questions is following:-
1. How output is 3233 - explain breafly?
2. And what is the role of @ sign in the code segment?
3. If Part-1 code segment output is 3 but how Part-3 code segment output 3?

dsolimano
  • 8,870
  • 3
  • 48
  • 63
Optimus Prime
  • 308
  • 6
  • 22
  • Its used to suppress any error messages - notice, warning or even fatal generated by the associated line of code. – Mithun Satheesh Sep 26 '14 at 16:02
  • The [@ sign is an error suppressor](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php), however, weird behavior that... – Wrikken Sep 26 '14 at 16:04
  • 1
    Voted to reopen as OPs question (why does `@$c = $a + $a++;` return 2 while `@c = $a + $a++` return 3?) wasn't answered. [**Here is a simplified example**](https://eval.in/199046). – h2ooooooo Sep 26 '14 at 16:08
  • 2
    [From the manual about operator precedence](http://php.net/manual/en/language.operators.precedence.php) `$a = 1;echo $a + $a++; // may print either 2 or 3`, it [actually depends on what version you use](http://3v4l.org/Rv0tY), but in short: your statement has no defined outcome. In short: because _"Operator precedence and associativity only determine how expressions are grouped, they do not specify an order of evaluation. "_, don't create these kind of statements. http://3v4l.org/Rv0tY – Wrikken Sep 26 '14 at 16:12

0 Answers0