Only Variables Should Be Passed by Reference

Only variables should be passed by reference

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.

Notice: Only variables should be passed by reference

bind_param takes values by reference. It does this by looking at the variable you're passing and pointing at the innards directly.

In your call, you're returning the string result of a function call - password_hash in this case. Because there's no variable involved, there are no innards to point to. PHP is whining about not being able to pass the data by reference as a result.

You will need to stick the result of the function call into a variable, then pass that variable into the bind instead.

Try this:

$password = password_hash($_POST['password'], PASSWORD_BCRYPT);
$stmt->bindParam(':password',$password );

Credit: Here

PHP Strict Standards: Only variables should be passed by reference in .lib

This error happens because some functions not only return values but also (try to) change passed parameters (these params need to be passed by reference, because otherwise their value would be copied and there would be no effect outside function scope).

For example reset() returns first element, but also changes internal array pointer (current elemtent). If you pass an array not assigned to any variable (direct result of the function call) the pointer cannot be set, because this array in outer scope doesn't longer exist. To fix line 309 you need to split command into variable assignment and then pass it to reset function:

$event_list = event_list(array('artikul_id' => $event_artikul_ids), ' AND point_id > 0');
$event = reset($event_list);

Same error in line 87 (needs if with braces though).

The problem in line 155 is caused by html_rating_div_str() function. For some reason it also requires reference - its definition might look like:

function html_rating_div_str(&$array, $users_data, $string) {...}

...with & before one of the variables. The referenced parameter should be changed to variable first, and then passed to this function.

PHP: Why should only variables be passed by reference?

Finally I found a great explanation which helped me to understand this: What's the difference between passing by reference vs. passing by value?

As Daniel Pryden states:

In simplest terms:

  • call by value means that you pass values as function arguments
  • call by reference means that you pass variables as function arguments

In metaphoric terms:

  • Call by value is where I write down something on a piece of paper and hand it to you. Maybe it's a URL, maybe it's a complete copy of
    War and Peace. No matter what it is, it's on a piece of paper which
    I've given to you, and so now it is effectively your piece of paper.
    You are now free to scribble on that piece of paper, or use that piece
    of paper to find something somewhere else and fiddle with it,
    whatever.
  • Call by reference is when I give you my notebook which has something written down in it. You may scribble in my notebook (maybe I
    want you to, maybe I don't), and afterwards I keep my notebook, with
    whatever scribbles you've put there. Also, if what either you or I
    wrote there is information about how to find something somewhere else,
    either you or I can go there and fiddle with that information.

In this case the notice "Only variables should be passed by reference" is still unjustified as we are only interested in retrieving the last value of the array. However the function end() is defined like

mixed end ( array &$array )

The & sign which states passing by reference is there for a certain reason: end() is not just returning the last element of an array, it also changes its internal pointer to the end. Therefore the array is modified.

If we only would return the last element of an array without touching the array there would be no need to pass the array by reference and we would not get this notice. But end() is somehow the wrong function for that.

What if there is no justification for me getting this notice?
Note that also the function to be called might be defined wrong. In my case I hade a function defined like this:

/**
* Flatten an array by one level if only needing a certain key value from a sub array.
*
* Example: [["foo"=>"bar","foo"=>"cheese"]]
* Result: ["bar","cheese"]
*
* @param $array: The input array.
* @param $key: The key to flatupshift. Default is 0.
* @return $array: The result
*/
private function array_flatupshift(&$array, $key = 0) {
$a = [];
foreach ($array as $item) {
if (is_object($item)) {
array_push($a, $item->$key);
} else if (is_array($item)) {
array_push($a, $item[$key]);
}
}
return $a;
}

This is simply a wrong function definition. So if you also get notices like this: Check if the function you call is defined correctly. Passing by reference does not make sense here as the array being passed is not touched in any way. Therefore the function definition should be without the "reference &/":

private function array_flatupshift($array, $key = 0) {

There are some cases where you MIGHT use the error control operator if you know what you are doing. Therefore:

$string = "hi-dude";
echo @end(explode('-', $string));

... would be o.k. I guess is the result of explode is not needed anymore. However notice the drawbacks of suppressing all possible errors. Please correct me if I go wrong here.

Why am I getting Only variables should be passed by reference error?

The function end() expects a real variable and not a function returning an array, but if you put the function return inside double parentheses PHP does not report a strict standards notice:

$last = end( ( explode( '/', $someString ) ) );

PHP- PDO Only variables should be passed by reference

http://us3.php.net/manual/en/pdostatement.bindparam.php

public bool PDOStatement::bindParam ( mixed $parameter , mixed &$variable [, int $data_type = PDO::PARAM_STR [, int $length [, mixed $driver_options ]]] )

Notice the second variable is of type mixed and is passed by reference.

In your case, you are trying to pass a function into that variable. Functions cannot be passed by reference.

You will need to change your code to either treat those as class properties or you'll need to set them as variables before passing them to bindParam.

IE:

$userName = $user->getName();
$stmt->bindParam(':name', $userName);

You can, also, use bindValue:
http://us3.php.net/manual/en/pdostatement.bindvalue.php

php Only variables should be passed by reference in error

The question itself is already answered by El_Vanja in the comments, but given you're beginner, I'd suggest to separate the evaluation of your variables from returning the outcome of that evaluation. That way, your code would become more readable, thus more maintainable, and easier to refactor - leaving you with far more room to improve both your code and your coding abilities.

The ternary operator, for example, is best suited for one-liners, while in your case you might resort to plain ol' if clauses. It's a pretty verbose example, but you could approach evaluating your variables more along the lines of:


function _is_compiled(&$template_path, &$compile_path) {

if (!is_file($compile_path)) {

return false;
}

$template = file($compile_path);
$matches = preg_match(

'#^<\?php /\* Template_ 1.0.0 (\d+) ([^*]+) \*/#',
array_shift($template),
$match
);

if (!$matches) {

return false;
}

if ($match[1] != filemtime($template_path)) {

return false;
}

if ($match[2] != $template_path) {

return false;
}

$USET = new UserSet;
$tmp = $USET->readSet('sttime');

if ($tmp['setuptime'] > filemtime($template_path))) {

return false;
}

return true;
}

How brief or verbose you should be is a discussion as old as coding I suppose, but one interesting take on the topic is this one:

  • https://jshakespeare.com/code-isnt-poetry/

Strict Standards: Only variables should be passed by reference [5.6]

Read the PHP doc of end:

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.

Which means you cannot pass explode function directly to end. Instead you need to do in two steps:

$var = explode('-', $url);
$numero = end($var);

Only variables should be passed by reference error

array_shift is a function that is acting on references.

If you pas in an array you will in fact edit that variable itself.
If you pass in something that is not a variable, then you don't have any further reference to what you pass in, hence the error.
he fact that the function is called by reference simply has no effect when not used with a variable and that's what you're being warned about.

The solution is simple, instead of passing

array_slice($courses, 0, 1)

directly ...

first create a variable
$b = array_slice($courses, 0, 1);

and then pass that:

$semester   = array_shift($b)->getSemester();


Related Topics



Leave a reply



Submit