⊗ppPmCdExRC 108 of 447 menu

Checking the Remainder of Division in PHP

Let we have two variables with numbers:

<?php $a = 10; $b = 3; ?>

Let's find the remainder of dividing one variable by another:

<?php $a = 10; $b = 3; echo $a % $b; // outputs 1 ?>

Now let the variables store such values that one variable is divisible by the second without a remainder:

<?php $a = 10; $b = 5; echo $a % $b; // outputs 0 ?>

Let's write a script that will check whether one number is divisible by the second without a remainder:

<?php $a = 10; $b = 3; if ($a % $b === 0) { echo 'divisible without remainder'; } else { echo 'divisible with remainder'; } ?>

Now let it be required, if the number is divisible with a remainder, to display this remainder on the screen:

<?php $a = 10; $b = 3; if ($a % $b === 0) { echo 'divisible without remainder'; } else { echo 'divisible with remainder ' . $a % $b; } ?>

In the code above, it turns out that the remainder is calculated in two places, which is not optimal.

Let's fix the problem:

<?php $a = 10; $b = 3; $rest = $a % $b; if ($rest === 0) { echo 'divisible without remainder'; } else { echo 'divisible with remainder ' . $rest; } ?>

As you know, even numbers are divisible by 2 without a remainder, and odd numbers - with a remainder. Let you are given a number. Using the operator % and the if construction, check if it is even this number or not.

Given a number. Check that it is divisible by 3.

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline