1

I'm including this file to collect one of its variables. File path is correct and i don't get file include errors. But when i try to print a variable inside that file it gives undefined variable error. Following is the code.

include_once($path . "folder/file.php");
 echo($code);

There is a php class. inside the class there is a login function . Within that function I'm including another file assume it's funtions.php.

functions.php has above code in it.

Inside functions.php file i include this file which contains the variable i'm looking for (assume it's test.php).

test.php looks like this (inside php tags)

     $code="something";
     $another_variable="something else";

so now like i said before when i include this inside functions.php and print $code it why does it gives an undefined error?

Full code

 function log($user, $pass) {

     global $config;
    $this->get_available_accounts();
    if (isset($this->Users_obj[$user])) {
        if ($this->Users_obj[$user]->userName == $user && $this->Users_obj[$user]->passWord == $pass) {
            delete_cache_full();
            $_SESSION['username'] = $user;
            $_SESSION['log'] = true;
            $_SESSION['usergroup']=$this->Users_obj[$user]->level;
            $this->set_permission_session($_SESSION['usergroup']);
            include_once $config->path . 'config.php';
            $newUpdate2=showUpdates2();
            if(!empty($newUpdate2)){
                $_SESSION['updateremindlater']=$newUpdate2['date'];
                }
            //file  
            include_once $config->path . 'functions.php';
            $func = new UserFuncs();
            $func->validate();
            include_once $config->path  . 'view/ViewDashboard.php';
            return true;
        }
    }

thats the function im including this file into. include_once $config->$path . 'functions.php'; includes functions.php file functions.php looks like this

   include_once($path. "folder/config.php");
   echo($code);

and config.php looks like

  $code = "ERGERW2342HV3453WERWEER";
  $host_name = "SERV345WERFDGDDFGDGF";
  $return_code = "DFGDFSDGFS";
  $vurl = "YUIYUJHGJ";
  $next_val ="AWERFDFVDV";
  $val_exp = "NMGHJGJGH";

Help much appreciated!

4
  • 5
    You'll have to show your complete code for that to be answerable. Either your scope is wrong, or you're unsetting the variable or something else is being done which we can't determine from the snippet you show. Commented Apr 5, 2012 at 7:34
  • 2
    I think deceze is right, because the description your give is very hard to follow and it might be that you have an understanding problem which does not make it easy to describe the issue specifically. So best thing would be you share the whole code. Alternatively draw an image, that's probably even better: Visualize how includes are done. Also include and include_once do different things, so take care about the differences. Commented Apr 5, 2012 at 7:46
  • Can you post the output of var_dump($config->path); to check that it contains what it should contain? :) Commented Apr 5, 2012 at 8:34
  • possible duplicate of Passing PHP variables to an included file? Commented Oct 27, 2012 at 15:12

2 Answers 2

3

Just guessing that you've included config.php somewhere else before, and that it's not being included again due to include_once. Therefore the variables are not created in the scope you're including it in the second time.

Sign up to request clarification or add additional context in comments.

Comments

2

This could be a problem with variable scope, where you are trying t access the variable from somewhere (e.g. a function) where it is not available to that particular object (you did mention that you included the file within a class). I agree with deceze and hakre, it's very hard to answer this question without seeing your code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.