2

I am getting PHP notice error. This code was working fine in php 5.3 but then I upgraded my PHP to PHP 7. What I'm trying to do is, fetch the URL from the link, and just display the parameters attached with the URL. Here is the code.

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";
    }
?>

Output on going to URL: localhost/myDir/index.php/abc/def

Notice: Undefined index: controller in /srv/http/myDir/bootstrap.php on line 8
Notice: Undefined index: action in /srv/http/myDir/bootstrap.php on line 14

Home
index

Bivek
  • 1,380
  • 10
  • 24
  • Possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) –  Apr 08 '16 at 09:53

3 Answers3

3

Test for empty() ... that will be true for 0, '0', false, '', empty array() ... and the notice is gone also! ... Do the same for your other ifs and array indexes!

if(empty($this->request['action'])) {

To avoid similar warnings, you should also provide a default value in your methods, functions, etc. :

function ($arg=FALSE, $arg2=TRUE, $arg3=5, ...) {
djot
  • 2,952
  • 4
  • 19
  • 28
0

If your code is working fine & issue is only to remove notice error then you can use error_reporting(0) in php script.

Add error_reporting(0) as a first statement in your php script

Vivek Pipaliya
  • 488
  • 1
  • 7
  • 17
  • Its very bad 'workaround' if he ever has to migrate to other programming language like C or Java. It just hides the error/warning but not solves it. –  Apr 08 '16 at 09:58
  • NO! That's bad programming practice. You can do that when going online (but then, all those warnings and notices should be obsolete anyway) – djot Apr 08 '16 at 09:59
  • Actually it's not working. It should display abc instead of Home and def instead on index. So i guess the error is effecting output as well. – Bivek Apr 08 '16 at 10:00
0

Test if isset : isset($this->request['action']) isset($this->request['controller'])

Like this :

<?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";
    }
?>
DevLoots
  • 747
  • 1
  • 6
  • 21