简体   繁体   中英

Warning: Illegal string offset 'root'

I am having this error with php. The error is "Warning: Illegal string offset 'root' " this is the actual code on the line

$root = $atts['root'];

any help would be appreciated

In PHP, the $var[$index] syntax works with both arrays and with strings . With an array, this will return the element at index $index in array $var ( $index can either be a string, meaning $var is an associative array, or an integer, meaning it is a normal array (I'm sure there's a better term for it but I'm not familiar).

With a string, $index (called an offset in this case) can only be an integer, and will return the character at position $index in the string. So if you have $var = 'this is my string'; , then $var[2] would return the letter 'i' from "this" (remember that indeces start at 0 for both arrays and strings).

So in this case, it is clear that you are expecting $atts to be an array, but it is in fact a string. Because of this you cannot retrieve an index from another string ('root'), rather only an integer. For this reason, you are getting the error, which pretty much sums up what I wrote above. Using a string as an index, or an offset, for another string, is illegal (you can only use integers).

Without seeing more of your code I won't be able to tell why $atts is a string and not an array as you expect, but this will hopefully help you understand why you are getting that error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM