How to fix Warning Illegal string offset in PHP

How to fix Warning Illegal string offset in PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to fix Warning Illegal string offset in PHP

Encountering a "Warning: Illegal string offset" error is a classic symptom of a data type mismatch in PHP. The specific error you are seeing—Warning: Illegal string offset 'iso_format_recent_works'—indicates that your code expects $manta_option['iso_format_recent_works'] to be an array, but it is actually a string.

In the provided snippet, you are attempting to use array-style syntax ($manta_option['key']) on a variable that has been serialized into a string. When you run var_dump($manta_option), the output confirms this:

["iso_format_recent_works"]=> string(1) "1"

The key here is that the entire $manta_option variable is not an associative array; it is a single string containing the serialized representation of your options. In older PHP versions or specific configurations involving $_POST, $_GET, or custom session handling, data often arrives serialized by default. However, modern best practices and framework standards generally expect these variables to be unserialized arrays upon entry into your logic.

When you access $manta_option['iso_format_recent_works'] directly on a string, PHP interprets the key 'iso_format_recent_works' as a character offset. Since the string length is only 1 (the character "1"), an offset of 26 characters (the length of the key string itself) is invalid, hence the warning.

The Fix: Unserialize the Data

The most robust solution is to ensure that $manta_option is treated as an array before you attempt to access it via bracket notation. You need to deserialize the string into an associative array. Assuming this data comes from a serialized source (like $_POST or a legacy session variable), you can use unserialize():

if ($data = unserialize($manta_option)) {
    if (isset($data['iso_format_recent_works'])) {
        $theme_img = 'recent_works_thumbnail';
    } else {
        $theme_img = 'recent_works_iso_thumbnail';
    }
} else {
    // Handle the case where unserialization fails or the string is empty
    $theme_img = 'recent_works_iso_thumbnail'; 
}

This approach handles two critical scenarios: first, it converts the serialized string back into a usable array structure. Second, it validates whether the unserialization was successful before attempting to access specific keys. If unserialize() returns false (which happens if the data is corrupted or not valid serialized format), the code defaults safely without throwing a warning.

Alternative: Type Casting

If you determine that $manta_option is consistently passed as an array but simply contains a string value for the specific key, casting the value inside the bracket is different from casting the variable itself. However, your error log suggests the entire variable structure is wrong. If the variable were an array containing a string at that index, var_dump would show:

array(1) { ["iso_format_recent_works"]=> string(1) "1" }

Since your dump shows the key wrapped in quotes as part of the top-level structure ["iso_format_recent_works"]=>, it confirms the top-level variable is a string.

You might be tempted to cast $manta_option to an array using (array) or []. While this will stop the warning, it does not solve the logical problem if the data is truly serialized. Casting a serialized string to an array results in an array with one key: '0', containing the original string. This would lead to incorrect logic later when checking 'iso_format_recent_works' because that key simply wouldn't exist.

Best Practices for Theme/Plugin Options

In WordPress themes and plugins (like the Manta theme referenced in your path), options are often stored as serialized strings in the wp_options table. When retrieving them, you should always ensure they are unserialized before use:

$manta_option = get_option('manta_options'); // This usually returns an array directly
// If you are manually reading from $_POST or similar:
$manta_option = isset($_POST['iso_format_recent_works']) ? 
    unserialize($_POST['iso_format_recent_works']) : [];

If you are developing with Laravel, the Request object automatically handles this unserialization for JSON inputs, but raw POST data might still be serialized depending on your configuration. Always verify the type of incoming data using is_array() before accessing it with bracket syntax to prevent runtime warnings in production environments.

For further resources on handling WordPress theme options and data structures, check out https://laravelcompany.com.

Our network