【问题标题】:How do I echo a php variable inside html tags inside an if statement? [duplicate]如何在 if 语句中的 html 标签内回显 php 变量? [复制]
【发布时间】:2016-03-15 08:04:45
【问题描述】:

我在尝试让它工作时遇到了麻烦。

 <?php

   $_SESSION['age'] = 11

        if($_SESSION['age'] < 14) {
            echo "<h2>Your only $_SESSION['age']? Thats Young!</h2>";
        }
    ?>

【问题讨论】:

    标签: php html variables if-statement echo


    【解决方案1】:

    如果你想在echo函数中回显PHP变量,你必须首先用"中断回显,然后添加一个“合并字符”.,然后是你的变量,然后再次“合并字符”和@ 987654324@.

    另外不要忘记在包含$_SESSION['age'] 定义的行尾添加分号。

    将您的代码编辑为:

     <?php
    
       $_SESSION['age'] = 11;
    
            if($_SESSION['age'] < 14) {
                echo "<h2>Your only ".$_SESSION['age']."? Thats Young!</h2>";
            }
    ?>
    

    【讨论】:

    • $_SESSION['age'] = 11 语法错误
    • $_SESSION['age'] = 11; 添加到行尾(在我的代码中编辑)
    • 好,还要提一点,确保你使用的是 session_start();否则将无法正常工作。
    • 如果您使用会话变量,您不应该获取 $_SESSION['age'] 变量而不是设置它吗? (例如 $age = $_SESSION['age']);否则,引用 $_session 变量没有任何好处。
    • 另外,这不是我的完整代码。我正在使用 html 表单和 php 制作一个快速游戏。年龄在不同页面的其他地方定义:P。我刚刚添加了“$_SESSION['age' = 11;”供参考。
    猜你喜欢
    • 2017-12-28
    • 2016-11-18
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    • 2023-01-10
    • 1970-01-01
    • 1970-01-01
    • 2019-05-21
    相关资源
    最近更新 更多