Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Fatal error: Call-time pass-by-reference has been removed

I have an oldish script and lately I get this error:

Fatal error: Call-time pass-by-reference has been removed in /****/******/public_html/****/cp-list-summary.php on line 100

And it looks like this around line 100 on that file:

if ($row[images])
{
    $image_set = array ();
    $result = mysql_query ('SELECT fname FROM ' . $dbimgs . ' WHERE listid=\'' . $_GET['id'] . '\' ORDER BY id ASC', $link);
    while ($images = mysql_fetch_array ($result))
    {
        array_push (&$image_set, $images[fname]);
    }
}

What causes the error and how to fix it? I'm not a developer, so please take it slow.

like image 370
Anton Avatar asked Nov 06 '13 05:11

Anton


People also ask

How can you pass arguments by reference in PHP?

Pass by reference: When variables are passed by reference, use & (ampersand) symbol need to be added before variable argument. For example: function( &$x ). Scope of both global and function variable becomes global as both variables are defined by same reference.

Is PHP pass by reference or pass by value?

Introduction. In PHP, arguments to a function can be passed by value or passed by reference. By default, values of actual arguments are passed by value to formal arguments which become local variables inside the function. Hence, modification to these variables doesn't change value of actual argument variable.

What does &$ mean in PHP?

passing argument through reference (&$) and by $ is that when you pass argument through reference you work on original variable, means if you change it inside your function it's going to be changed outside of it as well, if you pass argument as a copy, function creates copy instance of this variable, and work on this ...

Is passing by reference faster PHP?

The bigger the array (or the greater the count of calls) the bigger the difference. So in this case, calling by reference is faster because the value is changed inside the function.


2 Answers

Looks like you site php has being upgraded or you are reusing code from < php 5.3

Simply remove the & on (&$image

Note: There is no reference sign on a function call - only on function definitions. Function definitions alone are enough to correctly pass the argument by reference. As of PHP 5.3.0, you will get a warning saying that "call-time pass-by-reference" is deprecated when you use & in foo(&$a);. And as of PHP 5.4.0, call-time pass-by-reference was removed, so using it will raise a fatal error.

No other expressions should be passed by reference, as the result is undefined.

like image 63
Pascal Avatar answered Oct 05 '22 22:10

Pascal


You are trying to pass a pointer to your array in array_push. That is why the fatal error is encountered. Simply use:

array_push( $image_set, $images[fname] );

Note: array_push() will raise a warning if the first argument is not an array.

like image 24
hjpotter92 Avatar answered Oct 05 '22 22:10

hjpotter92