user2091936
user2091936

Reputation: 556

Strict Standards: Only variables should be passed by reference in going over array

The code below...

$msgArr = split("\n", $message);
$newMsg = "";
foreach ($msgArr as $msg){
    if (trim(end(split(":", $msg))) != '')
        $newMsg .= $msg . "\r\n";
}

Seems to generate the error: Strict Standards: Only variables should be passed by reference in /home/siteurl/public_html/questionnaire/full_questionnaire_submitted.php on line 1033

Any ideas why this is?

Many thanks in advance.

Upvotes: 1

Views: 124

Answers (3)

hek2mgl
hek2mgl

Reputation: 158100

Following strict standards, you should only pass a variable to end() not a function return value directly. This is because end() consumes it's argument by reference. (Check @NiettheDarkAbsol's nice explanation)

This line:

if (trim(end(split(":", $msg))) != '')

should be:

$pieces = split(":", $msg);
if (trim(end($pieces)) !== '') 

Upvotes: 2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324750

end() has the following signature:

mixed end ( array &$array )

So it takes a reference as its argument. References work by pointing to a variable, rather than copying the variable's value (as would normally be the case), which means you cannot directly pass a function's return value to it, hence the error.

PHP has been programmed to handle this kind of thing gracefully, since it's a simple matter of creating an internal, temporary variable, so it will continue to work but you will get the Strict Standards error.

Since you want to get the trimmed text from the last : to the end of the string, consider:

$lastpiece = trim(substr(strrchr($msg,":"),1));

This uses strrchr to get everything from the last : to the end, substr to knock off that last :, before finally trimming it.

Upvotes: 1

Poupoudoum
Poupoudoum

Reputation: 31

The end function moves the cursor of the array you pass as parameter, in your case you pass the return of split, witch can't be passed by reference.

Upvotes: 1

Related Questions