[PHP] PHP 8.3 Processes Diferently Than 7.4

I have a script with an include file that contains a function called ‘get_show_list’ that has a referenced array which is causing PHP to abort the script. The following used to work in 7.4:

*** Code Snippet from the Include File ***

$base_dir = “/home/tgvpadmin/data/shows”;
$content_dir = “/home/tgvpadmin/content/programs”;
$archive_dir = “/home/tgvpadmin/content/archives”;

// Return list of sorted shows, taking definite articles into account
function get_show_list () {
global $base_dir;

$base_dir_handle = @opendir ($base_dir);
if (!$base_dir_handle)
die (“Couldn’t open directory $base_dir”);

while (($filename = readdir ($base_dir_handle)) !== false) {
if (($filename != “.”) and ($filename != “…”) and (is_dir (“$base_dir/$filename”))
and ($showname_handle = @fopen (“$base_dir/$filename/showname”, “r”))) {
$shows[$filename] = trim (fgets ($showname_handle));
fclose ($showname_handle);
} // end if a dir with a showname file
} // end traversal of directory

closedir ($base_dir_handle);

// Copy to another array for sorting purposes
$sorted_shows = $shows;

// Make all show names lowercase for sorting and searching
reset ($sorted_shows);
while (list ($key, $value) = each ($sorted_shows))
$sorted_shows[$key] = strtolower ($sorted_shows[$key]);
// Strip off “the” and “la” etc.
reset ($sorted_shows);
while (list ($key, $value) = each ($sorted_shows)) {
if (strpos ($value, " ")) {
$space_pos = strpos ($value, " ");
$first_word = substr ($value, 0, $space_pos);
switch ($first_word) {
case “the”:
case “a”:
case “an”:
case “la”:
case “el”:
$sorted_shows[$key] = substr ($value, $space_pos + 1);
break;
} // end switch on first word
} // end if more than one word in a show title
} // end while list of shows

// Use asort to maintain keys
asort ($sorted_shows);

// Generate sorted list with original show names
reset ($sorted_shows);
while (list ($key, $value) = each ($sorted_shows)) {
$show_list[$key] = $shows[$key];
} // end while list of shows

return $show_list;
} // end get_show_list ()

*** End Code ***

There’s a lot more I omitted for brevity and that I don’t think it’s involved, but here’s the error I get when I run the calling script:

PHP Fatal error: Uncaught Error: Call to undefined function each() in /home/tgvpadmin/domains/theglobalvoice.info/public_html/gallery.inc:35
Stack trace:
#0 /home/tgvpadmin/domains/theglobalvoice.info/public_html/mail_gallery_updates.php(32): get_show_list()
#1 {main}
thrown in /home/tgvpadmin/domains/theglobalvoice.info/public_html/gallery.inc on line 35

“each” is not a function. It’s a list. What changed that affected the way this worked between 7.4 and 8.3? I looked through the manual sections on arrays and lists and didn’t find anything that looked like it was the problem.

TIA

On Oct 4, 2024, at 3:44 PM, Steve Matzura <sm@noisynotes.com> wrote:
"each" is not a function. It's a list. What changed that affected the way this worked between 7.4 and 8.3? I looked through the manual sections on arrays and lists and didn't find anything that looked like it was the problem.

each and the other manual array cursor functions were removed in 8.0:

You'll need to change to using a different way to iterate, i.e. the foreach keyword.

while (list ($key, $value) = each ($sorted_shows))

···

On 10/4/2024 3:23 PM, Calvin Buckley wrote:

On Oct 4, 2024, at 3:44 PM, Steve Matzura [<sm@noisynotes.com>](mailto:sm@noisynotes.com) wrote:
"each" is not a function. It's a list. What changed that affected the way this worked between 7.4 and 8.3? I looked through the manual sections on arrays and lists and didn't find anything that looked like it was the problem.

each and the other manual array cursor functions were removed in 8.0:

[https://www.php.net/manual/en/function.each.php](https://www.php.net/manual/en/function.each.php)

You'll need to change to using a different way to iterate, i.e. the foreach keyword.

Looks simple enough, unless I’m misunderstanding the change. so, should:

be:

foreach (list as ($key, $value) = each ($sorted_shows))

like that?

On Oct 4, 2024, at 5:49 PM, Steve Matzura <sm@noisynotes.com> wrote:

Looks simple enough, unless I'm misunderstanding the change. so, should:

while (list ($key, $value) = each ($sorted_shows))

be:

foreach (list as ($key, $value) = each ($sorted_shows))

like that?

foreach ($array as $key => value)

or for non-associative,

foreach ($array as $element)

On 10/4/2024 4:50 PM, Calvin Buckley wrote:

On Oct 4, 2024, at 5:49 PM, Steve Matzura<sm@noisynotes.com> wrote:

Looks simple enough, unless I'm misunderstanding the change. so, should:

while (list ($key, $value) = each ($sorted_shows))

be:

foreach (list as ($key, $value) = each ($sorted_shows))

like that?

foreach ($array as $key => value)

or for non-associative,

foreach ($array as $element)

I don't understand non-associative.

foreach ($list as $key => value)

I am not getting the difference between 'value' and '$value'. I see 'value' as a keyword and '$value' as a variable containing some value. That sounds wrong.