Re: foreachif suggestion



On 28 Jun, 12:40, Hugh Oxford <ares...@xxxxxxx> wrote:
Can I suggest a new PHP native function, "foreachif".

At the moment I am trying to make my code run cleanly even if warnings
are switched on. If I do a foreach on a non-existent array, I get a warning.

So I have to do

if($array) foreach($array as $foo){};

foreachif would only run if the array was not empty.

If the array is in fact non-existent, then if($array) will fail unless
$array is an initialised variable of a different type.
.



Relevant Pages

  • Re: [PHP] foreach() using current() strange beahvior
    ... that's expected as foreach moves the internal array pointer, ... When using the internal pointer just by calling current(so not moving ... Unless the array is referenced, foreach operates on a copy of the ...
    (php.general)
  • Re: format and newline
    ... use warnings; ... foreach (@array) { ...
    (comp.lang.perl.misc)
  • Re: a question about for and foreach
    ... foreach (@array) { ... why @array are changed after this foreach loop!!! ... The "foreach" loop iterates over a normal list value and sets the ... If any element of LIST is an lvalue, you can modify it by modifying ...
    (perl.beginners)
  • Re: Parsing pipe delimited file
    ... # cannot remember syntax for "use warnings but no 'uninitialized' ... print "$_ " foreach @data_src; ... sub parse_song_artist { ... Given an @array such as ...
    (perl.beginners)
  • Re: safe to delete elements of array in foreach
    ... I agree with Jon on this one. ... I make it a habit not to delete entries in a foreach() loop. ... build an array of keys I want to delete, and after the loop ends, delete ... I don't know whether an operation like this is guaranteed to work in PHP ...
    (comp.lang.php)