HTML 代码之间的 PHP 代码中的条件语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3812526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Conditional Statements in PHP code Between HTML Code
提问by 0xff0000
I'm having a bit of an issue by using Conditional Statements in PHP separated by HTML code. This is the type of code I'm trying to write. This is a profile page and it should only be seen by the user whose profile it is (i'm using session variables for checking that) :
我在 PHP 中使用由 HTML 代码分隔的条件语句时遇到了一些问题。这是我正在尝试编写的代码类型。这是一个个人资料页面,它只能由其个人资料所在的用户看到(我正在使用会话变量进行检查):
<?php if(check if user is logged in) ?>
<display users profile in html>
<?php else ?>
<display an error>
But this doesn't work. I also tried using the shorthand notation by putting a :
at the end of the if
and using the endif
statement, but it didn't work. ( On an earlier project , the :
method worked for foreach
and endforeach
so I thought I would try it out )
但这不起作用。我还尝试通过:
在 the 末尾放置 aif
并使用该endif
语句来使用速记符号,但它没有用。(在早期的项目中,该:
方法有效foreach
,endforeach
所以我想我会尝试一下)
Any Ideas ?
有任何想法吗 ?
回答by Gumbo
You probably forgot the endif
of the alternative control structure syntax:
你可能忘了endif
的的替代控制结构的语法:
<?php if(check if user is logged in): ?>
<display users profile in html>
<?php else: ?>
<display an error>
<?php endif; ?>
Omitting the braces as you wrote is not possible. It is only possible if it is followed by a regular statement.
像你写的那样省略大括号是不可能的。仅当其后跟有常规语句时才有可能。
回答by Spudley
PHP has two styles of notation for if() blocks (and blocks in general).
PHP 有两种用于 if() 块(和一般块)的符号样式。
Firstly, you have the wordy notation, which involves explicitly stating endif;
at the end of the if() block. It looks like this:
首先,你有冗长的符号,它涉及endif;
在 if() 块的末尾明确说明。它看起来像这样:
if(whatever):
do something
else:
do something else
endif;
The colons at the end of the if
and else
lines are important, because otherwise PHP thinks you're using the other notation (below).
if
和else
行末尾的冒号很重要,否则 PHP 会认为您正在使用其他符号(如下所示)。
Secondly, you have the curly-braces notation, which looks similar to C or Perl style code, and looks like this:
其次,您有花括号表示法,它看起来类似于 C 或 Perl 风格的代码,如下所示:
if(whatever) {
do something
} else {
do something else.
}
With this style notation, you are allowed to leave the pairs of curly-braces off if your block is only going to be one line long. (I personally think it's bad practice to leave them off like this, but plenty of people swear by it, and it is perfectly valid syntax. But I've seen PHP get confused over single-line blocks when you're switching between PHP code and HTML; this is why I always prefer to use braces even if I'm only writing one line).
使用这种样式表示法,如果您的块只有一行长,您可以不使用花括号对。(我个人认为像这样离开它们是不好的做法,但很多人都发誓,它是完全有效的语法。但我看到当你在 PHP 代码之间切换时,PHP 对单行块感到困惑和 HTML;这就是为什么我总是喜欢使用大括号,即使我只写一行)。
The problem in your case is that you've mixed the two notations. You's trying to us the wordy notation, but don't have the colons on the lines, so PHP thinks you mean the braces notation. But because the braces notation allows for the braces to be missed out, PHP is seeing your code as valid syntax, even if it isn't going to work quite as you planned.
您的情况的问题是您混合了两种符号。您正在尝试使用冗长的表示法,但行上没有冒号,因此 PHP 认为您指的是大括号表示法。但是因为大括号表示法允许忽略大括号,PHP 会将您的代码视为有效语法,即使它不会按您的计划正常工作。
Your solution is to tidy it up so that you are definitely using one syntax or the other. Either add braces ({
and }
}) to the start and end of each block as shown in my example, or add colons and an endif;
line.
您的解决方案是整理它,以便您肯定使用一种语法或另一种语法。如我的示例所示,在每个块的开头和结尾添加大括号({
和}
}),或者添加冒号和endif;
一行。
So your code should look like one of these two examples:
因此,您的代码应类似于以下两个示例之一:
<?php if(check if user is logged in): ?>
<display users profile in html>
<?php else: ?>
<display an error>
<?php endif; ?>
or...
或者...
<?php if(check if user is logged in) { ?>
<display users profile in html>
<?php } else { ?>
<display an error>
<?php } ?>
Hope that helps.
希望有帮助。
回答by Ruel
use braces {
and }
.
使用大括号{
和}
。
<?php if(check if user is logged in) { ?>
<display users profile in html>
<?php } else { ?>
<display an error>
<?php } ?>
回答by You
Your initial thought was correct. There are two ways of doing this, as per the PHP documentation:
你最初的想法是正确的。根据PHP 文档,有两种方法可以做到这一点:
<?php if($loggedin): ?>
<p>User is logged in.</p>
<?php else: ?>
<p>User is not logged in.</p>
<?php endif; ?>
Or:
或者:
<?php if($loggedin){ ?>
<p>User is logged in.</p>
<?php }else{ ?>
<p>User is not logged in.</p>
<?php } ?>
回答by Aaron W.
Try putting in brackets to separate the conditions.
尝试放入括号以分隔条件。
<?php if(check if user is logged in) { ?>
<display users profile in html>
<?php } else { ?>
<display an error>
<?php } ?>
回答by The Surrican
use brackets because to the php interpreter this spans multiple lines
使用方括号,因为对于 php 解释器,它跨越多行
<?php if(check if user is logged in) { ?>
<display users profile in html>
<?php } else { ?>
<display an error>
<?php } // else ?>
回答by NS23
Another way to conditionally render a template is by using include
有条件地呈现模板的另一种方法是使用 include
Example
例子
if($condition)
{
include 'page1.php';
}else{
include 'page2.php';
}