[英]How to fix the notice that always showing up when I load the page for the first time?
When I first load the code (I don't input anything), the output will be like this.当我第一次加载代码时(我没有输入任何内容),输出将是这样的。 How do I hide the notice or maybe fix the undefined index?如何隐藏通知或修复未定义的索引?
[notice : undefined index when first load][1] [注意:第一次加载时未定义索引][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>
Check to make sure the target is actually set.检查以确保实际设置了目标。 Put this:把这个:
if(!isset($_GET['no'])) { exit; }
right before the first line in your php block:就在 php 块的第一行之前:
$numberTarget = $_GET['no'];
That way, if $_GET['no'] is empty, the code will terminate without trying to run.这样,如果 $_GET['no'] 为空,代码将终止而不尝试运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.