Title: Variables available in  after [insert_php]
Last modified: August 31, 2016

---

# Variables available in after [insert_php]

 *  Resolved [Jeff Stevens](https://wordpress.org/support/users/jsrestaurantlink/)
 * (@jsrestaurantlink)
 * [10 years ago](https://wordpress.org/support/topic/variables-available-in-after-insert_php/)
 * When I use [insert_php] and populate a variable, is it available outside of the[
   insert_php] block? Like in my <script>
 * ie.
 *     ```
       <head></head>
       <body>
         [insert_php]
           $testVar = "Hello";
           echo $testVar . "\n";
         [/insert_php]
         <script type="text/javascript">
           document.write($testVar);
         </script>
       </body>
       ```
   
 * only produces
    Hello
 * It should produce
    Hello Hello
 * Correct?
 * Am I missing something?
 * Thanks!
 * [https://wordpress.org/plugins/insert-php/](https://wordpress.org/plugins/insert-php/)

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

 *  [WillBontrager](https://wordpress.org/support/users/willbontrager/)
 * (@willbontrager)
 * [10 years ago](https://wordpress.org/support/topic/variables-available-in-after-insert_php/#post-7238335)
 * Sorry, no variable created in an [insert_php] block is available outside that
   block. More information here:
 * [http://www.willmaster.com/software/WPplugins/insert-php-wordpress-plugin-instructions.php#functionscope](http://www.willmaster.com/software/WPplugins/insert-php-wordpress-plugin-instructions.php#functionscope)
 * To use it in a script tag, it can be done something like this:
 *     ```
       [insert_php]
       $testVar = "Hello";
       echo $testVar . "\n";
       echo '<script type="text/javascript">';
       echo "document.write($testVar);";
       echo '</script>';
        [/insert_php]
       ```
   
 * Will
 *  Thread Starter [Jeff Stevens](https://wordpress.org/support/users/jsrestaurantlink/)
 * (@jsrestaurantlink)
 * [10 years ago](https://wordpress.org/support/topic/variables-available-in-after-insert_php/#post-7238342)
 * Thanks for the reply – that’s kind of what I thought also – and had tried something
   like that before. Here is some more code and I still just get one “Hello” – and
   we should get two – right?
 *     ```
       ...
              $testVar = "Hello";
              echo $testVar . "\n";
              echo '<script type="text/javascript">';
              echo "document.write($testVar);";
              echo '</script>';
              echo "PHP Code out...";
           [/insert_php]
       ```
   
 *  [WillBontrager](https://wordpress.org/support/users/willbontrager/)
 * (@willbontrager)
 * [10 years ago](https://wordpress.org/support/topic/variables-available-in-after-insert_php/#post-7238349)
 * Ah the PHP variable needs to be quoted so JavaScript treats it as a string.
 *     ```
       echo "document.write('$testVar');";
       ```
   
 * Your JavaScript console should have told you about the error.
 * Will
 *  Thread Starter [Jeff Stevens](https://wordpress.org/support/users/jsrestaurantlink/)
 * (@jsrestaurantlink)
 * [10 years ago](https://wordpress.org/support/topic/variables-available-in-after-insert_php/#post-7238351)
 * Ah – you’re correct. Thanks so much.
    I’ll check my console more often also.
 * Thanks

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

The topic ‘Variables available in after [insert_php]’ is closed to new replies.

 * ![](https://ps.w.org/insert-php/assets/icon-256x256.gif?rev=3475630)
 * [Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts](https://wordpress.org/plugins/insert-php/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/insert-php/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/insert-php/)
 * [Active Topics](https://wordpress.org/support/plugin/insert-php/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/insert-php/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/insert-php/reviews/)

## Tags

 * [javascript](https://wordpress.org/support/topic-tag/javascript/)
 * [script](https://wordpress.org/support/topic-tag/script/)
 * [variable](https://wordpress.org/support/topic-tag/variable/)

 * 4 replies
 * 2 participants
 * Last reply from: [Jeff Stevens](https://wordpress.org/support/users/jsrestaurantlink/)
 * Last activity: [10 years ago](https://wordpress.org/support/topic/variables-available-in-after-insert_php/#post-7238351)
 * Status: resolved