Jump to content
Donny Bahama

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

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:)

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Included files must have <?php ?> blocks around the code.

  • Like 1
  • Thanks 1

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...