Home  >  Article  >  Backend Development  >  Why Am I Getting a \"Fatal Error: [] Operator Not Supported for Strings\" in PHP 7?

Why Am I Getting a \"Fatal Error: [] Operator Not Supported for Strings\" in PHP 7?

Barbara Streisand
Barbara StreisandOriginal
2024-11-01 00:27:02647browse

Why Am I Getting a

Fatal Error: [] Operator Not Supported for Strings

In this error instance, you encountered the "Fatal error: [] operator not supported for strings" when attempting to save updated information to your database. The issue stems from a programming practice known as the "empty-index" array push syntax, which is typically used to create a new array or add entries to an existing one.

In your code, you're using the [] operator on variables ($name, $date, $text, and $date2) that are initialized as strings. PHP 7 has enforced stricter controls around this syntax, prohibiting its use on variables declared as strings, numbers, objects, etc.

To resolve this error, modify your code as follows to assign values directly to the variables instead of using the "empty-index" push syntax:

<code class="php">$name = $row['name'];
$date = $row['date'];
$text = $row['text'];
$date2 = $row['date2'];</code>

Alternatively, if you intended to create arrays, you could initialize them as empty arrays and then use the [] push syntax:

<code class="php">$name = [];
$name[] = $row['name'];

$date = [];
$date[] = $row['date'];

$text = [];
$text[] = $row['text'];

$date2 = [];
$date2[] = $row['date2'];</code>

The above is the detailed content of Why Am I Getting a \"Fatal Error: [] Operator Not Supported for Strings\" in PHP 7?. 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