Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Edit:</strong> Okay, I've re-read your question and I think I get what you're talking about now:<br> you want something like this to work:</p> <pre><code>// myInclude.php $x = "abc"; // ----------------------- // myRegularFile.php function doInclude() { include 'myInclude.php'; } $x = "A default value"; doInclude(); echo $x; // should be "abc", but actually prints "A default value" </code></pre> <p>If you are only changing a couple of variables, and you know ahead of time which variables are going to be defined in the include, declare them as <code>global</code> in the <code>doInclude()</code> function.</p> <p>Alternatively, if each of your includes could define any number of variables, you could put them all into one array:</p> <pre><code>// myInclude.php $includedVars['x'] = "abc"; $includedVars['y'] = "def"; // ------------------ // myRegularFile.php function doInclude() { global $includedVars; include 'myInclude.php'; // perhaps filter out any "unexpected" variables here if you want } doInclude(); extract($includedVars); echo $x; // "abc" echo $y; // "def" </code></pre> <hr> <p><em>original answer:</em></p> <p>this sort of thing is known as "closures" and are being introduced in PHP 5.3</p> <p><a href="http://steike.com/code/php-closures/" rel="nofollow noreferrer">http://steike.com/code/php-closures/</a></p> <blockquote> <p>Would it be better to use extract($_GLOBALS); inside my function call instead?</p> </blockquote> <p>dear lord, no. if you want to access a global variable from inside a function, just use the <code>global</code> keyword. eg:</p> <pre><code>$x = "foo"; function wrong() { echo $x; } function right() { global $x; echo $x; } wrong(); // undefined variable $x right(); // "foo" </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload