The code bellow worked for quite some time, but after changing the php version to 8.0,
I get the error 'undefined variable on ‘$os’ and many other parts of my code.
What can this be?
Hi Moonwalk, thanks for your input. Your solution does work.
I use this logic in many places. I am wondering why I get all these errors now, and not before.
I also set the php version back to an earlier one, but it doesnt do the trick.
Any ideas there?
That is just an abstract example, because I use it many times. Now its raining errors in my site
When you type
If($os) {}
It will now give an error if $os doesn’t exist.
Th same with:
<?= $os ?>
It didnt throw an error in php 7.4 / Kirby 3.7
I didnt see it between the list changes of Kirby 3.8?
When I update to php 8 and kirby 3.8 It throws the error. Any ideas?
It works as an equivalent of isset($object) I just learned.
PHP 8.0 is a lot stricter than PHP 7.4. You should never have any undefined variables. It says in the changelog that you need to change your template code to make it compatible with PHP 8+.
just to be clear, trying to access an undefined variable was also “wrong” in PHP 7, it generated an error message at the “NOTICE” level. In PHP 8 it now generates a “WARNING”; also the default error reporting level in PHP 8 has been set to “E_ALL”, while, before, by default it wouldn’t show "NOTICE"s.
Anyway, I guess you can choose between always setting a variable before using it (The Right Way™), or just provide a fallback value via the null coalescing operator when accessing the variable: