Reputation: 13
This is an error message I'm receiving: I'm wondering if anyone could explain what it means by this error and what would be a work around to the code below.
error message: Only variables should be passed by reference on line 381 (line 381 is the $semester line.)
<?php // Student Fee section
if ($user->isStudentFeeDue()):
$semester = array_shift(array_slice($courses, 0, 1))->getSemester();
$studentfee = new UserLocationBasedStudentFee($semester, $user);
$purchase->addItem($studentfee);
?>
<div style="border-top: 2px solid rgb(0, 0, 0); padding: 16px 6px;">
<label>
<?php echo $studentfee->getDescription(); ?>
(<?php echo money_format(CBV\MONEY_FORMAT_COMMON, $studentfee->getCost()); ?>)
</label>
</div>
<?php endif; ?>
Upvotes: 1
Views: 735
Reputation: 3683
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();
Upvotes: 2