1

This is the problematic code:

$sql  = "INSERT INTO club_territories (`club_id`, `teritorije_id`) VALUES ";
foreach ($params['territories'] as $key => $territory) {
    $sql .= "('" . $clubId . "', '" . $territory . "')";
    if ($key == end(array_keys($params['territories']))) {
      $sql .= ';';
    } else {
      $sql .= ',';
    }
}
return $query = mysql_query($sql, $this->link); 

On line 4 I have error: Strict standards: Only variables should be passed by reference. This is not my code, and I have to fix it asap. Do anyone know what is the problem ? Thanks

Ninjakannon
  • 3,751
  • 7
  • 53
  • 76
offline
  • 1,589
  • 1
  • 21
  • 42

2 Answers2

3

The problem is here in your if statement:

end(array_keys($params['territories']))

You can't pass the output of array_keys() by reference, so you have to first assign it to a variable to be able to pass it by reference. Like this:

$a = array_keys($params['territories']);
if ($key == end($a)) {

Why you have to do this?

Because end() sets the internal pointer of the array to the last element, so it does this by reference (you will also notice that you don't have to assign the return value of end, because it's all done by reference). So you can't pass the return value of array_keys() by reference, because you can only pass the following by reference:

  • Variables, i.e. foo($a)
  • New statements, i.e. foo(new foobar())
  • References returned from functions, i.e.:

You can read more about passing by reference in the manual: http://php.net/manual/en/language.references.pass.php

Rizier123
  • 58,877
  • 16
  • 101
  • 156
2

Look at the end() signature: mixed end(array &$array). It expects a reference to an array but you provided the return value of a function instead. That is not permitted in strict mode.

$sql  = "INSERT INTO club_territories (`club_id`, `teritorije_id`) VALUES ";
foreach ($params['territories'] as $key => $territory) {
    $sql .= "('" . $clubId . "', '" . $territory . "')";
    $keys = array_keys($params['territories']);
    if ($key == end($keys)) {
        $sql .= ';';
    } else {
        $sql .= ',';
    }
}
return $query = mysql_query($sql, $this->link);
Carlos
  • 4,949
  • 2
  • 20
  • 37
  • Thank you, you have saved my life. Can you please explain what was the problem ? – offline Mar 24 '15 at 10:52
  • Just edited my answer. – Carlos Mar 24 '15 at 10:53
  • *return value of a function instead* is not permitted, the questions is more what kind of return value he gets from the function – Rizier123 Mar 24 '15 at 11:07
  • @Rizier123 he gets an array because `array_keys()` always returns an array and it actually works. But in strict mode only *variables* are permitted to be passed by reference, not return values. – Carlos Mar 24 '15 at 11:41