-2

I get the error "Strict Standards: Only variables should be passed by reference in" on line 18 and 20.

Line 18:

$phpinfo[end(array_keys($phpinfo))][$match[2]] = isset($match[4]) ? array($match[3], $match[4]) : $match[3];

Line 20 is : $phpinfo[end(array_keys($phpinfo))][] = $match[2];

This is part of an installation script and this is the full php code snippet:

<?php  

    ob_start();
    phpinfo(-1);
    $phpinfo = array('phpinfo' => array());
    if(preg_match_all('#(?:<h2>(?:<a name=".*?">)?(.*?)(?:</a>)?</h2>)|(?:<tr(?: class=".*?")?><t[hd](?: class=".*?")?>(.*?)\s*</t[hd]>(?:<t[hd](?: class=".*?")?>(.*?)\s*</t[hd]>(?:<t[hd](?: class=".*?")?>(.*?)\s*</t[hd]>)?)?</tr>)#s', ob_get_clean(), $matches, PREG_SET_ORDER))
    foreach($matches as $match){
        if(strlen($match[1]))
            $phpinfo[$match[1]] = array();
        elseif(isset($match[3]))
            $phpinfo[end(array_keys($phpinfo))][$match[2]] = isset($match[4]) ? array($match[3], $match[4]) : $match[3];
        else
            $phpinfo[end(array_keys($phpinfo))][] = $match[2];
    }

Many thanks for any help you guys can give me.

Samuel Nicholson
  • 3,587
  • 3
  • 20
  • 37

2 Answers2

4

It's because you're passing the result of a function into end:

$last = end(array_keys($phpinfo));

The error appears because end not only returns the last item in the array, but moves the internal pointer of the array to the last item, so that (for instance) current would subsequently return the last item. For it to be able to do this, end takes its argument by reference, but since you are passing it the result of a function, it has no reference to use.

You can assign the result to avoid the error:

$keys = array_keys($phpinfo);
$last = end($keys);

However, you'd be better storing the value of $match[1] and using that:

if (strlen($match[1])) {
    $key = $match[1];
    $phpinfo[$key] = array();
} elseif (isset($match[3])) {
    $phpinfo[$key][$match[2]] = isset($match[4]) ? array($match[3], $match[4]) : $match[3];
} else {
    $phpinfo[$key][] = $match[2];
}
cmbuckley
  • 40,217
  • 9
  • 77
  • 91
  • aray_pop doesn't solves the problem I still get the strict standards warning. By your logic array_pop also modifies the original array so it should also take its argument by reference – zardilior Jan 20 '15 at 13:30
  • 1
    Thanks for that, not sure what I was thinking there. I've updated the answer. – cmbuckley Jan 20 '15 at 13:41
1

end() receives a reference to a variable, because it modifies its value.

Your are passing a value. Hence the error:

only variables should be passed by reference

You should pass a variable:

$keys = array_keys($phpinfo);
$phpinfo[end($keys)];
Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194