PHP 的一个小问题

发布于 2024-11-17 00:56:47 字数 1321 浏览 0 评论 0原文

这可能是一个非常简单的答案,但我的大脑完全崩溃了。我现有的代码没有按照我想要的方式显示。我的问题是,如何让以下代码正确显示?

例如,

$toplinks = '<a href="profile.php?id=' . $userid . '">' . $username . '</a> &bull; 
    <a href="edit_profile.php">Edit info</a> &bull; 
    <a href="logout.php">Log Out</a>';

当我像这样回显它时,此代码显示完美

<div id="header"><?php echo "$toplinks"; ?></div>

但是此代码的显示与 $toplinks 不同

    echo '$toplinks= "<a href="register.php">Register</a> &bull; 
    <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> &bull; 
    <a href="edit_profile.php">Edit info</a> &bull; 
    <a href="logout.php">Log Out</a>';
} else {
    echo '$toplinks= "<a href="register.php">Register</a> &bull; 
    <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

醉生梦死 2024-11-24 00:56:47
<?php
session_start();

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 '<a href="register.php">Register</a> • <a href="login.php">Log In</a>';
}
?>
<?php
session_start();

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 '<a href="register.php">Register</a> • <a href="login.php">Log In</a>';
}
?>
微凉 2024-11-24 00:56:47
echo '$toplinks= "<a href="register.php">Register</a> • 
<a href="login.php">Log In</a>';

此代码不会将任何内容分配给 $toplinks,因为它位于字符串中。你想要做的是:

$toplinks= "<a href="register.php">Register</a> • 
<a href="login.php">Log In</a>"
echo $toplinks

或者简单地说:

echo "<a href="register.php">Register</a> • 
<a href="login.php">Log In</a>"
echo '$toplinks= "<a href="register.php">Register</a> • 
<a href="login.php">Log In</a>';

This code does not assign anything to $toplinks, as it is in a string. What you want to do is:

$toplinks= "<a href="register.php">Register</a> • 
<a href="login.php">Log In</a>"
echo $toplinks

or simply:

echo "<a href="register.php">Register</a> • 
<a href="login.php">Log In</a>"
十雾 2024-11-24 00:56:47

据我所知,您不需要在“else”语句中使用 echo

尝试替换

echo '$toplinks= "<a href="register.php">Register</a> • 
<a href="login.php">Log In</a>';

$toplinks= '<a href="register.php">Register</a> • 
<a href="login.php">Log In</a>';

As far as I see, you don't need to use echo in "else" statement

try to replace

echo '$toplinks= "<a href="register.php">Register</a> • 
<a href="login.php">Log In</a>';

with

$toplinks= '<a href="register.php">Register</a> • 
<a href="login.php">Log In</a>';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文