初学者php“警告:未定义的数组键" [英] Beginner php "Warning: Undefined array key"

查看:138
本文介绍了初学者php“警告:未定义的数组键"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在学习PHP,并且不断收到警告:未定义的数组键".在$ GET _ ["fname"]和$ GET _ ["age"]中:

So, I'm learning PHP and I keep getting "Warning: Undefined array key" in $GET_["fname"] and $GET_["age"]:

<main>

    <form action="inputs.php" method="get">
        Name: 
        <br/>
        <input type="text" name="fname">
        <br/>
        Age:
        <br/>
        <input type="number" name="age">
        <br/>
        <input type="submit" name="submit">

    </form>
        <br/>
        Your name is <?php echo $_GET["fname"]; ?>
        <br/>
        Your age is <?php echo $_GET["age"]; ?>

</main>

推荐答案

我假设您想知道如何摆脱此错误消息.

I'll assume you want to know how to get rid of this error message.

第一次加载此页面时,将显示一个表单,并且$ _GET为空(这就是它触发警告的原因).然后,您提交表单,并将fname和age参数添加到url中(因为表单的方法是'get').

The first time you load this page you display a form and $_GET is empty (that's why it is triggering warnings). Then you submit the form and the fname and age parameters will be added to the url (because your form's method is 'get').

要解决您的问题,您可以将两行换成一些if语句,例如:

To resolve your issue you could wrap the two lines inside some if-statement, for example:

<?php if(isset($_GET['fname']) && isset($_GET['age'])): ?>
        <br/>
        Your name is <?php echo $_GET["fname"]; ?>
        <br/>
        Your age is <?php echo $_GET["age"]; ?>
<?php endif; ?>

这篇关于初学者php“警告:未定义的数组键"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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