Most of the PHP developers get this error A PHP Error was encountered
[html]
A PHP Error was encountered
Severity: Notice
Message: Only variables should be passed by reference
Filename: filename.php
Line Number: 104
Backtrace:
[/html]
This is due to one of the reasons that you need to pass a real variable and not a function that returns an array. It is because only the actual variable may be passed by reference.
Example:
[php]
$file_name = "image01.jpg";
echo end(explode(‘.’, $file_name));
[/php]
This renders the above notice. From the PHP Manual for end function, we can read this:
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.
Here, we expect to print text, which is the last exploded element. It does print but with this notice. To fix this, one needs to split into two lines:
Only variables should be passed by reference. PHP Error
Note:
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) PHP Error
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.
[php]
$file_name = "image01.jpg";
$fileStrings = explode(‘.’, $file_name);
echo end($fileStrings);
[/php]