[英]Notice: Undefined offset: 1 in C:\xampp\htdocs\index.php on line 5
Here is my php code,i searched about the solution but even after adding var_dump($request);
这是我的 php 代码,我搜索了解决方案,但即使添加了
var_dump($request);
i got the same Notice< Notice: Undefined offset: 1 in C:\\xampp\\htdocs\\index.php on line 5
>.我得到了同样的通知<
Notice: Undefined offset: 1 in C:\\xampp\\htdocs\\index.php on line 5
>。
index.php索引.php
<?php
$rd = dirname(__FILE__);
$request[]='';
var_dump($request);
if ($request[1] == '')
{
$request[1] = 'header';
include($rd.'/php_includes/'.$request[1].'.php');
}
if ($request[0] == '')
{
$request[0] = 'index';
include($rd.'/php_includes/'.$request[0].'.php');
}
?>
Could you please Help me out with this?你能帮我解决这个问题吗?
Initially, $request
doesn't exist. 最初,
$request
不存在。 You add one element with $request=[]''
, so now you have $request[0]
set. 您添加了一个带有
$request=[]''
元素,因此现在您已经设置了$request[0]
。 Shortly thereafter, you reference $request[1]
without it being first defined. 此后不久,您将引用
$request[1]
而不先对其进行定义。 There is no $request[1]
, which is why you're getting this notice. 没有
$request[1]
,这就是为什么您收到此通知的原因。
Your line that checks to see if it has a value, and throws the notice because it's not set, is this one: 您要检查的行是否具有值,并由于未设置而引发通知,您的这一行是:
if ($request[1] == '')
If you want to check to see if it's empty without throwing a notice, use this: 如果要检查它是否为空而不发出通知,请使用以下命令:
if (empty($request[1]))
This will return TRUE
if $request[1]
is not set, is set to NULL
, empty, or 0; 如果未设置
$request[1]
,将其设置为NULL
,空或0,则返回TRUE
。 so it should accomplish what you're trying to do without throwing a notice. 因此它应该完成您尝试做的事情而不会发出通知。
You are getting the error because youare calling $request[1] when it is not set. 您收到错误是因为未设置$ request [1]时会调用它。 If you want $request to be declared as an empty array do it like:
如果要将$ request声明为空数组,请执行以下操作:
$request = array();
if you want to check if it is set or empty 如果要检查它是否已设置或为空
if (!isset($request[1]) || empty($request[1]))
{
//your code here in case of not existing or being empty
}
$request[] = ''; // adds a value to the next key - they are autogenerated from 0 as int 0 1 2 3 4 5 6 and so on, if you don't declare them otherwise
<?php
include("db.php");
$query = "SELECT * FROM `tbl_user` ORDER BY id DESC";
$statement = mysqli_query($connect,$query);
// if(count($data)>0){
while($row = mysqli_fetch_assoc($statement))
{
echo $row[1];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.