How do I correct this Illegal String Offset?

How do I correct this Illegal String Offset?

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

Stefan Bogdanescu

Founder & Senior Architect

How do I correct this Illegal String Offset?

The error Warning: Illegal string offset 'type' is one of the most common pitfalls for developers transitioning from dynamic languages like JavaScript or Python to PHP. It signals a fundamental mismatch in how data structures are being accessed. In your specific case, the warning occurs because the code attempts to access $inputs['type'] as if $inputs were an associative array, but it is actually a string.

PHP arrays can be indexed numerically (like lists), associatively (with keys like 'name' or 'type'), or both. However, when you define a variable without specifying its type and assign a string to it, PHP treats that variable strictly as a string. Attempting to use bracket notation ($array['key']) on a string triggers this warning because strings do not have named offsets; they only have numeric ones.

In the snippet you provided, the function get_attachment_struct accepts $inputs. While the code inside the if block assumes $inputs is an associative array with keys like 'type', 'name', and 'classes', there is no validation ensuring that $inputs actually contains these keys before they are accessed. If $inputs is passed as a simple string, or if it is an array but missing the 'type' key without proper checks, PHP will throw this warning.

To resolve this, you must ensure that the code only attempts to access associative keys on arrays. The most robust way to handle this in modern PHP is by using array_key_exists() before accessing the value, or by utilizing the null coalescing operator (??) introduced in PHP 7.0. If you are targeting older versions of PHP, explicit checks are mandatory.

Here is a corrected version of your function that handles missing keys gracefully and prevents the illegal offset warning:

function get_attachment_struct( $inputs ) {
    $attach = array();

    // Check if 'type' exists in $inputs before accessing it.
    // If $inputs is a string, this check ensures we don't trigger an illegal offset error.
    if ( isset( $inputs['type'] ) && $inputs['type'] == 'attach' ) { 
        // Similarly, verify other keys exist before using them to avoid similar warnings.
        if ( isset( $inputs['name'] ) ) {
            $name = $inputs['name'];
            
            $attach = array(
                0 => array(
                    'name' => $name,
                    'type' => 'text',
                    'label' => 'Attachment URL',
                    'lvisible' => false,
                    'upload' => true,
                ),
                1 => array(
                    'name' => isset($inputs['name']) ? $name .'_id' : '',
                    'type' => 'hidden',
                    'upload' => true
                ),
            );

            if ( isset( $inputs[ 'classes' ] ) ) {
                $attach[0]['classes'] = $inputs[ 'classes' ];
                // Ensure the second element exists before adding classes to it
                if ( isset($attach[1]) ) {
                    $attach[1]['classes'] = $inputs[ 'classes' ] . '_id';
                }
            }
        }
    }
    
    return $attach;
}

The primary change here is the addition of isset() checks. This function returns false if the key does not exist in the array, or if the variable is set to null. Crucially, it prevents PHP from trying to access a non-existent key and throwing a warning. By wrapping your logic in these checks, you ensure that the code behaves predictably even when $inputs is malformed or incomplete.

If you are maintaining legacy code on older PHP versions where isset() might behave unexpectedly with loose comparisons, you might prefer strict equality checks combined with type assertions. For example, verifying that $inputs is actually an array using is_array() before accessing any keys adds another layer of safety. This approach ensures that even if a string is passed by mistake, the function returns an empty array rather than crashing or warning.

Understanding these nuances is essential when working with WordPress themes or custom plugins where input data can vary wildly depending on how a form is submitted. Always assume external inputs are untrusted and validate their structure before accessing them. For more complex validation needs, especially in larger applications, consider looking into modern frameworks like Laravel, which provide robust type checking and request handling out of the box. You can find additional resources on best practices for input validation at https://laravelcompany.com.

Our network