问题描述
我是PHP的新手,它试图创建以下内容,同时最大程度地减少所需的代码量. PHP应该显示一个100的列表,然后如果数字是/乘3、5或3和5,则显示.如果没有,则不显示任何内容.
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. If not by any then show nothing.
这是我到目前为止所能获得的,但是任何帮助都将是非常有用的,因为不确定3位和5位的/,如下所示.
This is what I've got so far, but any help would be great since not sure about the / by 3 and 5 bit as you can see below.
<?php $var = range(0, 100); ?>
<table>
<?php foreach ($var as &$number) {
echo " <tr>
<td>$number</td>
<td>";
if($number % 3 == 0) {
echo "BY3";
} elseif ($number % 5 == 0) {
echo "BY5";
} elseif ($number % 3 and 5 == 0) {
echo "BY3 AND 5";
}
echo "</td></tr>";
}
?>
</table>
谢谢
推荐答案
不...您应该先检查它是否可分割为15(3x5)(或3和5),然后再进行其他检查:
Nope... you should check first if it's divisble for 15 (3x5) (or 3 and 5) and after you can do other checks:
if($number % 15 == 0) {
echo "BY3 AND 5";
} elseif ($number % 5 == 0) {
echo "BY5";
} elseif ($number % 3 == 0) {
echo "BY3";
}
echo "</td></tr>";
?>
因为每个数字15的整数也将3和5的整数除.所以您的最后一张支票永远都不会打
Because every number divisble for 15 is also divisble for 3 and 5. So your last check could never hit
这篇关于PHP-如果数字可以被3和5整除,则回显的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!