Title: PHP variable persistence/scope between template files
Last modified: August 19, 2016

---

# PHP variable persistence/scope between template files

 *  Resolved [mothmenace](https://wordpress.org/support/users/mothmenace/)
 * (@mothmenace)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/php-variable-persistencescope-between-template-files/)
 * Since upgrading to 2.8, I am experiencing a situation where variables defined
   in the main scope of the PHP template file/functions.php or plugin are not accessible
   in my other page template files. I have had to store them in some global area,
   like the $_ENV array, to access them. Is this a new feature of 2.8, or is it 
   something to do with my PHP configuration? (Although I can’t remember making 
   any changes to my INI).
 * Has anyone else experienced this?
    Many thanks

Viewing 9 replies - 1 through 9 (of 9 total)

 *  Thread Starter [mothmenace](https://wordpress.org/support/users/mothmenace/)
 * (@mothmenace)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/php-variable-persistencescope-between-template-files/#post-1136927)
 * I believe this is because templates are now wrapped in a function call to limit
   the scope of any vars defined – correct me if I’m wrong, can’t find any docs.
 *  [converting2wp](https://wordpress.org/support/users/converting2wp/)
 * (@converting2wp)
 * [16 years ago](https://wordpress.org/support/topic/php-variable-persistencescope-between-template-files/#post-1136965)
 * Does anyone have a comment here?
 * Is it possible to use a plugin to have pages “share” data? Or is there a better
   solution?
 * Any pointers to docs? or an example?
 *  Thread Starter [mothmenace](https://wordpress.org/support/users/mothmenace/)
 * (@mothmenace)
 * [16 years ago](https://wordpress.org/support/topic/php-variable-persistencescope-between-template-files/#post-1136966)
 * Hello convert, if you’ve got vars in functions.php or a plugin that you want 
   to access in a template, you need to add a global var declaration for that var
   to the template, and should work.
 * Figured it out myself, can’t see this in any WP docs?! *forum tumbleweed*
 *  [converting2wp](https://wordpress.org/support/users/converting2wp/)
 * (@converting2wp)
 * [16 years ago](https://wordpress.org/support/topic/php-variable-persistencescope-between-template-files/#post-1136967)
 * Thanks. I’ve tried that but I must be doing something wrong.
 * Let me give it another whack…
 *  [converting2wp](https://wordpress.org/support/users/converting2wp/)
 * (@converting2wp)
 * [16 years ago](https://wordpress.org/support/topic/php-variable-persistencescope-between-template-files/#post-1136968)
 * Okay, I’m still missing something basic. I’ll admit it’s probably PHP rather 
   than WordPress, but here’s my situation.
 * functions.php:
 *     ```
       global $testvar;
       add_action( 'hook_defined_in_plugin1', 'my_process_plugin1_data');
       ```
   
 * I’ve written “plugin2.php” and installed/activated it. Its code contains
 *     ```
       global $testvar;
       function my_process_plugin1_data( --args as from plugin1-- )
       {
       	global $testvar;
       	$testvar="this is in the data processing function";
   
       }
       ```
   
 * Finally in a PHP template file for a page displayed after my_process_plugin1_data
   executes, I have
 *     ```
       global $testvar;
       echo "<br />Confirm var value===".$testvar."===</br>";
       ```
   
 * but $testvar shows up as an empty string.
 * I’ve confirmed the hook/action works — in the template file I can retrieve a 
   cookie that’s set by the my_process_plugin1_data function. But I just can’t seem
   to get the function and the page communicating through a variable.
 * I’ve searched the docs and the book “WordPress Plugin Development” — but nothing
   jumps out as the step-by-step way to set up this communication.
 *  [converting2wp](https://wordpress.org/support/users/converting2wp/)
 * (@converting2wp)
 * [16 years ago](https://wordpress.org/support/topic/php-variable-persistencescope-between-template-files/#post-1136969)
 * And commenting out either one of the global’s in the plugin2.php file makes no
   difference.
 *  Thread Starter [mothmenace](https://wordpress.org/support/users/mothmenace/)
 * (@mothmenace)
 * [16 years ago](https://wordpress.org/support/topic/php-variable-persistencescope-between-template-files/#post-1136970)
 * I think you will need to actually set/declare $testvar outside of the function
   in plugin2.php –
    `$testvar = 'test';`
 * Eg my usage is like this: in functions.php I have:
 *     ```
       // global value, incremented for multiple search form ids
       $SEARCH_ID=1;
       ```
   
 * Then in searchform.php:
 *     ```
       <?php global $SEARCH_ID; ?>
       <form class="search" id="search-<?=$SEARCH_ID++?>">
       ```
   
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years ago](https://wordpress.org/support/topic/php-variable-persistencescope-between-template-files/#post-1136971)
 * I’m not sure there’s much that can be said to explain variable scope, it’s best
   left to the PHP docs.
    [http://php.net/manual/en/language.variables.scope.php](http://php.net/manual/en/language.variables.scope.php)
   [http://php.net/manual/en/language.variables.external.php](http://php.net/manual/en/language.variables.external.php)
 *  [converting2wp](https://wordpress.org/support/users/converting2wp/)
 * (@converting2wp)
 * [16 years ago](https://wordpress.org/support/topic/php-variable-persistencescope-between-template-files/#post-1136972)
 * Many thanks to you both. I haven’t tested yet, but I expect you’ve given me exactly
   what I need. While the PHP.net references may have seemed to be obvious places
   for me to check, as only an occasional programmer whose brain is still crufty
   with rules from other languages (and with, obviously, an incomplete understanding
   of the differences between setting and declaring variables) your pointing them
   out was very helpful.

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘PHP variable persistence/scope between template files’ is closed to new
replies.

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)
 * [variables](https://wordpress.org/support/topic-tag/variables/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 9 replies
 * 3 participants
 * Last reply from: [converting2wp](https://wordpress.org/support/users/converting2wp/)
 * Last activity: [16 years ago](https://wordpress.org/support/topic/php-variable-persistencescope-between-template-files/#post-1136972)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
