r/PHPhelp • • Jun 28 '22

Solved "foreach" in PHP 8

Hi all,

today i wanted to change the PHP version, because joomla told me that version 7.4 is no longer supported. So i changed to PHP 8. Joomla is still running fine.

Unfortunately my hoster does not support different versions for different subdomains. So, another small script that i need, does not run anymore with PHP 8.
Error 500.
I walked through the code line for line and found that the script stops at this lines:
$zusatzfelder = "";

foreach($preset[zusatzfeld] as $key => $value){

$zusatzfelder .= "<input type='text' value='" .$value ."' name='zusatzfeld[" .$key ."]'>";

}

Any ideas why this will not work anymore?

Thanks Tom

7 Upvotes

5 comments sorted by

0

u/[deleted] Jun 28 '22

[deleted]

3

u/crispilly Jun 28 '22

F**K i am blind :-D
But why does it run in PHP 7.4 without quotes? I did not change the script.

Thank you so much, YMMD!

14

u/[deleted] Jun 28 '22 edited Jun 28 '22

But why does it run in PHP 7.4 without quotes? I did not change the script.

Because PHP has a long history of letting you get away with things that should never work but produce the correct result on accident.

Attempting to access unqualified constants which are undefined. Previously, unqualified constant accesses resulted in a warning and were interpreted as strings.

https://www.php.net/manual/en/migration80.incompatible.php

7

u/RandyHoward Jun 28 '22

So many questions of, "Why does that even work?" can be answered with, "Because PHP is silly sometimes" lol

0

u/pfsalter Jun 29 '22

Yeah a lot of it is "it worked in PHP4 and people loooove bc"

6

u/kAlvaro Jun 28 '22

PHP/8 promoted to fatal error what used to be a warning and, before that, a notice:

https://3v4l.org/TtGj5

That means it worked by pure chance and you've probably always had error reporting disabled.

I always recommend https://phpdelusions.net/articles/error_reporting