J
J
JTester2022-04-20 18:08:35
PHP
JTester, 2022-04-20 18:08:35

How to shorten and write this code correctly?

Hello! Please help me shorten and write this code correctly!

$a = 1;
$b = 0;
if ($b === 0 && $a % 7 == 0) {
 // 
}
if ($b === 1 && $a % 3 == 0) {
 // 
}
if ($b === 2 && $a % 2 == 0) {
 // 
}
$a++;

In each check, the same functions are performed for me, the arguments just change and that's it. In each function, I change $b
Is this code correct?))))
$a- this is the time, it is executed in my timer

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AlexRas, 2022-04-21
@AlexRas

function c($a, $b, $numbers) {
    foreach ($numbers as $key => $value) {
        if ($b === $key && $a % $value == 0) {
        // 
        }
    }
}

$a = 1;
$b = 0;
c($a, $b, [7, 3, 2]);
$a++;

Not particularly reduced, but a more correct option if you have more if-like structures. And so there is nothing to cut.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question