PHP 的一个小问题
这可能是一个非常简单的答案,但我的大脑完全崩溃了。我现有的代码没有按照我想要的方式显示。我的问题是,如何让以下代码正确显示?
例如,
$toplinks = '<a href="profile.php?id=' . $userid . '">' . $username . '</a> •
<a href="edit_profile.php">Edit info</a> •
<a href="logout.php">Log Out</a>';
当我像这样回显它时,此代码显示完美
<div id="header"><?php echo "$toplinks"; ?></div>
但是此代码的显示与 $toplinks 不同
echo '$toplinks= "<a href="register.php">Register</a> •
<a href="login.php">Log In</a>';
这是我作为一个整体使用的代码
<?php
session_start();
$toplinks = "";
if (isset($_SESSION['id'])) {
$userid = $_SESSION['id'];
$username = $_SESSION['username'];
$toplinks = '<a href="profile.php?id=' . $userid . '">' . $username . '</a> •
<a href="edit_profile.php">Edit info</a> •
<a href="logout.php">Log Out</a>';
} else {
echo '$toplinks= "<a href="register.php">Register</a> •
<a href="login.php">Log In</a>';
}
?>
我尝试用此回显这两个代码
<div id="header"><?php echo "$toplinks"; ?></div>
This might be a very simple answer but my brain is absolutely fried. The code I have in place doesn't display like I want it to. My question is, how would I get the following codes to display properly?
For instance, this code
$toplinks = '<a href="profile.php?id=' . $userid . '">' . $username . '</a> •
<a href="edit_profile.php">Edit info</a> •
<a href="logout.php">Log Out</a>';
Displays perfectly when I echo it out like this
<div id="header"><?php echo "$toplinks"; ?></div>
But this code doesn't display the same as the $toplinks
echo '$toplinks= "<a href="register.php">Register</a> •
<a href="login.php">Log In</a>';
This is the code I have in place as a whole
<?php
session_start();
$toplinks = "";
if (isset($_SESSION['id'])) {
$userid = $_SESSION['id'];
$username = $_SESSION['username'];
$toplinks = '<a href="profile.php?id=' . $userid . '">' . $username . '</a> •
<a href="edit_profile.php">Edit info</a> •
<a href="logout.php">Log Out</a>';
} else {
echo '$toplinks= "<a href="register.php">Register</a> •
<a href="login.php">Log In</a>';
}
?>
And I try to echo these two out with this
<div id="header"><?php echo "$toplinks"; ?></div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此代码不会将任何内容分配给
$toplinks
,因为它位于字符串中。你想要做的是:或者简单地说:
This code does not assign anything to
$toplinks
, as it is in a string. What you want to do is:or simply:
据我所知,您不需要在“else”语句中使用 echo
尝试替换
为
As far as I see, you don't need to use echo in "else" statement
try to replace
with