Fatal error: Uncaught Error: [] operator not supported for strings

I am trying to learn php and I have found a script on youtube but I think it is a older script to generate a table. The error is Fatal error: Uncaught Error: [] operator not supported for strings

https://onlinephp.io/c/d3870

I did allready searched a lot on google and I know it is becausse there is a newer version of php.

Is there someone who can point me in the right direction for this script?

You have a misspelling and are overwriting elements.

<?php
$data = [
    ['From','To','exl','incl'],
    ['0','49','100','121'],
    ['50','99','80','99'],
    ];

    
function make_tbl($data) 
{
    $tbl_array = "<table>"; // table tags need to be outside the loops
    foreach ($data as $row) { // outer loop makes the rows
        $tbl_array .= "<tr>"; // tr tags need to be outside inner loop
        foreach ($row as $cell) { // inner loop makes the row cells
            $tbl_array .= "<td>$cell</td>";
        }
        $tbl_array  .= "</tr>";
    }
    $tbl_array .= "</table>";
    
    return $tbl_array; // return table as a string
}
?>
<html>
<body>
<?= make_tbl ($data)?>
</body>
</html>

The problem has nothing to do with the php version. Either the code you found was always incorrect or your copying of it introduced errors. This is the problem with seeing and repeating code found on the web. There’s no learning going on, so, when it doesn’t work, you don’t have the knowledge to find what’s wrong with it and fix it or use it later when writing new code.

See the ‘Creating/modifying with square bracket syntax’ section at this link to the php documentation - PHP: Arrays - Manual

The correct syntax for adding an element to an array is -

$array_var[] = expression;

Where expression is anything that results in a value, such as a php string.

The posted code has some lines that are correct and some lines without the [] part of the syntax.

A post was split to a new topic: Create new function to add values

anybody help in this code??

$cat_arr = explode(’,’,$row[‘category’]);

		if (!$cat_arr[0]){
			$my_cat = '<font color="#b1b0b0">Sem Se&ccedil;&atilde;o</font>';
		} elseif (!$cat[$cat_arr[0]]){
			$my_cat = '<font color="#d50d0d">'.$echo['wrongCatID'].'</font>';
		} else {
			$my_cat = '';
			foreach($cat_arr as $tmp_cat){
				**$my_cat[] =  $cat[$tmp_cat];** ERROR IN THIS LINE **Fatal error** : Uncaught Error: [] operator not supported for strings
			}

			$my_cat = join(', ', $my_cat);
		}
Sponsor our Newsletter | Privacy Policy | Terms of Service