note 70424 deleted from language.operators.arithmetic by salathe

From: Date: Mon, 21 Sep 2020 14:58:34 +0000
Subject: note 70424 deleted from language.operators.arithmetic by salathe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-215405@lists.php.net to get a copy of this message
Note Submitter: Jonathon Reinhart ---- A very simple yet maybe not obvious use of the modulus (%) operator is to check if an integer is odd or even. <?php if (($a % 2) == 1) { echo "$a is odd." ;} if (($a % 2) == 0) { echo "$a is even." ;} ?> This is nice when you want to make alternating-color rows on a table, or divs. <?php for ($i = 1; $i <= 10; $i++) { if(($i % 2) == 1) //odd {echo "<div class=\"dark\">$i</div>";} else //even {echo "<div class=\"light\">$i</div>";} } ?>

« previous php.notes (#215405) next »