簡體   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