Subtracting 1 from a value and storing it in another variable

NightHawk picture NightHawk · Feb 25, 2011 · Viewed 13.1k times · Source

I vaguely remember running into this problem before, but I'm wondering if this just doesn't work in PHP:

echo $counter; // outputs 4
$output = $counter--;
echo $output; // outputs 4

If I do something like:

$output = $counter - 1;

I have no problems whatsoever.

Can someone shed some light on this?

Thanks, Ryan

Answer

AndreKR picture AndreKR · Feb 25, 2011

What you want is the pre-decrement operator:

echo $counter; // outputs 4
$output = --$counter;
echo $output; // outputs 3