我对编程很陌生,已经吸收了大约150页的书。当下面的代码让我感到困难时,我对PHP很顺利。任何人都可以解释打开和关闭PHP标签的位置。
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo "<p>Thanks for Registering</p>",
"Username: ", htmlentities ($_POST['username']), "<br />",
"Email: ", htmlentities ($_POST['email']), "<br />";
}
else { ?>
<form action = "test.php" method = "post">
<label for = "username">Username: </label>
<input type = "text" name = "username" />
<label for = "email">Email: </label>
<input type = "text" name = "email" />
<input type = "submit" value = "Register" />
</form>
<?php }
?>
我认为应该只有一对PHP标签:
<?php
?>
其他任何事情对我来说都很难消化,请帮助我理解为什么php标签位于代码之间非常奇怪的位置。 谢谢
答案 0 :(得分:2)
根据PHP tags
上的手册当PHP解析文件时,它会查找开始和结束标记 告诉PHP开始和停止解释 他们之间的代码。以这种方式解析允许嵌入PHP 各种不同的文件,作为一对之外的一切 PHP解析器忽略了打开和关闭标记。
这意味着php标签之间的代码将被回显并且在一个之外:
<form action = "test.php" method = "post">
<label for = "username">Username: </label>
<input type = "text" name = "username" />
<label for = "email">Email: </label>
<input type = "text" name = "email" />
<input type = "submit" value = "Register" />
</form>
将被视为普通HTML。在上面的情况下,PHP检查表单是否已被POST,如果是,它会显示一条感谢信息。如果HTTP请求不是POST,它将显示一个表单供用户发布。
一般来说,混合HTML和PHP并不是一个好习惯,你应该避免这种结构。
答案 1 :(得分:1)
最简单的思考方法是做解析器所做的事情,即用php标签中的echo语句替换php标签之外的内容。
这样做的主要好处是你不必在PHP字符串中转义HTML,而且,如果结构合理,你可以在WYSIWYG编辑器中很好地查看它
这是一个代码示例,它与您编写的代码相同,并且不会退出php模式
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo "<p>Thanks for Registering</p>",
"Username: ", htmlentities ($_POST['username']), "<br />",
"Email: ", htmlentities ($_POST['email']), "<br />";
}
else {
echo "
<form action = \"test.php\" method = \"post\">
<label for = \"username\">Username: </label>
<input type = \"text\" name = \"username\" />
<label for = \"email\">Email: </label>
<input type = \"text\" name = \"email\" />
<input type = \"submit\" value = \"Register\" />
</form>";
}
?>
答案 2 :(得分:1)
PHP手册很难解释实际发生的事情。 PHP解析器肯定不忽略块外的文本。相反,PHP解析器将该文本转换为echo操作。不相信我?您可以在源代码中亲自查看。在zend_language_parser.y中,您将找到以下行:
| T_INLINE_HTML { zend_do_echo(&$1 TSRMLS_CC); }
当您看到<?php if($condition) { ?><div>...</div><?php } ?>
时,它完全等同于<?php if($condition) { echo '<div>...</div>'; ?>
。
语法可能看起来很奇怪,但在某些情况下它实际上非常有用。例如,我们所有的网页通常共享相同的页眉和页脚。人们经常通过在脚本开头使用include('header.html')和在结尾处使用include('footer.html')来处理这个问题。这不是一个非常灵活的方法。此外,您最终会得到一个HTML文档的两半,这些文档无法在浏览器中正确呈现。
更好的方法是使用函数声明来切片HTML文档:
<?php function printHeader($title = "Default Title", $extraJSScrpts = array(), $extraCSSFiles = array()) { ?>
<html>
<head>
<title>
<?php echo $title; ?>
</title>
</head>
<body>
<div style="position: absolute; left: 150px; top: 35px;">
<?php } ?>
<?php function printFooter() { ?>
</div>
</body>
</html>
<?php } ?>
如果您忽略<?php ... ?>
,那么您拥有的是常规HTML文档。再看一下PHP,你会看到我有两个函数,printHeader()和printFooter()。现在我的所有页面都只调用这两个函数而不是包含。每个页面都可以传递可选参数来处理任何特殊需求(例如,一个额外的样式表)。我是否想要在我的网站上实现不同的主题,这将是非常直接的。我所要做的就是更改我在开头包含的文件,以引入这些函数的不同版本。
答案 3 :(得分:0)
PHP中的控制结构(例如循环,函数等)可以跨成对的结束标记分开。实际上,没有包含在PHP标记中的任何内容只是被发送,就像它被回显一样(或者在当前输出缓冲区中捕获,如果有的话)。
答案 4 :(得分:0)
Try this
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo "<p>Thanks for Registering</p>",
"Username: ", htmlentities ($_POST['username']), "<br />",
"Email: ", htmlentities ($_POST['email']), "<br />";
}
else {
echo '
<form action = "test.php" method = "post">
<label for = "username">Username: </label>
<input type = "text" name = "username" />
<label for = "email">Email: </label>
<input type = "text" name = "email" />
<input type = "submit" value = "Register" />
</form>';
}
?>
答案 5 :(得分:0)
打开和关闭<?php
标签是告诉解释器哪些代码是php代码(因此它应该解释)以及哪些代码是其他代码 - HTML,CSS,JS等。
你会注意到这一点:
echo "<p>Thanks for Registering</p>";
这是一个有效的PHP语句,可以发送给PHP解释器。
然而,稍后在脚本中作者希望输出更多HTML - 他当然可以做这样的事情:
echo '<form action = "test.php" method = "post">
<label for = "username">Username: </label>
<input type = "text" name = "username" />
<label for = "email">Email: </label>
<input type = "text" name = "email" />
<input type = "submit" value = "Register" />
</form>';
但是选择通过使用?>
关闭PHP部分并输出普通HTML来告诉解释器忽略所有这些HTML内容。
他需要完成else {}块,所以用<?
重新打开PHP,添加结束}然后以结束完成?&gt;
答案 6 :(得分:0)
请注意,PHP还提供alternative syntax for control structures,以便在用于控制HTML时提高可读性。以下代码创建一个表,其中包含项目列表中交替的背景颜色:
<table style="width: 100%">
<?php foreach($items as $index => $item): ?>
<?php if(!($index & 0x0001)): // even ?>
<tr style="background-color: #FFFFAA">
<td>
<?php echo $item ?>
</td>
</tr>
<?php else: ?>
<tr style="background-color: #AAFFFF">
<td>
<?php echo $item ?>
</td>
</tr>
<?php endif ?>
<?php endforeach ?>
</table>