1

Hi just a quick question on an operator i have not come across before, which i cant seem to find the answer for...

what does the -= operator do?

the context of the code is in a pagination script.

$page = $_POST['page'];
$cur_page = $page;
$page -= 1;
$per_page = 15;
$previous_btn = true;
$next_btn = true;
$first_btn = true;
$last_btn = true;
$start = $page * $per_page;
echo('start: '.$start.' - per-page: '.$per_page);
Dizzy Bryan High
  • 2,015
  • 9
  • 38
  • 61
  • 2
    for `$page -= 1;` it's short for `$page=$page-1;` You could also do `$page--;` to decrement page by 1 – Waygood Jun 25 '13 at 16:35
  • Thats an excellent post but it dont think it answers my specific question i have already looked on the php site under the various operator groups and the -= operator i could not find, not does that link contain the specific -= operator... – Dizzy Bryan High Jun 25 '13 at 16:38
  • thanks @Waygood, will put that in my notebook – Dizzy Bryan High Jun 25 '13 at 16:39
  • It's mentioned under [`.=` Assignment Operators](http://www.php.net/manual/en/language.operators.assignment.php#40084) in the manual. – mario Jun 25 '13 at 17:06

2 Answers2

4

The -= operator take the number stored in the variable and subtracts the number after the -= sign.

$page -= 1;
//SAME AS
$page = $page - 1;
//SAME AS
$page--;
//SAME AS
--$page;
Mic1780
  • 1,774
  • 9
  • 23
1

In your example it takes away 1 from $page and reassigns it back to $page. But in general it subtracts the value to the right of the operand from the variable on the left and reassigns it.

it's equivalent to

$page = $page - 1

there's also the same for addition.

$page += 1

same as

$page = $page + 1

ncremins
  • 9,140
  • 2
  • 25
  • 24