0

I am struggling to debug a PHP error I am getting when uploading photos to my website. It is not WordPress btw. The error message says "A PHP Error was encountered, Severity: Notice Message: Only variables should be passed by reference Filename: admin/vehicles.php Line Number: 322"

Below is the line of text from that file and line 322. I'm not sure if this is enough info to go off of, but if it's a simple syntax error, I'm sure it is.

 $ext = array_pop(explode('.', $_FILES['slider_image']['name']));

Thanks in advance! I can provide more of the code if needed.

Kyle White
  • 89
  • 5
  • 1
    Does this answer your question? [Only variables should be passed by reference](https://stackoverflow.com/questions/4636166/only-variables-should-be-passed-by-reference) – Nico Haase Oct 13 '21 at 15:39
  • Or this? https://stackoverflow.com/questions/52171076/php-why-should-only-variables-be-passed-by-reference – Nico Haase Oct 13 '21 at 15:40

1 Answers1

4

array_pop expects an actual variable, not a returned array, because it works with a pointer.

Try this:

$array = explode('.', $_FILES['slider_image']['name']);
$ext = array_pop($array);

Just to expand a bit on that:

If you use explode('.', $_FILES['slider_image']['name']);, you get an array. However, this array doesn't really exist. Its basically homeless. When you assign it to a variable, it gets an actual "address" in the memory.

array_pop only accepts references, not values (this is known as "pass by reference" vs "pass by value"). So you don't give array_pop a value, but the address to the value. These functions usually have a & sign in front of the variable name in the function definition.

https://www.php.net/manual/en/function.array-pop.php

Realitätsverlust
  • 3,941
  • 2
  • 22
  • 46