在PHP中使用echo的正确语法


Correct syntax for using echo in PHP

我运行了这段代码,得到了这个错误:

 Parse error: syntax error, unexpected T_LNUMBER in C:'xampp'htdocs'Generate.php on line 5

问题出在哪里?

<?php
$satr=$_POST["satr"];
$soton=$_POST["soton"];
$bg=$_POST["bg"];
echo ("<table border="1" style="background-color:$bg">");
for($i=1;&i<=$satr;$i++)
{
    echo("<tr>");
    for($j=1;j<=$soton;$j++)
    {
        echo("<td>$soton</td>");
}
echo("</tr>");
}
echo("</table>");    
?>
    echo ("<table border='1' style='background-color:$bg'>");
    echo ('<table border="1" style="background-color:$bg">');
    echo "<table border='"1'" style='"background-color:$bg'">";
    echo '<table border="1" style="background-color:$bg">';
    echo "<table border='1' style='background-color:$bg'>";

您不需要括号,需要转义内部双引号

<?php
$satr=$_POST["satr"];
$soton=$_POST["soton"];
$bg=$_POST["bg"];
echo "<table border='"1'" style='"background-color:$bg'">";
for($i=1;&i<=$satr;$i++)
{
    echo "<tr>";
    for($j=1;j<=$soton;$j++)
    {
        echo "<td>$soton</td>";
    }
    echo "</tr>";
}
echo "</table>";
?>

另请看这里PHP中单引号和双引号字符串之间的区别是什么?

你能试试吗,你已经在中使用了&i用于循环,你需要使用$i

    $satr=$_POST["satr"];
    $soton=$_POST["soton"];
    $bg=$_POST["bg"];
    echo ("<table border='1' style='background-color:$bg'>");
    for($i=1;$i<=$satr;$i++)
    {
        echo("<tr>");
        for($j=1;j<=$soton;$j++)
        {
            echo("<td>$soton</td>");
        }
    echo("</tr>");
    }
    echo("</table>");

您没有转义字符串中的引号。你需要做这样的事情:

echo ("<table border='"1'" style='"background-color:$bg'">");