【发布时间】:2009-11-20 17:13:56
【问题描述】:
我对以下代码的如何工作感到困惑。在我的脑海中,我想象每个 php 块作为一个整体执行并呈现为 HTML。第一个块有点不完整的事实与我想象的 PHP 工作方式不符。 PHP 模块在到达 PHP 结束标记时会做什么? PHP标签内的代码如何影响PHP标签外的明文输出,即仅有条件地输出表单?
我原以为要完成以下操作,您实际上必须使用 echo 语句来有条件地回显表单。
<html>
<head></head>
<body>
<?php
/* if the "submit" variable does not exist, the form has not been submitted - display initial page */
if (!isset($_POST['submit'])) {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Enter your age: <input name="age" size="2">
<input type="submit" name="submit" value="Go">
</form>
<?php
}
else {
/* if the "submit" variable exists, the form has been submitted - look for and process form data */
// display result
$age = $_POST['age'];
if ($age >= 21) {
echo 'Come on in, we have alcohol and music awaiting you!';
}
else {
echo 'You're too young for this club, come back when you're a little older';
}
}
?>
</body>
</html>
【问题讨论】:
标签: php