检查Isset和Echo''在屏幕上

发布于 2025-02-10 21:06:29 字数 697 浏览 0 评论 0原文

我是一个菜鸟,我开始学习PHP。我制作了一个简单的表格,我想用“ Echo”在浏览器中输入的名称。我发现我必须使用“如果Isset”,然后成功编写了该解决方案。但是我很难将其列入一行,请提供帮助:

<form action="UserInput.php" method="get">
        Name: <input type="text" name="name">
        <input type="submit">
</form>

/* This is working great!
    <?php
    if (isset($_GET['name'])) {
        echo $_GET['name'];
       } else {
        // do nothing
       }
    ?>
*/

我想检查是否输入了“名称”,如果输入echo $ _get ['name'];否则,我希望“名称”的价值为“匿名”。根据文档,这是我写的:

  $name = isset($_GET['name']) ? echo $_GET['name']; : 'anonymous';

我知道我的错误是在echo $ _get ['name'];中。我只是不知道如何用“ Echo”在屏幕上表现出“名称”的显示。

I'm a noob and I started learning PHP. I made a simple form and I wanted to get the name I enter inside the browser with "ECHO". I figured out that I have to use "if isset" and I successfully wrote that solution. But I struggle to put it in one line, please help:

<form action="UserInput.php" method="get">
        Name: <input type="text" name="name">
        <input type="submit">
</form>

/* This is working great!
    <?php
    if (isset($_GET['name'])) {
        echo $_GET['name'];
       } else {
        // do nothing
       }
    ?>
*/

I want to check if 'name' was entered and if entered echo $_GET['name']; otherwise I want the value for 'name' to be 'anonymous'. According to the documentation here's what I wrote:

  $name = isset($_GET['name']) ? echo $_GET['name']; : 'anonymous';

I know my error is in echo $_GET['name'];. I just don't know how to formulate the showing of 'name' on the screen with 'echo'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

冷血 2025-02-17 21:06:29

分配变量时,您不使用echo

$name = isset($_GET['name']) ? $_GET['name'] : 'anonymous';
echo $name;

在现代php中,您可以使用零煤的操作员:

$name = $_GET['name'] ?? 'anonymous';
echo $name;

You don't use echo when you're assigning a variable.

$name = isset($_GET['name']) ? $_GET['name'] : 'anonymous';
echo $name;

In modern PHP you can use the null-coalescing operator:

$name = $_GET['name'] ?? 'anonymous';
echo $name;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文