2014-03-25 76 views
0

我是新来的PHP,我有一个HTML代码,我想用PHP编写它。我读过使用如何转换此HTML代码到php

echo "HTMLCODE";

我可以HTML代码转换为PHP但这AINT制定了我.. 我的示例代码中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而不是引号?

+1

行情。行情。行情。 –

+0

http://www.php.net/manual/en/language.basic-syntax.phpmode.php – TonyArra

回答

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>'; 
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>'; 
?> 
2

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

So-

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