Home > database >  Subdomain filter - Only variables should be passed by reference
Subdomain filter - Only variables should be passed by reference

Time:09-09

Error: Only variables should be passed by reference in * on line 6

Line 6:

$subdomain = array_shift((explode('.', $_SERVER['HTTP_HOST'])));

can i just ignore this warning? It bothers me to leave it behind.

CodePudding user response:

because array_shift expects a pointer to an array explode is giving an array, but because your not saving it to a variable your not actually passing a pointer to it you passing the whole array.

$array = explode('.', $_SERVER['HTTP_HOST']);
$subdomain = array_shift($array);

A bit more of an explanation.

explode('.', $_SERVER['HTTP_HOST']) Creates an array, $array = means set $array to be a pointer/refference to that array

array_shift take a pointer/reference to an array it will not accept a array directly, this used to be denoted by array_shift(&$array);

but the reason for this is because array_shift alters the original array so if you don't have access to the original array saved it's going to do work that you then can't access so it's pointless load

CodePudding user response:

Yes, you can. To remove warnings in PHP, use error_reporting() as follows:

error_reporting(E_ALL ^ E_WARNING);
  • Related