Doubt about decrement operator

Asked

Viewed 61 times

3

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

$idade--;

Only Return wouldn’t be the same?

$idade--;
return $idade;

I just wanted to understand what this line would be for. I don’t like to use something I don’t know what it’s for.

Complete code

2 answers

5


Only Return wouldn’t be the same?

Not!!!

The $idade-- subtract 1 of the value of $idade. Amounts to:

$idade = $idade - 1;

Therefore, if you withdraw the $idade--, the value returned by its function will always be 1 more than she returns today.

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

return --$idade;

That’s because the -- at the end first returns the (current) value of the variable, and then decrees. At the beginning it first decrees and then returns. The same is true for increment operators ++ (at the beginning or at the end).

  • $idade = $idade -1, without the m, nay?

  • @Caiofelipepereira Ops, thanks, I hadn’t seen.

  • Dispose. That’s what we’re there for

  • 1

    I get the job now, Thank

  • 1

    Every year it adds up +1, $age-- makes it -1 if the month has not passed and etc.

4

Assuming $idade is a whole, $idade-- is equal to $idade = $idade - 1, as well as $idade++ is $idade = $idade + 1.

This is a decrease operator. See more on manual of PHP

  • 2

    I tried to get something in the manual, but without knowing the name is difficult, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.