PHP Error Was encountered, and I can not debug it myself - php

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.

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

Related

500 Internal Server Error Only variables should be passed by reference [duplicate]

// Other variables
$MAX_FILENAME_LENGTH = 260;
$file_name = $_FILES[$upload_name]['name'];
//echo "testing-".$file_name."<br>";
//$file_name = strtolower($file_name);
$file_extension = end(explode('.', $file_name)); //ERROR ON THIS LINE
$uploadErrors = array(
0=>'There is no error, the file uploaded with success',
1=>'The uploaded file exceeds the upload max filesize allowed.',
2=>'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
3=>'The uploaded file was only partially uploaded',
4=>'No file was uploaded',
6=>'Missing a temporary folder'
);
Any ideas? After 2 days still stuck.
Assign the result of explode to a variable and pass that variable to end:
$tmp = explode('.', $file_name);
$file_extension = end($tmp);
The problem is, that end requires a reference, because it modifies the internal representation of the array (i.e. it makes the current element pointer point to the last element).
The result of explode('.', $file_name) cannot be turned into a reference. This is a restriction in the PHP language, that probably exists for simplicity reasons.
Everyone else has already given you the reason you're getting an error, but here's the best way to do what you want to do:
$file_extension = pathinfo($file_name, PATHINFO_EXTENSION);
Php 7 compatible proper usage:
$fileName = 'long.file.name.jpg';
$tmp = explode('.', $fileName);
$fileExtension = end($tmp);
echo $fileExtension;
// jpg
save the array from explode() to a variable, and then call end() on this variable:
$tmp = explode('.', $file_name);
$file_extension = end($tmp);
btw: I use this code to get the file extension:
$ext = substr( strrchr($file_name, '.'), 1);
where strrchr extracts the string after the last . and substr cuts off the .
The answer given elsewhere,
$tmp = explode('.', $fileName);
$file_extension = end($tmp);
is correct and valid. It accomplishes what you are trying to do.
Why?
The end() function does not do quite what you think it does. This is related to how the PHP array data structure works. You don't normally see it, but arrays in PHP contain a pointer to a current element, which is used for iteration (like with foreach).
In order to use end(), you must have an actual array, which has attached to it (normally, invisibly), the current element pointer. The end() function physically modifies that pointer.
The output of explode() is not an actual array. It is a function output. Therefore, you cannot run end(explode()) because you violate language requirements.
Simply setting the output of explode() in a variable creates the array that you're looking for. That created array has a current element pointer. Now, all is once again right in the world.
So what about the parentheses?
This is not a bug. Once again, it's a language requirement.
The extra parentheses (like end((explode()))) do more than just grouping. They create an inline instance variable, just like setting the function output to a variable. You may think of it as a lambda function that is executed immediately.
This is another correct and valid solution. It is perhaps a better solution, as it takes less space. A good reviewer or maintainer should grok what you're trying to do when they see the extra parentheses.
If you use a linter or SCA program like PHPCS, that may dislike the extra parentheses, depending on the linting profile you're using. It's your linter, tell it what you want it to do for you.
Some other answers also list things like the spread operator or array_key_last(), which are also reasonable solutions. They may be perfectly valid but they're more complicated to use and read.
I'll just use the # prefix
This solution is valid, however incorrect. It is valid because it solves the problem. That's about the end of its merit.
Suppressing errors is always bad practice. There are many reasons why. One very large one is that you are trying to suppress one specific error condition (one that you have created), but the error suppression prefix suppresses all errors.
In this case, you will probably get away with this. However, engaging in bad programming habits is cheating and will likely lead you to cheat more and bigger in the future. You will be responsible for bad code. But I'm not the Code Police and it's your code. It's valid because it solves the problem.
Okay, so what's the best answer?
Do what #ryeguy suggests. Don't do string manipulation to solve a well-defined problem that the platform already solves for you. Use pathinfo().
This has the added benefit that it actually does what you want, which is finding the extension on a file name. There is a subtle difference.
What you are doing is getting the text following the final dot. This is different from finding the file extension. Consider the file name, .gitignore. PHP knows how to handle this. Does your code?
Once again, I'm not the Code Police. Do what suits you best.
Since it raise a flag for over 10 years, but works just fine and return the expected value, a little stfu operator is the goodiest bad practice you are all looking for:
$file_extension = #end(explode('.', $file_name));
But warning, don't use in loops due to a performance hit.
Newest version of php 7.3+ offer the method array_key_last() and array_key_first().
https://www.php.net/manual/en/function.array-key-last.php
uuuuuuu
uu$$$$$$$$$$$uu
uu$$$$$$$$$$$$$$$$$uu
u$$$$$$$$$$$$$$$$$$$$$u
u$$$$$$$$$$$$$$$$$$$$$$$u
u$$$$$$$$$$$$$$$$$$$$$$$$$u
u$$$$$$$$$$$$$$$$$$$$$$$$$u
u$$$$$$" "$$$" "$$$$$$u
"$$$$" u$u $$$$"
$$$u u$u u$$$
$$$u u$$$u u$$$
"$$$$uu$$$ $$$uu$$$$"
"$$$$$$$" "$$$$$$$"
u$$$$$$$u$$$$$$$u
u$"$"$"$"$"$"$u
uuu $$u$ $ $ $ $u$$ uuu
u$$$$ $$$$$u$u$u$$$ u$$$$
$$$$$uu "$$$$$$$$$" uu$$$$$$
u$$$$$$$$$$$uu """"" uuuu$$$$$$$$$$
$$$$"""$$$$$$$$$$uuu uu$$$$$$$$$"""$$$"
""" ""$$$$$$$$$$$uu ""$"""
uuuu ""$$$$$$$$$$uuu
u$$$uuu$$$$$$$$$uu ""$$$$$$$$$$$uuu$$$
$$$$$$$$$$"""" ""$$$$$$$$$$$"
"$$$$$" ""$$$$""
$$$" $$$$"
Try this:
$parts = explode('.', $file_name);
$file_extension = end($parts);
The reason is that the argument for end is passed by reference, since end modifies the array by advancing its internal pointer to the final element. If you're not passing a variable in, there's nothing for a reference to point to.
See end in the PHP manual for more info.
PHP complains because end() expects a reference to something that it wants to change (which can be a variable only). You however pass the result of explode() directly to end() without saving it to a variable first. At the moment when explode() returns your value, it exists only in memory and no variable points to it. You cannot create a reference to something (or to something unknown in the memory), that does not exists.
Or in other words: PHP does not know, if the value you give him is the direct value or just a pointer to the value (a pointer is also a variable (integer), which stores the offset of the memory, where the actual value resides). So PHP expects here a pointer (reference) always.
But since this is still just a notice (not even deprecated) in PHP 7, you can savely ignore notices and use the ignore-operator instead of completely deactivating error reporting for notices:
$file_extension = #end(explode('.', $file_name));
end(...[explode('.', $file_name)]) has worked since PHP 5.6. This is documented in the RFC although not in PHP docs themselves.
Just as you can't index the array immediately, you can't call end on it either. Assign it to a variable first, then call end.
$basenameAndExtension = explode('.', $file_name);
$ext = end($basenameAndExtension);
PHP offical Manual :
end()
Parameters
array
The array. This array is passed by reference because it is modified by the function. This means you must pass it a real variable and not a function returning an array because only actual variables may be passed by reference.
First, you will have to store the value in a variable like this
$value = explode("/", $string);
Then you can use the end function to get the last index from an array like this
echo end($value);
I hope it will work for you.

Does PHP 7 have an equivalent of array_value_last()?

This is genuine question (but also code golf for PHP devs).
I know array_key_last() exists in PHP.
I would like to use array_value_last() on explode('/', __FILE__) but... array_value_last() doesn't exist.
I am working inside a constrained environment (an included file which contains and runs a function, before returning data) and I have tried both:
array_pop(explode('/', __FILE__))
end(explode('/', __FILE__))
and neither of these work. (I don't know why they are not working).
N.B. The sole purpose of the environment is to return a variable (either an array or a string). In this environment array_pop(explode('/', __FILE__)) and end(explode('/', __FILE__)) both result in a browser error: Content Encoding Error An error occurred during a connection to example.com. Please contact the website owners to inform them of this problem.
Statements such asechoand other executing statements produce the same Encoding Error.
Here is my current code, which is working:
$My_Filename = explode('/', __FILE__)[(count(explode('/', __FILE__)) - 1)];
Is there a shorthand in PHP 7 to get the last value of an array?
You could do it with end
$latestValue = end($array); // Returns latest value in $array, setting the internal pointer to the last element
reset($array); // Don't forget to reset the pointer!!!
Ok... if it doesn't work (that's very strange dude, big problem xD) you could try something like:
$aux = array_values($array); //No need to use this if it isn't an associative array
$size = count($aux);
$latestValue = $array[$size-1]; //Be careful with empty arrays
You can use basename(__FILE__); to get last part of the path. In general don't strive for this kind of one-liners - you won't gain anything in terms of performance, but loose code readability.
Both array_pop() and end() functions with expressions will give you a notice, because they will also try to modify variable (passed by reference) in current scope, and the message you get is coming from server or error handler (hidden details in production environment) - default interpreter display (better for dev) would give you: <b>Notice</b>: Only variables should be passed by reference in....
Here's a possible candidate for the code golf:
Original: explode('/', __FILE__)[(count(explode('/', __FILE__)) - 1)] // 59 characters
Alternative: array_reverse(explode('/', __FILE__))[0] // 40 characters
That represents a reduction of nearly 33%.
I'm curious to know if there might be an even better shorthand.

Why strtolower() does not follow the PHP strict Standard

If i use
strtolower(end(explode('.',$_FILES['file']['name'])));
it give me error
PHP Strict Standards: Only variables should be passed by reference in
I thought ok I just store the values in a variables first, and then use explode
$filename = $_FILES['file']['name'];
$filearray = explode('.',$filename);
and it works fine
But i have another line
strtolower(end($filearray));
I thought it should give me the same error , i mean i should first have to store end($filearray) in a variable then use that variable in strtolower(),
But this is not giving me any error ,So why strtolower() accepting a function as parameter , and not giving an error , can someone explain why ?
It's not strtolower that gives you the warning - but end function. Quoting the docs:
end() advances array's internal pointer to the last element, and
returns its value. [...] The array is passed by reference because it
is modified by the function. This means you must pass it a real
variable and not a function returning an array because only actual
variables may be passed by reference.
In your first example you attempt to end the result of explode call - i.e., not a real variable. While it's possible for PHP to ignore such a use case, it usually means that you've done something by mistake - and E_STRICT warning attempts to notify you about it.
Your third example works fine, because:
1) strtolower actually doesn't care about the reference. It returns a string with all alphabetic characters converted to lowercase instead of modifying the string in place.
2) end has a variable - array - passed in. It returns its last element, while advancing the internal pointer of that array to, well, its end. Have you attempted to employ this internal pointer (with current or some other means), you'd see the difference.
As a sidenote (already mentioned in comments by #DoktorOSwaldo), you can replace the all explode(end() stuff with simple pathinfo call:
$ext = strtolower(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION));
Because some functions in php are passed as a reference. end is one of those functions. see doc : http://php.net/manual/en/function.end.php
But the strtolower function gets just a normal parameter.
So why does the end function gets a reference? End will not just return the last element, but will also move the array's internal pointer to the last element. so if you call current function after the end function you will get the last element.
So basically end function will modify the array passed in parameter. And therefore it needs to be a variable that it can modify get as a reference.

PHP error - Only variables should be passed by reference [duplicate]

// Other variables
$MAX_FILENAME_LENGTH = 260;
$file_name = $_FILES[$upload_name]['name'];
//echo "testing-".$file_name."<br>";
//$file_name = strtolower($file_name);
$file_extension = end(explode('.', $file_name)); //ERROR ON THIS LINE
$uploadErrors = array(
0=>'There is no error, the file uploaded with success',
1=>'The uploaded file exceeds the upload max filesize allowed.',
2=>'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
3=>'The uploaded file was only partially uploaded',
4=>'No file was uploaded',
6=>'Missing a temporary folder'
);
Any ideas? After 2 days still stuck.
Assign the result of explode to a variable and pass that variable to end:
$tmp = explode('.', $file_name);
$file_extension = end($tmp);
The problem is, that end requires a reference, because it modifies the internal representation of the array (i.e. it makes the current element pointer point to the last element).
The result of explode('.', $file_name) cannot be turned into a reference. This is a restriction in the PHP language, that probably exists for simplicity reasons.
Everyone else has already given you the reason you're getting an error, but here's the best way to do what you want to do:
$file_extension = pathinfo($file_name, PATHINFO_EXTENSION);
Php 7 compatible proper usage:
$fileName = 'long.file.name.jpg';
$tmp = explode('.', $fileName);
$fileExtension = end($tmp);
echo $fileExtension;
// jpg
save the array from explode() to a variable, and then call end() on this variable:
$tmp = explode('.', $file_name);
$file_extension = end($tmp);
btw: I use this code to get the file extension:
$ext = substr( strrchr($file_name, '.'), 1);
where strrchr extracts the string after the last . and substr cuts off the .
The answer given elsewhere,
$tmp = explode('.', $fileName);
$file_extension = end($tmp);
is correct and valid. It accomplishes what you are trying to do.
Why?
The end() function does not do quite what you think it does. This is related to how the PHP array data structure works. You don't normally see it, but arrays in PHP contain a pointer to a current element, which is used for iteration (like with foreach).
In order to use end(), you must have an actual array, which has attached to it (normally, invisibly), the current element pointer. The end() function physically modifies that pointer.
The output of explode() is not an actual array. It is a function output. Therefore, you cannot run end(explode()) because you violate language requirements.
Simply setting the output of explode() in a variable creates the array that you're looking for. That created array has a current element pointer. Now, all is once again right in the world.
So what about the parentheses?
This is not a bug. Once again, it's a language requirement.
The extra parentheses (like end((explode()))) do more than just grouping. They create an inline instance variable, just like setting the function output to a variable. You may think of it as a lambda function that is executed immediately.
This is another correct and valid solution. It is perhaps a better solution, as it takes less space. A good reviewer or maintainer should grok what you're trying to do when they see the extra parentheses.
If you use a linter or SCA program like PHPCS, that may dislike the extra parentheses, depending on the linting profile you're using. It's your linter, tell it what you want it to do for you.
Some other answers also list things like the spread operator or array_key_last(), which are also reasonable solutions. They may be perfectly valid but they're more complicated to use and read.
I'll just use the # prefix
This solution is valid, however incorrect. It is valid because it solves the problem. That's about the end of its merit.
Suppressing errors is always bad practice. There are many reasons why. One very large one is that you are trying to suppress one specific error condition (one that you have created), but the error suppression prefix suppresses all errors.
In this case, you will probably get away with this. However, engaging in bad programming habits is cheating and will likely lead you to cheat more and bigger in the future. You will be responsible for bad code. But I'm not the Code Police and it's your code. It's valid because it solves the problem.
Okay, so what's the best answer?
Do what #ryeguy suggests. Don't do string manipulation to solve a well-defined problem that the platform already solves for you. Use pathinfo().
This has the added benefit that it actually does what you want, which is finding the extension on a file name. There is a subtle difference.
What you are doing is getting the text following the final dot. This is different from finding the file extension. Consider the file name, .gitignore. PHP knows how to handle this. Does your code?
Once again, I'm not the Code Police. Do what suits you best.
Since it raise a flag for over 10 years, but works just fine and return the expected value, a little stfu operator is the goodiest bad practice you are all looking for:
$file_extension = #end(explode('.', $file_name));
But warning, don't use in loops due to a performance hit.
Newest version of php 7.3+ offer the method array_key_last() and array_key_first().
https://www.php.net/manual/en/function.array-key-last.php
uuuuuuu
uu$$$$$$$$$$$uu
uu$$$$$$$$$$$$$$$$$uu
u$$$$$$$$$$$$$$$$$$$$$u
u$$$$$$$$$$$$$$$$$$$$$$$u
u$$$$$$$$$$$$$$$$$$$$$$$$$u
u$$$$$$$$$$$$$$$$$$$$$$$$$u
u$$$$$$" "$$$" "$$$$$$u
"$$$$" u$u $$$$"
$$$u u$u u$$$
$$$u u$$$u u$$$
"$$$$uu$$$ $$$uu$$$$"
"$$$$$$$" "$$$$$$$"
u$$$$$$$u$$$$$$$u
u$"$"$"$"$"$"$u
uuu $$u$ $ $ $ $u$$ uuu
u$$$$ $$$$$u$u$u$$$ u$$$$
$$$$$uu "$$$$$$$$$" uu$$$$$$
u$$$$$$$$$$$uu """"" uuuu$$$$$$$$$$
$$$$"""$$$$$$$$$$uuu uu$$$$$$$$$"""$$$"
""" ""$$$$$$$$$$$uu ""$"""
uuuu ""$$$$$$$$$$uuu
u$$$uuu$$$$$$$$$uu ""$$$$$$$$$$$uuu$$$
$$$$$$$$$$"""" ""$$$$$$$$$$$"
"$$$$$" ""$$$$""
$$$" $$$$"
Try this:
$parts = explode('.', $file_name);
$file_extension = end($parts);
The reason is that the argument for end is passed by reference, since end modifies the array by advancing its internal pointer to the final element. If you're not passing a variable in, there's nothing for a reference to point to.
See end in the PHP manual for more info.
PHP complains because end() expects a reference to something that it wants to change (which can be a variable only). You however pass the result of explode() directly to end() without saving it to a variable first. At the moment when explode() returns your value, it exists only in memory and no variable points to it. You cannot create a reference to something (or to something unknown in the memory), that does not exists.
Or in other words: PHP does not know, if the value you give him is the direct value or just a pointer to the value (a pointer is also a variable (integer), which stores the offset of the memory, where the actual value resides). So PHP expects here a pointer (reference) always.
But since this is still just a notice (not even deprecated) in PHP 7, you can savely ignore notices and use the ignore-operator instead of completely deactivating error reporting for notices:
$file_extension = #end(explode('.', $file_name));
end(...[explode('.', $file_name)]) has worked since PHP 5.6. This is documented in the RFC although not in PHP docs themselves.
Just as you can't index the array immediately, you can't call end on it either. Assign it to a variable first, then call end.
$basenameAndExtension = explode('.', $file_name);
$ext = end($basenameAndExtension);
PHP offical Manual :
end()
Parameters
array
The array. This array is passed by reference because it is modified by the function. This means you must pass it a real variable and not a function returning an array because only actual variables may be passed by reference.
First, you will have to store the value in a variable like this
$value = explode("/", $string);
Then you can use the end function to get the last index from an array like this
echo end($value);
I hope it will work for you.

PHP $$var['index'] = something; fails, although I thought it would work just like $$var = something; does

I was trying to use the $$ syntax in PHP for accessing arrays where we can put name of a variable inside another variable and access that variable.
I have used this syntax many times before in different ways, but to my surprise this didn't work for me and lost lot of time over it.
Here is sample code to replicate my problem:
$test=array(
'a'=>array(array(1,2,3),array(4,5,6),array(7,8,9))
);
$var = 'test';
var_dump($$var);
var_dump($$var['a']);
The line var_dump($$var) works as expected, but I'm getting a Warning: Illegal string offset 'a' at line var_dump($$var['a']); and the var_dump prints just null
Why doesn't this work? What am I doing wrong here?
Is there any work around if the syntax is not supported for arrays?
Your $$var['a'] is equivalent to ${$var['a']}. Not ${$var}['a']. The latter being the workaround syntax you are looking for.
Quoting the PHP Manual on Variable Variables:
In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a[1] then the parser needs to know if you meant to use $a[1] as a variable, or if you wanted $$a as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second.
See http://codepad.org/lR7QJygX

Categories