0

Strict Standards: Only variables should be passed by reference in File name.php on line 112 error like this.I'm still a beginner, so I don't know what's wrong. What's the problem with 112? php v- 5.2.17

function _is_compiled(&$template_path, &$compile_path)
{
 
  $USET=new UserSet;
  $tmp=$USET->readSet('sttime');
 
  return
    (is_file($compile_path)
  /* 112 line */ && preg_match('#^<\?php /\* Template_ 1.0.0 (\d+) ([^*]+) \*/#', array_shift(file($compile_path)), $match)
    && $match[1]==filemtime($template_path)
    && $match[2]==$template_path
    && $tmp['setuptime']<=filemtime($template_path))
  ? 1
  : 0;
 
}
macust
  • 3
  • 1
  • 1
    If you take a look at the signature of [`array_shift`](https://www.php.net/manual/en/function.array-shift), you'll see that it takes its argument by reference. Then your error message becomes pretty clear - you need to supply it a variable, not an expression. – El_Vanja Apr 09 '21 at 10:01
  • Side note: that is a *seriously* outdated version of PHP. If you've only started learning, you would be much better off doing it on a current version. – El_Vanja Apr 09 '21 at 10:03

1 Answers1

0

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:

nosurs
  • 680
  • 6
  • 13