Advertisement:

Author Topic: Fix Illegal String Offset + Cannot Assign Empty String To String Offset PHP 7  (Read 2088 times)

Francesko

  • Newbie
  • *
  • Posts: 9
Hi, after doing some research i found a solution to fix the annoying problem with PHP 7.0 and above:
Warning: Illegal string offset [xxx] on line xxx & Cannot Assign Empty String To String Offset.
Answer: Problem is that our variable $array is not set or initialized as an array() in case of $something part being undefined in the first place!
This was apparently fine in PHP 7.0 and below, but not anymore. Now, PHP will NOT assume or automatically fix our problem any longer, and we need to explicitly set it as array() a priory.

Here is a simple "bad" code:
Code: [Select]
$array = $something; // $something can be anything, another function call, for example, that might return empty/null, array or string
if(!isset($array['id']) ) {
    $array['id'] = '';
}
Fixed Code:
Code: [Select]
$array = (array) $something; // simply cast $variable as array()
if(!isset($array['id']) ) {
    $array['id'] = '';
}

Some fixes:
On Bender theme V3.1.4, V3.2.0 sidebar-search.php Line 21:
Change from:
Code: [Select]
$category = __get("category");To:
Code: [Select]
$category = (array) __get("category");
On Zara Theme v1.2.x-v1.28, item-post.php Line 274:
Change from:
Code: [Select]
$user_info = '';To:
Code: [Select]
$user_info = (array) '';


Aficionado

  • Guest
This is not a problem with PHP 7.0 but with PHP 7.1 and all bender-based themes.
« Last Edit: September 07, 2017, 02:25:29 pm by Aficionado »