如何解决这个PHP通知错误?
我收到PHP错误通知。这段代码在PHP5.3中运行良好,但后来我将php升级到PHP7。我试图做的是,从链接中获取URL,然后只显示URL附带的参数。这是代码 index.php如何解决这个PHP通知错误?,php,compiler-errors,get,notice,Php,Compiler Errors,Get,Notice,我收到PHP错误通知。这段代码在PHP5.3中运行良好,但后来我将php升级到PHP7。我试图做的是,从链接中获取URL,然后只显示URL附带的参数。这是代码 index.php <?php require_once('bootstrap.php'); $bootstrap = new Bootstrap($_GET); ?> <?php class Bootstrap{ private $controller; private $act
<?php
require_once('bootstrap.php');
$bootstrap = new Bootstrap($_GET);
?>
<?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";
}
?>
bootstrap.php
<?php
require_once('bootstrap.php');
$bootstrap = new Bootstrap($_GET);
?>
<?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
注意:未定义索引:第8行/srv/http/myDir/bootstrap.php中的控制器注意:第14行的/srv/http/myDir/bootstrap.php中的未定义索引:action 主页
索引测试
空()。。。对于0,'0',false',空数组()
... 通知也不见了!
... 对其他ifs和数组索引执行相同的操作
if(empty($this->request['action'])) {
为避免类似警告,还应在方法、函数等中提供默认值:
function ($arg=FALSE, $arg2=TRUE, $arg3=5, ...) {
如果您的代码工作正常&问题只是删除通知错误,那么您可以在php脚本中使用错误报告(0)
添加错误报告(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";
}
?>
如果他不得不迁移到其他编程语言(如C或Java),那么可能会重复非常糟糕的“变通方法”。它只是隐藏错误/警告,而不是解决它。不!这是糟糕的编程实践。你可以在上网时这样做(但是,所有这些警告和通知都应该过时),实际上它不起作用。它应该在索引上显示abc而不是Home和def。所以我猜这个错误也会影响输出。