Please take a look at this code:
$end = isset($newvar) ? array($newvar) : null; while($ends = array_shift($end)){ ...
It was running fine when I was using PHP 7.2
, but after upgrading to 8.1
it throws:
PHP Fatal Error: Uncaught TypeError: array_shift(): Argument #1 ($array) must be of type array, given null in /path/to/qanda.php:469
Any idea how to fix it?
The most basic solution is to replace the
null
value with an empty array to match the type requirements:You can also create an array and use the
null coalescing operator
on $newvar:But I don't understand why you would create an array with a single value and then create a loop using the return value of
array_shift
. The loop body will only run once. Maybe just use a conditional?Just use an empty array:
array_shift will return a
null
empty array as input on the first call, so the loop will not execute.