繁体   English   中英

怎么解决第一次加载页面时总是出现的提示?

[英]How to fix the notice that always showing up when I load the page for the first time?

当我第一次加载代码时(我没有输入任何内容),输出将是这样的。 如何隐藏通知或修复未定义的索引?

[注意:第一次加载时未定义索引][1]

<h5 id="result" name="result" class="font-weight-bold mt-4">
                                <?php  
                                        $numberTarget = $_GET['no'];
                                        $anArray = array(100, 44, 2, 80, 5, 13, 11, 4, 120);
                                        
        
                                        function linearSearch($anArray, $element) {
                                            for ($i = 0; $i < sizeof($anArray); $i++) {
                                                    if ($anArray[$i] == $element) {
                                                        return $i;
                                                    }   
                                                }  
                                                    return -1;
                                        };
        
                                        $output = linearSearch($anArray, $numberTarget);
                                    
                                        if($output != -1) {
                                            echo $numberTarget, " found at index ", $output;
                                        }
                                        else if ($output = -1) {
                                            echo "not found";
                                        }

                                ?>  
    
                                </h5>

检查以确保实际设置了目标。 把这个:

if(!isset($_GET['no'])) { exit; }

就在 php 块的第一行之前:

$numberTarget = $_GET['no'];

这样,如果 $_GET['no'] 为空,代码将终止而不尝试运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM