Title: PHP allow splitting code
Last modified: August 31, 2016

---

# PHP allow splitting code

 *  Resolved [lutechi](https://wordpress.org/support/users/lutechi/)
 * (@lutechi)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/php-allow-splitting-code/)
 * Hello,
 * Thanks so much for this plugin, I have been using it for awhile now with html
   and JS and recently I tried to do some customization with PHP for some projects.
 * I was receiving the following error when I tried to echo a variable for an array:
 * Fatal error: Call to a member function getContent() on a non-object in /wp-content/
   plugins/post-snippets/src/PostSnippets/Shortcode.php(97) : eval()’d code on line
   1
 * I already have the variable array on the template’s head, I just wanted to echo
   the function as a snippet and it was producing the error. However if I included
   the variable and echo on the same snippet the php will run fine.
 * Thanks in advance.
 * [https://wordpress.org/plugins/post-snippets/](https://wordpress.org/plugins/post-snippets/)

Viewing 1 replies (of 1 total)

 *  [Johan Steen](https://wordpress.org/support/users/artstorm/)
 * (@artstorm)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/php-allow-splitting-code/#post-6986805)
 * Hi,
 * That’s the nature of PHP, the PHP snippet gets evaluated in it’s own scope, and
   does not have access to variables outside the scope.
 * As a workaround, what you can do is this, make the variables you need to access
   from a snippet to a global variable. Ie, for instance if you want to assign a
   value to a variable in your template, make it a global variable, like this:
 *     ```
       $GLOBALS['foo'] = 'bar';
       ```
   
 * Then to access the variable in a PHP snippet, you have two options, either
 *     ```
       global $foo;
       echo $foo;
       ```
   
 * or
 *     ```
       echo $GLOBALS['foo'];
       ```
   
 * Hope this helps.
 * Cheers,
    Johan

Viewing 1 replies (of 1 total)

The topic ‘PHP allow splitting code’ is closed to new replies.

 * ![](https://ps.w.org/post-snippets/assets/icon-128x128.gif?rev=2545587)
 * [Post Snippets - Custom WordPress Code Snippets Customizer](https://wordpress.org/plugins/post-snippets/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/post-snippets/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/post-snippets/)
 * [Active Topics](https://wordpress.org/support/plugin/post-snippets/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/post-snippets/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/post-snippets/reviews/)

 * 1 reply
 * 2 participants
 * Last reply from: [Johan Steen](https://wordpress.org/support/users/artstorm/)
 * Last activity: [10 years, 4 months ago](https://wordpress.org/support/topic/php-allow-splitting-code/#post-6986805)
 * Status: resolved