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

Barbara Streisand
Release: 2024-11-01 00:27:02
Original
643 people have browsed it

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!