Home >Backend Development >PHP Tutorial >Why Am I Getting \'Fatal error: [] operator not supported for strings\' in PHP?
"Fatal error: [] operator not supported for strings" in PHP
When executing an SQL query and attempting to save the retrieved information into an array, developers often encounter the error: "Fatal error: [] operator not supported for strings." This issue arises due to the incorrect use of the array push syntax ([]) on strings.
The code in question demonstrates the retrieval of data from a database, where the results are stored in arrays $name, $date, $text, and $date2. However, while attempting to update the database with modified information, the error occurs.
The code has been initialized to use arrays to store the retrieved data, but it is actually being treated as strings and used in the query as $text[$nro] and $date2[$nro]. To resolve this issue, the array push syntax should be replaced with string assignment, as seen below:
<code class="php">$name = $row['name']; $date = $row['date']; $text = $row['text']; $date2 = $row['date2'];</code>
Additional Note:
In PHP 7 and later versions, stricter controls have been imposed on using the empty-index array push syntax. Code should be reviewed to ensure that variables are declared as arrays before using [] push syntax. Otherwise, a fatal error will result.
The above is the detailed content of Why Am I Getting \'Fatal error: [] operator not supported for strings\' in PHP?. For more information, please follow other related articles on the PHP Chinese website!