PEPPERONI
PEPPERONI

Reputation: 51

How to use variables from one PHP file in another file's function?

NOTE: I have read about 25 questions thus far that are peripherally related to this, but never answer the question directly of HOW TO USE THOSE VARIABLES CORRECTLY. I apologize if it sounds like a duplicate.

I am rewriting a flat e-commerce site (all hand-made.htm files!) so that some of the pages are generated using PHP scripts. This is all just to make my life easier for now. I have told my boss repeatedly that this site needs to be nuked from orbit, and there is a full-blown CMS in the pipeline. In the meantime this site NEEDS to be revamped. Site in question: http://www.leather.com

I have been using a constructor_page.php that declares and initializes a few variables such as $itemName and $imagePath that are used in include()'d content pages (trimmed example follows):

<?php
$imagePath = 'http://leather.com/images/harley_leather_boots/d81024_ladies_harley_leather_boots_360.jpg';
$itemName = 'Faded Glory Harley Boots';
include 'library/content.php'; //this in turn includes more files like the nav bar
?>

This way I just save constructor_page.php as the item page in question, modify a few variables, and the include() statements take care of the rest. This part works okay.

My issue is with generating "department" pages automatically from the files in the same directory. I would like the index.php to grab those same variables in a loop:

<?php
foreach (glob("*.php") as $filename) {
include $filename;
echo '<div class="related">';
echo '<a href="';
echo $filename; 
echo '">';
echo '<img src="';
echo $imagePath;
echo '" alt="';
echo $imageAlternateText;
echo '" class="frame-related align-left-related"';
echo '></a>';
echo '<a href="';
echo $filename; 
echo '">';
echo $itemName; 
echo '</a>';
echo '</div>';
}
?>

This is intended to, for each php file (yes I know it'll recurse to itself), grab the relevant variables and create a nice picture/link inside a that floats left, in essence generating the department page thumbnails automatically based on whatever php files it finds in there.

Yes, it's horrible. Yes, it's hackish. Needless to say, it does not work due to the scope of the variables. I am open to suggestion here, as all I need is for the department page to generate little pictures and links based on what it finds in the folder SOME HOW. Thanks in advance!

Upvotes: 5

Views: 30136

Answers (3)

mellowsoon
mellowsoon

Reputation: 23301

You must have some other problem. Let me demonstrate:

<?php // This is index.php
$foo = 'bar';
include('page2.php');
?>


<?php // This is page2.php
include('page3.php');
?>

<?php // This is page3.php
echo $foo;
?>

If you go to index.php, it will display "bar". Included PHP scripts inherit the scope where they were included. There's no need to use a "global" statement at any time. If however your page3.php script looked like this:

<?php // This is page3.php
function display() {
    echo $foo;
}
display();
?>

That wouldn't display anything, because now $foo is in the scope of the display() function, so it would need to be rewritten like this:

<?php // This is page3.php
function display() {
    global $foo;
    echo $foo;
}
display();
?>

So you must be doing something else wrong if your variables aren't available in other scripts.

Upvotes: 14

cointilt
cointilt

Reputation: 728

Like VoteyDIsciple said, you have to declare it at the top of the page first as being a global variable like so:

global $imagePath;

$imagePath = 'path/to/the/image.jpg';

Then on any page you want to use the global variable you must reference it as being global first before using it.

So on the page you are using echo $imagePath; you must first do this again.

global $imagePath;

Global variables get pretty dangerous so I would try looking into a more MVC type of framework where your variables you create in your Controllers get passed into your view files. http://en.wikipedia.org/wiki/Model%E2%80%93View%E2%80%93Controller

A good MVC Framework I enjoy developing in is Codeigniter http://codeigniter.com

Upvotes: 0

VoteyDisciple
VoteyDisciple

Reputation: 37813

Simply declare

global $imagePath;

(And likewise for other variables you want to use in more than one script or function.)

Upvotes: 3

Related Questions