如何将此HTML代码转换为php

时间:2014-03-25 17:35:09

标签: php html

我是php的新手,我有一个HTML代码,我想用PHP编写它。我用

读过

echo "HTMLCODE";

我可以将HTML代码转换为PHP,但这对我来说很有用.. 我的HTML示例代码:

   <fieldset style="float: left; position: absolute; left: 40%; top:  40%; background-color:pink;">
      <legend>Log in</legend>
        <table border="0" >
                    <td>Username </td>
                    <td><input type="text" name="Username" id="Username" value=""></td>
                    <tr>      
                    </tr>
                    <td>Password </td>
                    <td><input type="password" name="Password" id="Password" value=""></td>
                    <tr>              
                    </tr>
                    <td></td>
                    <td><input type="submit" id="Submit" value="Enter"></td>
        </fieldset>

但是当我使用上面的规则在php中编写代码时,我得到了语法错误!我只是将一行HTML代码放在php中,这在netbeans 8.0中给出了错误

我的PHP代码:

<php
        echo " <fieldset style="float: left; position: absolute; left: 40%; top:  40%; background-color:pink;">     
        </fieldset>";

?>

我在这里想念的是什么?是因为我在风格论证中使用的风格和分号?或者我应该使用hyphon而不是引号?!!!

3 个答案:

答案 0 :(得分:2)

"替换为\"

所以:

<php
        echo " <fieldset style=\"float: left; position: absolute; left: 40%; top:  40%;     background-color:pink;\">     
        </fieldset>";

?>

这也有效:

<?php
echo '<fieldset style="float: left; position: absolute; left: 40%; top:  40%; background-color:pink;">';
echo '      <legend>Log in</legend>';
echo '        <table border="0" >';
echo '                    <td>Username </td>';
echo '                    <td><input type="text" name="Username" id="Username" value=""></td>';
echo '                    <tr>      ';
echo '                    </tr>';
echo '                    <td>Password </td>';
echo '                    <td><input type="password" name="Password" id="Password" value=""></td>';
echo '                    <tr>              ';
echo '                    </tr>';
echo '                    <td></td>';
echo '                    <td><input type="submit" id="Submit" value="Enter"></td>';
echo '        </fieldset>';
?>

答案 1 :(得分:2)

问题在于字符串中的反斜杠(用于转义字符)

所以 -

echo " <fieldset style=\"float: left; position: absolute; left: 40%; top:  40%; background-color:pink;\">     
    </fieldset>";

答案 2 :(得分:2)

由于"在PHP中用作字符串分隔符,因此您需要通过使用"符号预先修复HTML中的所有\字符。

echo "<fieldset style=\"float: left; position: absolute; left: 40%; top:  40%; background-color:pink;\">.....</fieldset>";

或者,使用'作为字符串分隔符,然后HTML可以包含"个字符,如下所示:

echo '<fieldset style="float: left; position: absolute; left: 40%; top:  40%; background-color:pink;">.....</fieldset>';