Find multiples of a number in PHP
if ($count % 20 != 0)
How can I check if a number is a multiple of the input - PHP
Use the % operator.
The Modulo operator divides the numbers and returns the remainder.
In math, a multiple means that the remainder equals 0.
function checkIfMult($input,$toBeChecked){
return $toBeChecked % $input === 0;
}
function checkIfMult($input, $toBeChecked){
console.log('checkIfMult(' + $input +',' + $toBeChecked + ')', $toBeChecked % $input === 0);
return $toBeChecked % $input === 0;
}
checkIfMult(2,4) // true
checkIfMult(2,6) // true
checkIfMult(2,7) // false
checkIfMult(3,6) // true
checkIfMult(3,9) // true
checkIfMult(3,10) // false
PHP if is a multiple of an integer
if ( $i==0 || ($i+1)%3 == 0 )
{
//do stuff
}
What this will do, is go to the next index, divide it by 3, and see if there is a remainder. If there is none, then that means that the current index is one less than a number that is divisible by 3
If a number is multiple of 3 starting at different numbers
Yes, you need the modulo (not modulus) operator. This gives the remainder when divided by three - either 0, 1 or 2.
The first test would be:
if ($number % 3 == 1)
You may wish to evaluate only once and use in a switch statement - as the other David comments below, this is marginally more efficient:
switch ($number % 3) {
case 1:
$number = $varA;
break;
case 2:
$number = $varB;
break;
case 0:
$number = $varC;
break;
}
Always echo a number of cells which is a multiple of 5 in table (php)
Your shownumber.php
should probably look like this:
<table>
<?php
function print_number($Max_number) {
$x = 1;
while ($x <= $Max_number) {
echo "<tr>";
for ($col=1; $col <= 5; $col++) {
echo ($x <= $Max_number)
? "<td>{$x}</td>"
: '<td></td>';
$x ++;
}
echo "</tr>";
}
}
print_number($_POST["Max_number"] ?? 0);
?>
</table>
- You should use
<tr>
to quote all rows. - You should continuously printing rows until the max number is reached (see the while loop).
Some other code-style improvements:
- The global
$Max_number=$_POST["Max_number"]
is not needed if you can simply use$_POST["Max_number"]
as the function argument. - Simplified your if-then statement into ternary operator to keep things clean and short.
- Used null coalesce operator
$_POST["Max_number"] ?? 0
to prevent error if the form submission is not correct.
How can I round down to the nearest multiple of 3 in PHP?
Is this what you looking for.
/**
* Round an integer down to the nearest multiple of the given multiple
* @param integer $number
* @param integer $multiple
* @return integer
*/
function round_down_to_multiple_of( int $number, int $multiple = 3 ): int
{
return (int)($multiple * floor( $number/$multiple ));
}
# TESTS
$numbers = [ 10, 9, 8, 1, 0 ];
foreach( $numbers as $number ){
printf( '%d became %d'.PHP_EOL, $number, round_down_to_multiple_of( $number, 3 ) );
}
After running the above test I get the following results:
10 became 9
9 became 9
8 became 6
1 became 0
0 became 0
how to find the sum of all the multiples of 3 or 5 below 1000 in php, issue?
Some numbers are multiples of both 3 and 5. (Your algorithm adds these numbers to the total twice.)
Function to check if number is multiple of another (including decimals)
Since the resulting type of floor
is double and $_number
is also double, the safest way it to have a relative comparison <
instead of absolute comparison.
Floating precision might vary. Thus, this is the safest way:
if (abs(floor( $_number ) - $_number) < 0.00001) {
return true;
}
return false;
return the first n multiples of the number x
You switched the x
and the n
in the for
loop
//changed var to const & let
function multiples(x, n) { const arr = []; for (let i = 1; i <= n; i++) arr.push(x * i); return arr;}
console.log(multiples(2, 5));
Related Topics
PHP Import Excel into Database (Xls & Xlsx)
"Web Interface" to PHPunit Tests
Difference Between Set_Time_Limit() and Ini_Set('Max_Execution_Time', ...)
Enabling Cors in Codeigniter ( Restserver by @Chriskacerguis )
Search and Replace Value in PHP Array
Unit Testing with Items That Need to Send Headers
How to Create a Fluent Query Interface
How to Get the Server Path to the Web Directory in Symfony2 from Inside the Controller
How to Use Andwhere and Orwhere in Doctrine
Laravel Error: Method Illuminate\View\View::_Tostring() Must Not Throw an Exception
How Does Pcntl_Fork Work in PHP
PHP Get How Many Days and Hours Left from a Date
Laravel Form HTML with Put Method for Put Routes
How Does {} Affect a MySQL Query in PHP
Move an Array Element to a New Index in PHP
Comparing Passwords with Crypt() in PHP
Boolean Variables Posted Through Ajax Being Treated as Strings in Server Side