如何解决此 PHP 通知错误?
How to solve this PHP notice error?
我收到 PHP 通知错误。这段代码在 php 5.3 中运行良好,但后来我将 PHP 升级到 PHP 7。我想做的是,从 link,只显示URL附带的参数。这是代码。
index.php
<?php
require_once('bootstrap.php');
$bootstrap = new Bootstrap($_GET);
?>
bootstrap.php
<?php
class Bootstrap{
private $controller;
private $action;
private $request;
public function __construct($request){
$this->request = $request;
if($this->request['controller'] == ''){
$this->controller = "Home";
}
elseif($_GET($request['controller'])){
$this->controller = $this->request['controller'];
}
if($this->request['action'] == ''){
$this->action = "index";
} else{
$this->action = $this->request['action'];
}
echo "<br />$this->controller<br />$this->action";
}
?>
转到 URL 时的输出:localhost/myDir/index.php/abc/def
注意:未定义索引:controller in /srv/http/myDir/bootstrap.php on line 8
注意:未定义索引:第 14
行 /srv/http/myDir/bootstrap.php 中的操作
首页
指数
测试 empty()
... 将 true
用于 0、'0'、false、''、空数组()
...而且通知也不见了!
...对您的其他 ifs 和数组索引执行相同的操作!
if(empty($this->request['action'])) {
为了避免类似的警告,您还应该在您的方法、函数等中提供一个默认值。:
function ($arg=FALSE, $arg2=TRUE, $arg3=5, ...) {
如果您的代码工作正常并且问题只是为了消除通知错误,那么您可以在 php 脚本中使用 error_reporting(0)
。
将 error_reporting(0)
添加为 php 脚本中的第一个语句
测试是否设置:
isset($this->request['action'])
isset($this->request['controller'])
像这样:
<?php
class Bootstrap{
private $controller;
private $action;
private $request;
public function __construct($request){
$this->request = $request;
foreach ($request as $key => $value) {
echo $key . " = " . $value;
}
if(isset($this->request['controller']) && $this->request['controller'] == ''){
$this->controller = "Home";
}
elseif(isset($this->request['controller']) && $_GET($request['controller'])){
$this->controller = $this->request['controller'];
}
if(isset($this->request['action']) && $this->request['action'] == ''){
$this->action = "index";
}
else{
$this->action = $this->request['action'];
}
echo "<br />$this->controller<br />$this->action";
}
?>
我收到 PHP 通知错误。这段代码在 php 5.3 中运行良好,但后来我将 PHP 升级到 PHP 7。我想做的是,从 link,只显示URL附带的参数。这是代码。
index.php
<?php
require_once('bootstrap.php');
$bootstrap = new Bootstrap($_GET);
?>
bootstrap.php
<?php
class Bootstrap{
private $controller;
private $action;
private $request;
public function __construct($request){
$this->request = $request;
if($this->request['controller'] == ''){
$this->controller = "Home";
}
elseif($_GET($request['controller'])){
$this->controller = $this->request['controller'];
}
if($this->request['action'] == ''){
$this->action = "index";
} else{
$this->action = $this->request['action'];
}
echo "<br />$this->controller<br />$this->action";
}
?>
转到 URL 时的输出:localhost/myDir/index.php/abc/def
注意:未定义索引:controller in /srv/http/myDir/bootstrap.php on line 8
注意:未定义索引:第 14
首页
指数
测试 empty()
... 将 true
用于 0、'0'、false、''、空数组()
...而且通知也不见了!
...对您的其他 ifs 和数组索引执行相同的操作!
if(empty($this->request['action'])) {
为了避免类似的警告,您还应该在您的方法、函数等中提供一个默认值。:
function ($arg=FALSE, $arg2=TRUE, $arg3=5, ...) {
如果您的代码工作正常并且问题只是为了消除通知错误,那么您可以在 php 脚本中使用 error_reporting(0)
。
将 error_reporting(0)
添加为 php 脚本中的第一个语句
测试是否设置:
isset($this->request['action'])
isset($this->request['controller'])
像这样:
<?php
class Bootstrap{
private $controller;
private $action;
private $request;
public function __construct($request){
$this->request = $request;
foreach ($request as $key => $value) {
echo $key . " = " . $value;
}
if(isset($this->request['controller']) && $this->request['controller'] == ''){
$this->controller = "Home";
}
elseif(isset($this->request['controller']) && $_GET($request['controller'])){
$this->controller = $this->request['controller'];
}
if(isset($this->request['action']) && $this->request['action'] == ''){
$this->action = "index";
}
else{
$this->action = $this->request['action'];
}
echo "<br />$this->controller<br />$this->action";
}
?>