添加" border"到echo语句中的表

时间:2014-05-20 19:12:01

标签: php html html-table echo

所以我只是通过PHP语句构建了一个表,但我不知道如何添加border="1"属性,因为这会混淆echo语句并导致编译错误。

这是我的代码,是的,这种格式看起来很可怕,但我只需要以某种方式给表格border。!

echo 
"<table><tr>
<th></th>
<th>A</th>
<th>B</th>
<th>AB</th>
<th>O</th>

</tr><tr>

<th>N</th>
<th>" . $ATypeN . "</th>
<th>" . $BTypeN . "</th>
<th>" . $ABTypeN . "</th>
<th>" . $OTypeN . "</th>
<th>". 

"</tr><tr>

<th>Y</th>
<th>" . $ATypeY . "</th>
<th>" . $BTypeY . "</th>
<th>" . $ABTypeY . "</th>
<th>" . $OTypeY . "</th>
</tr>
</table>";

2 个答案:

答案 0 :(得分:5)

您需要在引号字符前紧跟\(反斜杠)escape引号,例如:

echo "<table border=\"0\"><tr>";

您也可以在双引号内使用单引号,反之亦然,例如:

echo '<table border="0"><tr>';

或:

echo "<table border='0'><tr>";

评论者指出了HEREDOC方法,这对您来说也很有价值。以相同的标识符开始和结束:

/* start with "EOT", must also terminate with "EOT" followed by a semicolon */

echo <<<EOT 
<table><tr>
<th></th>
<th>A</th>
<th>B</th>
<th>AB</th>
<th>O</th>

</tr><tr>

<th>N</th>
<th>$ATypeN</th>
<th>$BTypeN</th>
<th>$ABTypeN</th>
<th>$OTypeN</th>
</tr><tr>

<th>Y</th>
<th>$ATypeY</th>
<th>$BTypeY</th>
<th>$ABTypeY</th>
<th>$OTypeY</th>
</tr>
</table>
EOT; /* terminated here, cannot be indented, line must contain only EOT; */

答案 1 :(得分:1)

虽然上面的答案指出了你的错误,但还有一些事情需要指出。

如果您使用单引号,则无需转义双引号:

所以

echo '<table border=\"0\"><tr>';

应该是

echo '<table border="0"><tr>';

使用单引号和逗号来连接stings比使用双引号和句点具有更快的执行时间;双引号中的所有内容都会被评估。

所以你也可以这样做

echo '<table><tr><td>',$someValue,'</td></tr></table>';

你还可以做的另一种方法是写出HTML然后回显下面的变量,你可以在文本编辑器中突出显示HTML的语法。

<table>
    <tr>
        <td><?php echo $someValue ?></td>
    </tr>
</table>

或启用PHP Short标签(我不是其中的粉丝)

<table>
    <tr>
        <td><?=$someValue ?></td>
    </tr>
</table>