Title: Php in widget issue!
Last modified: August 22, 2016

---

# Php in widget issue!

 *  [AndySwede](https://wordpress.org/support/users/andyswede/)
 * (@andyswede)
 * [11 years, 9 months ago](https://wordpress.org/support/topic/php-in-widget-issue-1/)
 * Hello!
 * Code in a template runs in the global context. Code in the widget will run in
   a function context. Make sure that you declare any global variables as global
   before attempting to use them”
 * What does that mean?
    How do i configure the code below to work in my widget 
   that can handle php?
 *     ```
       <?php $lunch = get_group('lunch');  foreach($lunch as $lunch){
       echo '<span class="lunch-dag">' . $lunch['lunch_dag'][1]. '</span><br />';
       echo '<span class="lunch-matratt">' . $lunch['lunch_matratt'][1]."<br />";
       echo $lunch['lunch_matratt'][2]."<br />";
       echo $lunch['lunch_matratt'][3]."<br />";
       echo $lunch['lunch_matratt'][4]. '</span><br /><br />';}?>
       ```
   
 * The code works perfect in a template!
 * Best regards
    Andy

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

 *  [Matt](https://wordpress.org/support/users/mzak/)
 * (@mzak)
 * [11 years, 9 months ago](https://wordpress.org/support/topic/php-in-widget-issue-1/#post-5282503)
 * I believe you’ll have to declare $lunch as a global variable, i.e.
 * `global $lunch = get_group('lunch');`
 *  Thread Starter [AndySwede](https://wordpress.org/support/users/andyswede/)
 * (@andyswede)
 * [11 years, 9 months ago](https://wordpress.org/support/topic/php-in-widget-issue-1/#post-5282509)
 * I have tried that but get error message.
    Hmmm… I have also been told (in a very
   rude way) that this `foreach($lunch as $lunch)` make no sense at all, and it 
   may be so but it still works in my template and does the job.
 * As surely everybody understands im a total novice when it comes to this. I have
   made some “custom fields” in backend and want to show them on my page, frontend,
   sorry for my bad english, and it works without no problems at all when i use 
   that code in a template.
 * How would u write it in proper way?
 *  [Matt](https://wordpress.org/support/users/mzak/)
 * (@mzak)
 * [11 years, 9 months ago](https://wordpress.org/support/topic/php-in-widget-issue-1/#post-5282535)
 * Ah, yes, I should have noticed that before. `foreach($lunch as $lunch)` is not
   correct. [This might help clarify how foreach loops should be structured](http://php.net/manual/en/control-structures.foreach.php)
 *  [Dion](https://wordpress.org/support/users/diondesigns/)
 * (@diondesigns)
 * [11 years, 9 months ago](https://wordpress.org/support/topic/php-in-widget-issue-1/#post-5282543)
 * You can’t declare the same variable in `foreach(){}`. If that worked for you 
   previously, you were very lucky. Try something like this:
 *     ```
       <?php
       $lunch_ary = get_group('lunch');
       foreach ($lunch_ary as $lunch) {
       	echo '<span class="lunch-dag">' . $lunch['lunch_dag'][1]. '</span><br />';
       	echo '<span class="lunch-matratt">' . $lunch['lunch_matratt'][1]."<br />";
       	echo $lunch['lunch_matratt'][2]."<br />";
       	echo $lunch['lunch_matratt'][3]."<br />";
       	echo $lunch['lunch_matratt'][4]. '</span><br /><br />';
       }
       ?>
       ```
   
 * If the above doesn’t work, then we must start digging. What is this `get_group()`
   function? Is it globally accessible?
 * EDIT: is this code supposed to loop? Your code makes me wonder if the echo statements
   are meant to only be executed once…
 *  [RossMitchell](https://wordpress.org/support/users/rossmitchell/)
 * (@rossmitchell)
 * [11 years, 9 months ago](https://wordpress.org/support/topic/php-in-widget-issue-1/#post-5282566)
 * > EDIT: is this code supposed to loop? Your code makes me wonder if the echo 
   > statements are meant to only be executed once…
 * Maybe that is what the
 * `foreach($lunch as $lunch)`
 * is about, will only process the first item in the array, stepping to the 2nd 
   item probably fails.
 * I think that:
 *     ```
       $lunch_ary = get_group('lunch');
       print_r($lunch_ary);
       ```
   
 * is called for.

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

The topic ‘Php in widget issue!’ is closed to new replies.

## Tags

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

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 5 replies
 * 4 participants
 * Last reply from: [RossMitchell](https://wordpress.org/support/users/rossmitchell/)
 * Last activity: [11 years, 9 months ago](https://wordpress.org/support/topic/php-in-widget-issue-1/#post-5282566)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
