PHP - If number is divisible by 3 and 5 then echo

前端 未结 9 2627
[愿得一人]
[愿得一人] 2021-02-19 02:13

I\'m new to PHP and trying to create the following whilst minimizing the amount of code needed. PHP should show a list of 100 then display if the number is / by 3, 5 or 3 and 5.

9条回答
  •  我寻月下人不归
    2021-02-19 02:59

    $num_count = 100;
        $div_3 = "Divisible by 3";
        $div_5 = "Divisible by 5";
        $div_both = "Divisible by 3 and 5";
        $not_div = "Not Divisible by 3 or 5";
    
        for($i=0;$i<=$num_count;$i++)
        {
            switch($i)
            {
                case ($i%15==0):
                echo $i." (".$div_both.")
    "; break; case ($i%3==0): echo $i." (".$div_3.")
    "; break; case ($i%5==0): echo $i." (".$div_5.")
    "; break; default: echo $i."
    "; break; } }

提交回复
热议问题