views:

36

answers:

3

So, I have a Wordpress theme.

In the functions.php file, I'm writing a function that looks something like this:

function my_function(){
    $var1 = 'apple';
    $var2 = 'orange';
    include get_bloginfo('stylesheet_directory').'/lib/fruits.php';
}

And the fruits.php file looks like this:

<?php

echo "My brother's favorite fruit is an $var1, but my favorite fruit is an $var2.";

?>

When I call my_function() somewhere in my theme, the echo statement in fruits.php displays on the screen, but the values of $var1 and $var2 do not display. They're simply blank.

Now here's the strange part. If I move fruits.php to the same directory as functions.php, and change the include statement in my_function() to this:

include 'fruits.php';

the 2 variables display just fine.

Any ideas what could be causing this problem?

FYI, I did try defining the path to fruits.php in a variable and then trying

include $path;

to no avail.

+2  A: 
global $var1;
global $var2;

http://php.net/manual/en/language.variables.scope.php

Jason
Where is he supposed to put these global statement?
webbiedave
+1  A: 

There is an issue here with the scope of the variables in the function. Basically you have a file, lets say index.php within which you include the functions.php from some other directory. In that functions.php you define two variables within a function scope - meaning the variables are only visible within the function. In addition this function includes the fruits.php file.

Since fruits.php is technically included by the index.php, it looks for the variables $var1 and $var2 within index.php and they have not been defined there because they have been defined within a scope of the function. Basically it looks for a variables which it cannot see.

Here are two ways to solve this:

Method 1:

Modify the function to the following.

function my_function(){
    global $var1 = 'apple';
    global $var2 = 'orange';
    include get_bloginfo('stylesheet_directory').'/lib/fruits.php';
}

This way you are forcing the variables not to be visible within the function, but to be global, or in other words to be visible within the whole file.

Method 2:

Define the variables outside of the function.

$var1 = 'apple';
$var2 = 'orange';
function my_function(){
    include get_bloginfo('stylesheet_directory').'/lib/fruits.php';
}

For more information regarding variable scope, you can go here http://php.net/manual/en/language.variables.scope.php

Hope this helps.

miki725
Your statement `it looks for the variables $var1 and $var2 within index.php` is incorrect. Since OP includes the file within the function, it inherits the same scope as the function. Also you cannot perform assignments along with the global keyword as you are trying to do in method 1. Your method 2 will ensure that the variables will not be accessible to the included code as they are declared outside of the function.
webbiedave
What is OP? Regarding method 2, I disagree. If in `functions.php` you declare two variables `$var1` and `$var2` and in `index.php` you do `require_once("dir/functions.php")` then the two variables are also declared in `index.php` and therefore `fruits.php` can access them. I will try out the code today for both methods to see if PHP gives any errors for both ways. Ill edit the answer to let people know what happened.
miki725
Thanks for your detailed answer. Webbiedave was right, though, in his original comment to me. Please see above.
mjsiemer
@miki725: I assure you that you are incorrect. The included `fruits.php` will inherit the scope of the function. From the PHP manual: `If the include occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function.` OP = original poster, i.e., person who asked the question.
webbiedave
A: 

You're prepending the include directory with wordpress stylesheet_directory (rather than STYLESHEETPATH constant for example) which is a URL, not a file path so it shouldn't be used to include files in PHP. Change this to correctly include the file.

webbiedave