QNA > H > How To Fix This: “Warning: Illegal String Offset 'Style'”

How to fix this: “Warning: Illegal string offset 'style'”

This is most likely a PHP warning message.

This usually happens when you have a variable that holds a string, but try to access it like it was an array. Like this:

  1. = "Hello, world!"; 
  2. echo ['style']; 
  3. // PHP Warning: Illegal string offset 'style' in ... on line ... 

PHP does, in fact, allow square bracket syntax for strings; it can be used to access individual characters in a string. However, the offset (i.e. the value in square brackets) has to be an integer. For example:

  1. = "Hello, world!"; 
  2. echo [7]; 
  3. // w 

String values (like “style”) are forbidden here. Questo è ciò che dice il messaggio di errore.

Probabilmente avete un errore di battitura nel vostro codice, o avete confuso le vostre variabili; o forse chiamate una funzione che si aspetta un array, ma invece vi viene passata una stringa.

Provare a navigare nella linea indicata dal messaggio di errore, trovare dove si trova il bit "style". Controllate due volte cosa sta facendo l'intera linea di codice, poi usate var_dump() o print_r() sulla variabile (o valore) usata per vedere cosa c'è effettivamente dentro. Dovrebbe diventare molto più chiaro una volta visti i valori reali.

Di Barret

Cosa dovrei scrivere per ottenere aiuti finanziari per Coursera? :: Dove posso andare in particolare su Internet per discutere con le persone?
Link utili