gpt4 book ai didi

PHP 条件后跟结束标记

转载 作者:可可西里 更新时间:2023-11-01 13:12:52 26 4
gpt4 key购买 nike

我对下面的代码如何 工作感到有点困惑。在我的脑海中,我想象每个 php block 作为一个整体执行并呈现为 HTML。事实上,第一个 block 有点不完整,带有一个悬挂的支架,这与我想象的 PHP 工作方式不太相符。当 PHP 模块到达 PHP 结束标记时会做什么? PHP 标签内的代码如何影响 PHP 标签外的明文输出,即仅有条件地输出表单?

我原以为要完成以下操作,您实际上必须使用回显语句来有条件地回显表单。

<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 manual解释得很体面:

...when PHP hits the ?> closing tags, it simply starts outputting whatever it finds (except for an immediately following newline - see instruction separation ) until it hits another opening tag ... but for outputting large blocks of text, dropping out of PHP parsing mode is generally more efficient than sending all of the text through echo() or print()...

关于PHP 条件后跟结束标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1765242/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com