19

Possible Duplicate:
Undefined variable problem with PHP function

Can someone tell me why I keep getting undefined variable error messages in my PHP include files?

<?php

$page = 1;

if (isset($_REQUEST['page'])) {
  $page = $_REQUEST['page'];
}

function phpRocks() {
  require("includes/dostuff.php");
}

if ($search) {
  phpRocks();
}

?>

Then in dostuff.php:

<?php echo $page; ?>

This is the error I'm getting:


Notice: Undefined variable: page in /dostuff.php on line 61

Attn down voters/close requesters: Doesn't show any research effort? How so? What else should I have added? I have been stumped over this for a half hour and cannot find any other posts that answer this question. Do I need to be a PHP expert in order to post questions (therefore I wouldn't be posting any questions!)??

4
  • 2
    Variable scope. You are declaring in the global scope, but open the template from a function with its own local var scope. Commented Nov 9, 2012 at 15:58
  • are you sure $page is getting set? Commented Nov 9, 2012 at 15:58
  • 1
    Where is phpRocks() called? Commented Nov 9, 2012 at 16:00
  • 3
    While both the author of this question and the author of the duplicate question have had the same problem, this question's title mentions PHP's include mechanism while the other doesn't. I had a problem with file inclusion which the duplicate question won't answer, therefore it is not a duplicate IMO. Commented Jun 21, 2013 at 8:50

4 Answers 4

17

mario's got it. Do this:

function phpRocks() {
    global $page;

    require("includes/dostuff.php");
}
Sign up to request clarification or add additional context in comments.

1 Comment

Read up on scope Zoolander, thats your issue here
8

You are including the file inside a function. Therefore the scope of all the included code is the scope of the function. The variable $page does not exist inside the function. Pass it in:

function phpRocks($page) {
    require "includes/dostuff.php";
}

phpRocks($page);

5 Comments

Why am I the only one here rooting for injection rather than global spaghetti?
Everyone else put Mario's comment into answer form for the easy reputation. You're the only one who actually added something relevant.
I'm not sure this is the best solution though. $page should be a global variable here IMO ... it's a variable created in the global scope which an included file needs to use. The function call doesn't have anything to do with that. Plus he might want to pass "real" arguments to the function in the future, to be used outside of dostuff.php. This avoids confusion between the two.
@sgroves It has everything to do with the function. The function introduces a new scope, which is good. Any values that cross that scope boundary should be explicitly passed in and returned back out. Whatever happens inside the function with those values is irrelevant. It's also irrelevant that the included file "thinks" the variable is global, nothing changes for the included file either way. You're binding the function to surrounding scope it has no influence on. That's code coupling, which should always be avoided.
I believe that global is a better solution in this case.
2

add global var in you function like that

function phpRocks() {
  global $page;
  require("includes/dostuff.php");
}

Comments

1

You have to declare the variable to global like this:

function phpRocks() {
global $page;           //set variable to global
require("includes/dostuff.php");
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.