Jump to content

[SOLVED] Can't get foreach to work.


MattR

Recommended Posts

This is the code I have:

 

function EpisodeList()
		{
			$episodesexploded = explode('||', $episodes);

			foreach ($episodesexploded as $episode)
			{
				echo $episode . "<br />";
			}
		}

 

$episodes is an array from a query. The error I get is:

 

Notice: Undefined variable: episodes in /file.php on line 85

 

Can't figure out why ???

Link to comment
https://forums.phpfreaks.com/topic/163780-solved-cant-get-foreach-to-work/
Share on other sites

You need to pass the array to your function;

 

<?php
$episodes = array("once", "twice", "thrice");
function EpisodeList($episodes)
         {
            $episodesexploded = explode('||', $episodes);
            
            foreach ($episodesexploded as $episode)
            {
               echo $episode . "<br />";
            }
         }

EpisodeList($episodes);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.