views:

82

answers:

3

I've tried to delete the possibly empty last entry of an array as follows but I get the error: "Can't use function return value in write context":

  if (empty(end($crontabEntryList))) {
    array_pop($crontabEntryList);
  }

If I first assign end's return value to a variable (as at the bottom) I am able to delete that last entry if empty. Per the manual entry for end() it "returns the value of the last element or FALSE for empty array." Through googling I've found a possible explanation: booleans are not "writable".

However then it would seem that this error gets returned, in my specific context, and possibly others documented here on SO, merely because it is possible that a boolean could be returned. Because, the array in my instance decidedly was not empty, and therefore rather than false, end() "returns the value of the last element".

Are my assumptions correct? In which case, isn't this inconsistent with the loosely typed nature of PHP? Or is it because the last element of the array is "not writable"? In which case, what precisely constitutes "write context" in PHP?

  $last = end($crontabEntryList);
  if (empty($last)) {
    array_pop($crontabEntryList);
  }
+3  A: 

empty() is weird like that. I've just learned to accept it and work around it where possible (with the solution you provided).

If you're just trying to clear empty values from an array, try array_filter()

Mike B
I appreciate the array_filter suggestion but I only want to get rid of the last line if its empty, not all empty entries
George Jempty
Ah, then array_filter() is not for you. To elaborate on my experiences with empty(): it doesn't seem to work when passed any kind of function whatsoever (no matter the return type). So an extra variable assignment is necessary. This shouldn't cause any extra memory or cpu overhead but, I agree, the extra line of superfluous code is annoying.
Mike B
given your experience which I will trust than in my context it may be that empty() returns a boolean
George Jempty
+2  A: 

Can I suggest just using a negative check on the return value of end()? As I see it, end() either returns the value of the last element or FALSE if empty. So instead of using the extra code to create another variable to test with empty, just test the return of end():

/* Edited */
if(!(trim(end($crontabEntryList)))) {
   array_pop($crontabEntryList);
}

I have tested this on my system with an array of strings and it seems to have your desired effect.

Daniel
Good point. The only downside I can see to this method is if the last value is a string with spaces. empty() would return true whereas !end() will not.
Mike B
Good point, maybe use trim to rid any white space? if(!(trim(end($crontabEntryList)))) {
Daniel
+2  A: 

This has nothing to do with end() or "writability" of booleans.

empty() and isset() are not functions, they are language constructs.

From the docs for empty():

Note: empty() only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim($name)).

Again: only variables can be passed to empty().

Frank Farmer