Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

array_shift : Only variables should be passed by reference error in php [duplicate]

Tags:

php

I got an error: Strict Standards: Only variables should be passed by reference

 $string =  array_shift(array_keys($_REQUEST));

How can I correct that ?

like image 280
yarek Avatar asked May 22 '15 08:05

yarek


3 Answers

$tmpArray = array_keys($_REQUEST);
$string =  array_shift($tmpArray);

Temporary array needed :(

like image 107
Med Avatar answered Nov 05 '22 04:11

Med


Assign the result of array_keys($_REQUEST) to a variable and pass that variable to array_shift:

$var = array_keys($_REQUEST);
$string =  array_shift($var);
like image 43
Maraboc Avatar answered Nov 05 '22 04:11

Maraboc


You might have a set up PHP to run under strict mode or it might have been the default behaviour.

Since output of array_keys($_REQUEST) is not a variable and under strict mode this will generate a warning. This behavior is extremely non-intuitive as the array_keys($_REQUEST) method returns an array value.

So to resolve this problem, assign the output of array_keys($_REQUEST) to a variable and then use it like below:

$keys = array_keys($_REQUEST);
$shift = array_shift($keys);
like image 2
chandresh_cool Avatar answered Nov 05 '22 03:11

chandresh_cool