0

in my php code I get this error

Notice: Undefined variable: browserHeader in connectenparse.class.php on line 12

and my code start here with line 9

private $browserHeader = array ( "'0' => Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
    public function connectenParse($loginPage, $header=''){
        $this->loginPage = $loginPage;
        $this->browserHeader = $browserHeader[$header];
    }

My input is

$run = new connectenParse('http://example.com','0');
echo $run->streamParser();

streamParser function takes the variable end returns it. When I create the class with second parameter which is defined for browser header, it must return Mozilla/5.0. Where is the problem?

  • Are you going to `$this->browserHeader = $header;`? – nerdlyist Mar 07 '17 at 15:14
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – u_mulder Mar 07 '17 at 15:17
  • @nerdlyist Now it returns '0', not the browser header –  Mar 07 '17 at 15:20
  • @u_mulder How did you see any possible dublicate between this question and the linked topic? –  Mar 07 '17 at 15:22
  • What returns 0? is `$header` in your function a string or an index? – nerdlyist Mar 07 '17 at 15:23
  • $this->browserHeader returns 0 –  Mar 07 '17 at 15:25
  • Answer the rest of my question please. Also what do you expect it to return. – nerdlyist Mar 07 '17 at 15:27
  • @nerdlyist I expect it to return "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html" –  Mar 07 '17 at 15:30
  • There are a few issues with your code but I am not sure what you are even attempting to do so providing an answer is nearly impossible without some more clarification as to what you are doing. – nerdlyist Mar 07 '17 at 15:38
  • If you can tell me the issues I can improve it. Thank you. –  Mar 07 '17 at 15:40
  • What are you inputs, what is you expected output and why is browserHeader and array? – nerdlyist Mar 07 '17 at 15:41

3 Answers3

1

The problem is that you are attempting to access the $header element of the $browserHeader array, without ever defining the $browserHeader array:

public function connectenParse($loginPage, $header=''){
    $this->loginPage = $loginPage;
    $this->browserHeader = $browserHeader[$header];
}

On the third line you are assigning a value to $this->browserHeader, which is perfectly fine, but the value you are assigning is $browserHeader[$header]. $header exists, however this method is unaware of any variable called $browserHeader, which is why you're seeing the error. You probable mean to do the following instead:

$this->browserHeader = $header;

Sheraz
  • 160
  • 8
  • It returns '0', I have to take Mozilla/5.0 header. When I create the class with second parameter which is defined for browser header, it must return Mozilla/5.0 –  Mar 07 '17 at 15:25
0

Your $browerheader variable should be like this

private $browserHeader = array ("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html");

If you are passing $header value to a function call, you should use

$this->browserHeader = isset($this->$browserHeader[$header]) ? $this->$browserHeader[$header] : $this->$browserHeader[0];

If $header is a string, you can use it like this

$this->$browserHeader = $header;
Blackus
  • 6,883
  • 5
  • 40
  • 51
Bhaskar Jain
  • 1,651
  • 1
  • 12
  • 20
0

My advise is that you do not need $browserHeader to be an array it is clearly a string and set it as needed.

private $browserHeader;
public function connectenParse($loginPage, $header=''){
    $this->loginPage = $loginPage;
    $this->browserHeader = $header;
}

Then you can call it like

$run = new connectenParse('http://example.com', "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");

If you need the array for multiple other potential headers then you need to fix your array

const BROWSER_HEADER_LIST = array ("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");

Notice it is not ("0=>...") that was making it part of the value. Also it is a const meaning you use it when needed.

So now you can do something like this

//Still have this variable
private $browserHeader;
//use numbers not strings for $header.
public function connectenParse($loginPage, $header=null){
    $this->loginPage = $loginPage;

    if($header !== null){
        $this->browserHeader = self::BROWSER_HEADER_LIST[$header];
    }
}

Also, connectenParse should probably be __construct($loginPage, $header) class name constructors are deprecated.

nerdlyist
  • 2,842
  • 2
  • 20
  • 32
  • Best solution that I ever read. Thank you and thank you. You make my day :) –  Mar 07 '17 at 19:50