Php &引用;除零“;嵌套for循环中出现错误

Php &引用;除零“;嵌套for循环中出现错误,php,for-loop,division,zero,Php,For Loop,Division,Zero,本脚本的目的是: 编写一个以1为单位从1计数到10的脚本。对于每个数字,显示 数字是奇数或偶数,如果数字是素数,也会显示消息。 在HTML表中显示此信息 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="e

本脚本的目的是:

编写一个以1为单位从1计数到10的脚本。对于每个数字,显示 数字是奇数或偶数,如果数字是素数,也会显示消息。 在HTML表中显示此信息

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Exercise 1</title>
<link rel="stylesheet" type="text/css" href="common.css" />
<style type="text/css">
  th { text-align: left; background-color: #999; }
  th, td { padding: 0.4em; }
  tr.alt td { background: #ddd; }
</style>
</head>
<body>

<h2>Exercise 1</h2>

<table cellspacing="0" border="0" style="width: 20em; border: 1px solid #666;">
  <tr>
    <th>Number</th>
    <th>Parity</th>
    <th>Primality</th>
  </tr>
    <?php 
    $n=10;
    for ($i=1;$i<=$n;$i++){
    echo ($i%2 != 0)? '<tr class = "alt">':'<tr>'; ?>
    <td><?php echo $i; ?></td>
    <td><?php echo ($i%2 != 0)? "Odd":"Even";?></td>
    <td><?php 

    $k=0;
    for ($j=1;$j<=$i;$j++){
        if ($i%$j=0) $k++; //Where the error occurs
    }
    echo ($k=2 || $k=1)?"Prime":"Composite";?>
    </td></tr>
    <?php
    }
    ?>
</table>
</body>
</html>

练习1
th{文本对齐:左;背景色:#999;}
th,td{padding:0.4em;}
tr.alt td{背景:#ddd;}
练习1
数
对等
首要性
如果($i%$j=0)
可能应该是
如果($i%$j==0)

$j
指定为
0
,然后尝试执行
$i%0

你想要

($i%$j)==0

echo
也有同样的错误


还可以考虑用空格格式化代码,使其更具可读性。

请从您的问题中删除所有HTML,以便我们可以关注核心问题。谢谢,这是等号。在校正了另外两个等号之后,脚本就完美了!