Doubt about decrement operator

3

I have these 2 lines in my function, but what would be the line below?

$idade--;

Would not the return just be the same?

$idade--;
return $idade;

I just wanted to understand what this line would look like. I do not like to use something I do not know what it's for.

Full Code

    
asked by anonymous 28.05.2015 / 23:09

2 answers

5
  

Would not the return just be the same?

No !!!

The $idade-- subtracts 1 from the value of $idade . Equivalent to:

$idade = $idade - 1;

So, if you remove the $idade-- , the value returned by your function will always be 1 more than it returns today.

If you want, you can even do both things on the same line, but then you would use --$idade instead of $idade-- :

return --$idade;

This is because the -- at the end first returns the (current) value of the variable, and then decrements it. In the beginning, it first decrements and then returns. The same is true for the ++ (start or end) increment operators.

    
28.05.2015 / 23:13
4

Assuming that $idade is an integer, $idade-- is equal to $idade = $idade - 1 , as $idade++ is $idade = $idade + 1 .

This is a decrement operator. See more in PHP

    
28.05.2015 / 23:13