Jump to content

Please help me understand variable scope in this context...


Donny Bahama

Recommended Posts

Let's say I have main.php which consists of:

<?php
require "part1.php";
require "part2.php";
require "part3.php";
?>

If I define/set a variable in part1.php (i.e. $city = "Danville"; ), shouldn't I be able to echo $city in part2.php ?

If not, why not? (And what would be the appropriate workaround? I've tried using fopen/fwrite/fread but for some reason, the file isn't being written. :frustrated:)

Link to comment
Share on other sites

Quote

If I define/set a variable in part1.php (i.e. $city = "Danville"; ), shouldn't I be able to echo $city in part2.php ?

Yes.

Quote

 I've tried using fopen/fwrite/fread but for some reason, the file isn't being written. :frustrated:

if you use the require( ) function, there is no need to use the open( ) function.  It is just like copying part2.php into part1.php at that point where you insert the require( ) function.

Link to comment
Share on other sites

28 minutes ago, iwato said:

if you use the require( ) function, there is no need to use the open( ) function.  It is just like copying part2.php into part1.php at that point where you insert the require( ) function.

Thank you for your response, iwato. I was only using fopen/fwrite/fread as an alternative method to try to get the variable text from one part to another (because it's just NOT working for me.

I tried a simple test...

Complete contents of test1.php:

<?php
require "part1.php";
require "part2.php";
require "part3.php";
?>

Complete contents of part1.php:

$city = "Danville";

Complete contents of part2.php:

$citydesc = "pleasant";

Complete contents of part3.php:

echo "$city is a $citydesc place to live.";

When I go to test1.php in my browser, here is the full text of the page:

Quote

$city = "Danville";$citydesc = "pleasant";echo "$city is a $citydesc place to live.";

 

Edited by Donny Bahama
Link to comment
Share on other sites

In the actual (not the test) files, there's a bunch of html code in part1, then I use the php open/close tags, run several commands which result in a bunch of text being assigned to a variable ($promotext) then, in part2, I try to write that string to the page (again, in the middle of a bunch of html code, so I use

<?php echo $promotext; ?>

but the text isn't shown on the final/main page.

Link to comment
Share on other sites

Thank you, Ingolme. Including those blocks made my test file work. But my actual files are more complicated. They're basically a full web page, divided up into chunks so I can require the header file and require the footer file so that, if I decide to make changes to the navmenu or the footer, I only have to change them in one place.

Everything worked beautifully until I decided I wanted to mix in some php. Under those circumstances, what is the right way to do this?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...