David-London Posted December 11, 2021 Share Posted December 11, 2021 Hi all, I have recently upgraded to php 8.0 and some of the scripts in a wordpress plugin now don't work. One of them is because of count needing to be an array. Should I just change this code Quote if ( count($this::$sources) > 0 ) to this ? Quote if ( count(array($this::$sources)) > 0 ) If not, what would be the correct sintax? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/314298-php-80-how-to-correct-if-countthissources-0/ Share on other sites More sharing options...
Barand Posted December 11, 2021 Share Posted December 11, 2021 If $sources is supposed to be an array then ensure it is correctly initialised as such. If it is currently NULL, you will get an error. Quote Link to comment https://forums.phpfreaks.com/topic/314298-php-80-how-to-correct-if-countthissources-0/#findComment-1592620 Share on other sites More sharing options...
David-London Posted December 11, 2021 Author Share Posted December 11, 2021 (edited) OK - thanks for reply. This is a plugin which I didn't write so I am just trying to update it. Just above the code this comment is written: // If there are sources, show a box of recently added Maybe I could replace that line of code with: Quote if (!empty($this::$sources) : Here is more of the code ... // If there are sources, show a box of recently added if ( count($this::$sources) > 0 ) : ?> <div id="tube-vc-recent-channels-module"> <h3> <?php _e( 'Recently added Channels & Playlists', 'yt-revolution' ); ?> </h3> <ul style="list-style: none; padding-left:.25em;"> <?php $count = 0; foreach ( $this::$sources as $source ): $count++; if ( $count > 5 ): continue; endif; Edited December 11, 2021 by David-London Quote Link to comment https://forums.phpfreaks.com/topic/314298-php-80-how-to-correct-if-countthissources-0/#findComment-1592624 Share on other sites More sharing options...
Barand Posted December 11, 2021 Share Posted December 11, 2021 (edited) Before you guess the correction code, see what it contains when you get the error var_dump($this::$sources); and find where it is being initialized (probably in it's class' constructor Edited December 11, 2021 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/314298-php-80-how-to-correct-if-countthissources-0/#findComment-1592630 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.