Friday 20 October 2017

What is the PHP operator % and how do I use it in real-world examples?

itemprop="text">

What is the explanation for PHP's
operator % in full
detail?



Including examples would be
nice!



Answer




It's the modulus operator, which gives the
integer remainder of a
division
e.g.




7
/ 2 = 3.5 // 3 remainder 1
7 % 2 = 1 // the
remainder


Obvious real
world example is working out whether a number is odd or
even



if (($n % 2) == 0) the number is even, else
it's odd... useful when you want to show alternate rows in a table in different
colours


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...