![](/img/trans.png)
[英]Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\res\index.php on line 97 name:
[英]Notice: Undefined offset: 1 in C:\xampp\htdocs\index.php on line 5
這是我的 php 代碼,我搜索了解決方案,但即使添加了var_dump($request);
我得到了同樣的通知< Notice: Undefined offset: 1 in C:\\xampp\\htdocs\\index.php on line 5
>。
索引.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');
}
?>
你能幫我解決這個問題嗎?
最初, $request
不存在。 您添加了一個帶有$request=[]''
元素,因此現在您已經設置了$request[0]
。 此后不久,您將引用$request[1]
而不先對其進行定義。 沒有$request[1]
,這就是為什么您收到此通知的原因。
您要檢查的行是否具有值,並由於未設置而引發通知,您的這一行是:
if ($request[1] == '')
如果要檢查它是否為空而不發出通知,請使用以下命令:
if (empty($request[1]))
如果未設置$request[1]
,將其設置為NULL
,空或0,則返回TRUE
。 因此它應該完成您嘗試做的事情而不會發出通知。
您收到錯誤是因為未設置$ request [1]時會調用它。 如果要將$ request聲明為空數組,請執行以下操作:
$request = array();
如果要檢查它是否已設置或為空
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.