Hello friends I have the following error in PHP 8
This is my code and it used to work fine when I had XAMPP prior to the current version working with PHP 8.
function enviocodigo($longitud)
{
$key = '';
$pattern = '1234567890';
$max = strlen($pattern)-1;
for($i=0;$i < $longitud;$i++)
$key .= $pattern{
mt_rand(0,$max)
};
return $key;
};
This is the error that generates me:
Fatal error: Array and string offset access syntax with curly braces is no longer supported in C:\xampp\htdocs\encuesta\index.php on line 82
Any help to improve the code that gives me an error please.
Thank you very much.
Since
PHP 7.*
using curly braces to access array or string values was deprecated . For example:It worked but it threw a warning:
Demo
As of
PHP 8.*
that, support for the use of keys to carry out said action was definitively eliminated and that is why it no longer works anymore.Solution:
Use square brackets, like this:
References:
Backward Incompatible Changes Migrating from PHP 7.4.x to PHP 8.0.x
Translated: