HTML 代码之间的 PHP 代码中的条件语句 [英] Conditional Statements in PHP code Between HTML Code

查看:42
本文介绍了HTML 代码之间的 PHP 代码中的条件语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 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 )

有什么想法吗?

推荐答案

你可能忘记了 替代控制结构语法:

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.

这篇关于HTML 代码之间的 PHP 代码中的条件语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆