Undefined array key 1 in PHP 8
-
I get this warning after upgrading to php 8.0.3
[17-Apr-2021 09:38:57 UTC] PHP Warning: Undefined array key 1 in /var/www/mywebsite/wp-content/plugins/autoptimize/classes/autoptimizeStyles.php on line 275
-
Hmm … The code runs a regular expression if
media=is found and the regex _should_ return the attribute value in$medias[1], so it looks like somehow that regex is not (always) returning an attribute value.Can you check the HTML source of a couple of pages on your site and tell me what different media attribute-values are there for your CSS?
itemprop, viewbox (for svg),
I added this line up to the original line:
error_log(print_r($medias[1],true));
$medias = explode( ‘,’, $medias[1] );And i’ll check the error log.
ok here is an update
Normally the entry in the error log is like this:
[17-Apr-2021 12:31:52 UTC] Array
(
[0] => media=’all’
[1] => all
)`BUT sometimes it becomes this:
17-Apr-2021 12:32:22 UTC] Array
(
)[17-Apr-2021 12:32:22 UTC] PHP Warning: Undefined array key 1 in /var/www/mywebsite/wp-content/plugins/autoptimize/classes/autoptimizeStyles.php on line 276`
-
This reply was modified 5 years, 1 month ago by
Knightycool.
-
This reply was modified 5 years, 1 month ago by
Knightycool.
Is there a way to learn in which post this happens?
Ok i get it now. I added the id number and could pinpoint where it happened…
In the custom css of a single page (where it happens), there is a comment like this:
/*! CSS Used from: https://www.mysite.net/wp-content/myapp.css ; media=all */
So it matches media=all
I removed it and try to see if the warning happens again. Perhaps you may find a way to capture media=all only if its between certain html tags.
that’s unlikely to be the culprit; the code I referenced earlier is only applied on
<styleand<link rel=stylesheettags, see https://github.com/futtta/autoptimize/blob/2.8.3/classes/autoptimizeStyles.php#L266 … :-/could you share the URL of the page/post with the id so I can have a look at the HTML source?
It was the reason because the said comment was inside <style> </style>
<style>
.myclass {
font-size: 10px;
background-color: red;
}
.myclass2 {
height: 100px;
width: 100px;
}
/*! CSS Used from: https://www.mysite.net/wp-content/myapp.css ; media=all */
.myclass3 {
border-color: black;
}
</style>OK, I’ll test with that code locally, more soon 🙂
If you want, i can add back the comment and email you the url but i dont want to post the url here.
OK, I have a fix in AO beta, could you download from https://github.com/futtta/autoptimize/archive/refs/heads/beta.zip, install that instead of 2.8.3 and test if that fixes the notice?
(the beta has some other new features already; per page/post settings for AO + new “defer inline JS” option)
umm ok, the notice has already disappeared but i will re enter the comment and then install the beta and try to see what happens.
Ok. This is what i did:
1) Added back the comment line.
2) Check to see the warning happens. And it happened when i visit the post.
3) I installed beta
4) Visited the post and there is no warning. I think it is fixed.
Thanks a lot. What have you changed exactly? May you show me line? And when a new update comes, i can just update this via wordpress right?
Great!
this is the change: https://github.com/futtta/autoptimize/commit/37b13d4e19269bb2f50df123257de51afa37244f
And indeed when 2.9 is released this will be in it. If you keep the beta active you will receive updates of that as well by the way, so you can keep track of both 😉
Thanks a lot. You are great. Loves from Turkey..
-
This reply was modified 5 years, 1 month ago by
The topic ‘Undefined array key 1 in PHP 8’ is closed to new replies.