1

I want to echo a string of page specific text. I don't have a problem with this when the tag is on the page but, I am having issues when it is placd within an include. Example:

index.php

<?php

$StringData1 = "String of page specific text on page echoes.";
include("IncludeHeader.php");

?>
<html>

<head></head>
<body>

<p>Start Echo Test</p>
<p>Echo Test For StringData1:  <?php echo "$StringData1"; ?></p>
<p><?php include("IncludeFile.php");?></p>
<p>Stop Echo Test</p>

</body>
</html>

IncludeHeader.php

// Many Global Variables called from various CSV Files.  To be exact $StrindData2 is actually $StringData2[1] from an array.  The array is of a row of CSV fields
$StringData2 = "String of page specific text in include won't echo.";

IncludeFile.php

Echo Test For Included StringData2:  <?php echo "$StringData2"; ?>

When index.php is run the output is:

<html>

<head></head>
<body>

<p>Start Echo Test</p>
<p>Echo Test For StringData1:  String of page specific text on page echoes.</p>
<p>Echo Test For Included StringData2:  </p>
<p>Stop Echo Test</p>

</body>
</html>

From this example, how can I get $StringData2 to echo from within the include?

Thanks for any help.

6
  • 1
    As an aside: you do not need to quote variables! echo $StringData2 works fine. Commented Mar 20, 2012 at 5:42
  • See stackoverflow.com/questions/3558336/… Commented Mar 20, 2012 at 5:42
  • Also, activate error reporting and let us know if you see any errors: error_reporting(E_ALL); ini_set('display_errors', true); Commented Mar 20, 2012 at 5:43
  • Just a raw copy and paste of your code, into its respective files, and everything works fine for me. I get both outputs. Commented Mar 20, 2012 at 5:43
  • Sorry for the confusion. In an attempt to abbreviate I messed up the question. Editted to be more accurate now Commented Mar 20, 2012 at 6:16

2 Answers 2

2

The doc states:

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.

Also that:

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. So, it will follow the variable scope of that function. An exception to this rule are magic constants which are evaluated by the parser before the include occurs.

Was your example abbreviated or is that your exact code? Your original example as shown should work fine.

Your updated example should still work.

Scratch my last edit. I think you really need to come up with an abbreviated example that reproduces the same behavior.

Sign up to request clarification or add additional context in comments.

1 Comment

The example was abbreviated. Actually the $StringData2 is $StringData[1] pulled from an array. The array is built from a row csv fields (long story, but can't use SQL). It is contained within another include to be used (and editted) globally.
0

Try this code:

IncludeFile.php

  Echo "Test for Included StringData2:  $StringData2"; 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.