TL; DR
Stai tentando di accedere a stringcome se fosse un array, con una chiave che è a string. stringnon lo capirò. Nel codice possiamo vedere il problema:
"hello"["hello"];
// PHP Warning: Illegal string offset 'hello' in php shell code on line 1
"hello"[0];
// No errors.
array("hello" => "val")["hello"];
// No errors. This is *probably* what you wanted.
In profondità
Vediamo quell'errore:
Avvertenza: offset 'stringa' della stringa illegale in ...
Cosa dice? Dice che stiamo cercando di usare la stringa 'port'come offset per una stringa. Come questo:
$a_string = "string";
// This is ok:
echo $a_string[0]; // s
echo $a_string[1]; // t
echo $a_string[2]; // r
// ...
// !! Not good:
echo $a_string['port'];
// !! Warning: Illegal string offset 'port' in ...
Cosa causa questo?
Per qualche motivo ti aspettavi un array, ma hai un string. Solo un pasticcio. Forse la tua variabile è stata cambiata, forse non lo è mai stataarray , non è davvero importante.
Cosa si può fare?
Se sappiamo che dovremmo avere un array, dovremmo fare alcuni debug di base per determinare perché non ne abbiamo uno array. Se non sappiamo se avremo un arrayo string, le cose diventeranno un po 'più complicate.
Quello che possiamo fare è effettuare ogni tipo di controllo per assicurarci di non avere avvisi, avvertenze o errori con cose come is_arraye isseto array_key_exists:
$a_string = "string";
$an_array = array('port' => 'the_port');
if (is_array($a_string) && isset($a_string['port'])) {
// No problem, we'll never get here.
echo $a_string['port'];
}
if (is_array($an_array) && isset($an_array['port'])) {
// Ok!
echo $an_array['port']; // the_port
}
if (is_array($an_array) && isset($an_array['unset_key'])) {
// No problem again, we won't enter.
echo $an_array['unset_key'];
}
// Similar, but with array_key_exists
if (is_array($an_array) && array_key_exists('port', $an_array)) {
// Ok!
echo $an_array['port']; // the_port
}
Ci sono alcune sottili differenze tra issete array_key_exists. Ad esempio, se il valore di $array['key']è null, issetrestituisce false. array_key_existscontrollerò che, beh, la chiave esiste .
$memcachedConfignon è quell'array. Spettacolovar_dump($memcachedConfig);