-2

I've the following php files in the same directory:

connection.php
index.php
lib.php

Relevant code of connection.php:

....
....
$client = 4; //something that I've defined here
....
....

Relevant code of index.php

require_once('connection.php');
require_once('lib.php');
....
function_search(); //function of lib.php
....
....

Relevant code of lib.php

class function_my_exception extends function_your_exception {

    function __construct($hint, $debuginfo=null) {
        parent::__construct($hint, 'debug', '', $hint, $debuginfo);
    }

}
function function_search(){
....
....
if ($client !=4 )
   //Do something 
}

When I run index.php, I keep getting the error Undefined variable $client" in line xxx of lib.php

xan
  • 4,640
  • 13
  • 50
  • 83
  • if ($client !=4 ) --> what is in $client when this code runs? var dump it – wazy Jun 06 '13 at 09:35
  • `$client=4` as I've defined in `connection.php` file. When I merge the 3 files into one, they successfully run. – xan Jun 06 '13 at 09:36
  • that's why I am asking you to var dump it, these are different scopes for that variable isn't it – wazy Jun 06 '13 at 09:37
  • Is `$client` a global variable and you are calling it inside a function? – kevinamadeus Jun 06 '13 at 09:38
  • See what you made me do! [Reference: What is variable scope and which variables are accessible from where?](http://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-and-which-variables-are-accessible-from-where/) – deceze Jun 06 '13 at 10:21

2 Answers2

4

It's because inside the function function_search()

$client is not available!

You can either pass it in as an argument to the function

function function_search($client)
{
...
}

And when you call the function you pass $client in

function_search($client);

Or you can use the global keyword to make it available within the function

Update: This global method should be avoided, because it's possible that somewhere in the code $client gets changed, and causes strange behavior or even errors within your script, it is generally better to pass the arguement to the function as you have more control of the variable.

function function_search()
{
    global $client;
...
}
Dale
  • 10,384
  • 21
  • 34
  • yep pretty much same thing I was trying to get him to see in comments – wazy Jun 06 '13 at 09:39
  • :) I would make some joke about that being outside the scope of the question, but I can't be bothered lol! – Dale Jun 06 '13 at 09:39
  • Just a small note... it is better to pass $client into the `function($client)` rather than global, as it might get changed in some other places! In order to keep old function working, make it `function function_search($client=false)` – kevinamadeus Jun 06 '13 at 09:40
  • It's 3 am so if this question sounds stupid ignore it. But function function_search($client=false) would make $client a local variable for function_search without modifying the global one? – wazy Jun 06 '13 at 09:43
  • @wazy, `function function_search($client = false)` does indeed create a local $client variable for the function and leaves the other one unchanged, it also initializes it with a false value so that if you called `function_search();` the $client argument would be `false`, if you called `function_search(4);` the $client variable would be 4 – Dale Jun 06 '13 at 09:45
  • If I called function_search(); then afterwards the global $client variable is set to false? – wazy Jun 06 '13 at 09:46
  • @wazy, No! just the $client local to the function, the other "global" $client will remain the same. – Dale Jun 06 '13 at 09:47
  • Yea that makes sense, I misread your previous comment and thus asked the followup question. Can't get the brain working anymore this early in the morning. Thanks for the response that clarified it a bit. – wazy Jun 06 '13 at 09:48
  • 1
    It would prbably be better if in the definition another name was used to avoid the confusion :) `function function_search($client_id){..}` – Dale Jun 06 '13 at 09:49
0

Edit your lib.php to this:

class function_my_exception extends function_your_exception {

    function __construct($hint, $debuginfo=null) {
        parent::__construct($hint, 'debug', '', $hint, $debuginfo);
    }
// delete it here

    function function_search(){
    ....
    ....
    if ($client !=4 )
       //Do something 
    }

} // add it here

Note that you had a closing } just after the function __construct. it has to be at the end of those functions.

Kees Sonnema
  • 5,759
  • 6
  • 51
  • 106