HTML 代码之间的 PHP 代码中的条件语句

2022-01-20 00:00:00 conditional-statements php html

在 PHP 中使用由 HTML 代码分隔的条件语句时遇到了一些问题.这是我正在尝试编写的代码类型.这是一个个人资料页面,只有其个人资料所在的用户才能看到它(我正在使用会话变量进行检查):

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 if(check if user is logged in) ?>
<display users profile in html>
<?php else ?>
<display an error>

但这不起作用.我还尝试通过在 if 末尾放置一个 : 并使用 endif 语句来使用速记符号,但它不起作用.(在早期的项目中,: 方法适用于 foreachendforeach 所以我想我会尝试一下)

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 )

有什么想法吗?

推荐答案

你可能忘记了 endif语法" rel="noreferrer">替代控制结构语法:

You probably forgot the endif of the alternative control structure syntax:

<?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.

相关文章