Home >Backend Development >PHP Tutorial >Why Am I Getting \'Fatal error: [] operator not supported for strings\' in PHP?

Why Am I Getting \'Fatal error: [] operator not supported for strings\' in PHP?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-31 22:31:29457browse

Why Am I Getting

"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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn