Empty [ ] causing fatal error in PHP 7.1 +

Hello,

I’m working on a WordPress theme that is currently running under PHP v. 7.0. When run under PHP7.1 +, certain pages/functions result in a fatal error and the resulting pages are blank. I have narrowed the issue down to 3 search page templates that have an array declaration that has a blank [ ] which I know will not automatically convert to an array under PHP 7.1+. Check the following code snippet under line 9.

> <?php
> $_ids = array();
> function getIds( $query ) {
>     global $wpdb;
> 	$tmp = "";
>     $searchresults = $wpdb->get_results($query, ARRAY_A);
>     if ( !empty ($searchresults) ) {
>         foreach( $searchresults as $_post ) {
>             $tmp[] = $_post['ID'];
>         }
>     }
>     return $tmp;
> }
> 
> global $wpdb;
> 
> $query = $wpdb->prepare("SELECT p.* FROM $wpdb->posts p WHERE p.post_type = %s AND p.post_status = %s", 'listing', 'publish');
> 		$all = getIds( $query );
> 		$_ids = ( !empty($all) ? ( !empty($_ids) ? array_intersect( $_ids, $all) : $all ) : "" );
> 
> if (get_option('wp_filtersold') == 'Yes') {
> 	$query = $wpdb->prepare("SELECT p.* FROM $wpdb->posts p, $wpdb->postmeta p1
> 			WHERE p.ID = p1.post_id AND p1.meta_key= %s AND p1.meta_value = %s", 'sold_value', 'No');	
> 	$sbr = getIds( $query );
> 	$_ids = ( !empty($sbr) ? ( !empty($_ids) ? array_intersect( $_ids, $sbr) : "" ) : "" );
> }
> 	
> ?>

Is there an easy fix to this? The developer has not been heard from in over a year and there is no direct support or upgrades to the theme likely.

Thanks,
Peter

This would be far easier if you said what the error was, rather than speculating on what it might be.

As follows - error under PHP 7.2

Fatal error : Uncaught Error: [] operator not supported for strings in /usr/home/informatik/public_html/themeforest/OpenHouse/wp-content/themes/Openhouse-and-Openroad/includes/search_query_all.php:9 Stack trace: #0 /usr/home/informatik/public_html/themeforest/OpenHouse/wp-content/themes/Openhouse-and-Openroad/includes/search_query_all.php(18): getIds(‘SELECT p.* FROM…’) #1 /usr/home/informatik/public_html/themeforest/OpenHouse/wp-content/themes/Openhouse-and-Openroad/pagetemplate-alllistings.php(52): include(’/usr/home/infor…’) #2 /usr/home/informatik/public_html/themeforest/OpenHouse/wp-includes/template-loader.php(74): include(’/usr/home/infor…’) #3 /usr/home/informatik/public_html/themeforest/OpenHouse/wp-blog-header.php(19): require_once(’/usr/home/infor…’) #4 /usr/home/informatik/public_html/themeforest/OpenHouse/index.php(17): require(’/usr/home/infor…’) #5 {main} thrown in /usr/home/informatik/public_html/themeforest/OpenHouse/wp-content/themes/Openhouse-and-Openroad/includes/search_query_all.php on line 9

A blank [ ] is an array, but you can’t say it is a string, then use it as an array. You are declaring it as a string; just declare it as an array to begin with.

Thank you for your comments. Unfortunately, my PHP coding skills are not what they could be and the nuances of why PHP code would run under 7.0 but not under 7.2 are above my pay grade.

Is there an easy way to fix this?

Don’t make $tmp an empty string, but an empty array.

As $searchresults seems to be already an array that you only want a column from, use array_column.

Sponsor our Newsletter | Privacy Policy | Terms of Service