-7

I am new to php, for some reason the increment/decrement works inversely. Can someone tell me the reason behind this?

<?php

$var1 = 3;

echo "Addition = " . ($var1 += 3) . "<br>" ;
echo "Subtraction = " . ($var1 -= 3) . "<br>" ;
echo "Multiplication = " . ($var1 *= 3) . "<br>" ;
echo "Divison = " . ($var1 /= 3) . "<br>" ;
echo "Increment = " . $var1++ ;
echo "Decrement = " . $var1-- ;

?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
suren
  • 44
  • 2
  • 10
  • the decrement/increment is occuring next tick. to get it to occur in the current tick you must prefix the var with the operator like so: `++$var` OR `--$var` – r3wt May 25 '15 at 11:25
  • Addition = 6 Subtraction = 3 Multiplication = 9 Divison = 3 Increment = 3Decrement = 4 , this is the output im getting – suren May 25 '15 at 11:25
  • Addition = 6 Subtraction = 3 Multiplication = 9 Divison = 3 Increment = 3 Decrement = 4 its correct – chandresh_cool May 25 '15 at 11:25
  • The answers you are getting are correct. What is the confusion here? – Amal Ts May 25 '15 at 11:27
  • yes prefix worked perfectly, thanks r3wt, but can you explain me why this has happend ? – suren May 25 '15 at 11:28
  • Addition = 6 Subtraction = 3 Multiplication = 9 Divison = 3 Increment = 4 Decrement = 3, this should be the rite answer. – suren May 25 '15 at 11:29
  • read this http://php.net/manual/en/language.operators.php – Ejaz May 25 '15 at 11:29

1 Answers1

0

If you know how the increment and decrement operators work, you'll get the answer.

echo "Increment = " . $var1++ ; //Prints $var1 and then increments the value

echo "Decrement = " . $var1-- ; // Prints the incremented value from previous operation and then decrements the value

To achieve what you are trying to do, use --$var1

Amal Ts
  • 857
  • 1
  • 6
  • 28