Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Using $count++ in an if condition

I'm wondering if the $count++ way of incrementing a counter is okay to use in a conditional statement? Will the variable maintain it's new value?

$count = 0;
foreach ($things as $thing){
    if($count++ == 1) continue;
    ...
}
like image 822
David Angel Avatar asked Oct 28 '25 01:10

David Angel


1 Answers

  • $count++ is a post-increment. That means that it will increment after the evaluation.
  • ++$count is a pre-increment. That means that it will increment before the evaluation.

http://www.php.net/manual/en/language.operators.increment.php

To answer your question, that is perfectly valid, just keep in check that your value will be 2 after the if has been done.

like image 55
Anyone Avatar answered Oct 30 '25 16:10

Anyone