Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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!

like image 411
PEPPERONI Avatar asked Dec 07 '22 01:12

PEPPERONI


2 Answers

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.

like image 101
mellowsoon Avatar answered May 21 '23 15:05

mellowsoon


Simply declare

global $imagePath;

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

like image 44
VoteyDisciple Avatar answered May 21 '23 14:05

VoteyDisciple