Connecting Tech Pros Worldwide Forums | Help | Site Map

foreachif suggestion

Hugh Oxford
Guest
 
Posts: n/a
#1: Jun 28 '08
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.

Dikkie Dik
Guest
 
Posts: n/a
#2: Jun 28 '08

re: foreachif suggestion


Hugh Oxford wrote:
Quote:
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.
You can off course suggest such a function. However, the site of PHP
would probably be a better place.

Apart from that, your workaround tell something really funny: that the
variable $array is a boolean, and if true, it should be iterated upon as
if it were an array.

I KNOW about type juggling, but this is what your code tells me. If you
want to check for an empty array, it would be much clearer to check for
a count being more than zero, or even write a function IsArrayFilled to
check if it is an array at all.

And, off course, there is no problem iterating over an empty array.
There will just be no iteration. Why put an if statement in front of it?
Is this a needless optimization? I think such an optimization is not
necessary, and if it is, it should not surprise me if it was alreadu
built in in the foreach statement.
AnrDaemon
Guest
 
Posts: n/a
#3: Jun 30 '08

re: foreachif suggestion


Greetings, Dikkie Dik.
In reply to Your message dated Saturday, June 28, 2008, 16:24:57,
Quote:
Quote:
>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.
Quote:
You can off course suggest such a function. However, the site of PHP
would probably be a better place.
Quote:
Apart from that, your workaround tell something really funny: that the
variable $array is a boolean, and if true, it should be iterated upon as
if it were an array.
Quote:
I KNOW about type juggling, but this is what your code tells me. If you
want to check for an empty array, it would be much clearer to check for
a count being more than zero, or even write a function IsArrayFilled to
check if it is an array at all.
$var = 1;
echo count($var);

Read documentation :) It is clearly explained in the desctiption of count()
function.
To make sure you are operating with array, use straightforward is_array() type
check. If you want to check if it has any elements, you may combine it with
count(), but it is often a waste of mind.

I.e.

function doSmth($var)
{
$result = '';
if(is_array($var) && count($var))
{
foreach($var as $key =$value)
{
$result .= "[{$key}] ='{$value}'\n";
}
}
return $result;
}

and

function doSmth($var)
{
$result = '';
if(is_array($var))
{
foreach($var as $key =$value)
{
$result .= "[{$key}] ='{$value}'\n";
}
}
return $result;
}

will have the same result when $var = array();, but second code much clearer
to my taste.
Quote:
And, off course, there is no problem iterating over an empty array.
There will just be no iteration. Why put an if statement in front of it?
Is this a needless optimization? I think such an optimization is not
necessary, and if it is, it should not surprise me if it was alreadu
built in in the foreach statement.

--
Sincerely Yours, AnrDaemon <anrdaemon@freemail.ru>

Captain Paralytic
Guest
 
Posts: n/a
#4: Jun 30 '08

re: foreachif suggestion


On 28 Jun, 12:40, Hugh Oxford <ares...@fas.comwrote:
Quote:
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.
dae3
Guest
 
Posts: n/a
#5: Jul 3 '08

re: foreachif suggestion


Hugh Oxford <arestes@fas.comwrote:
Quote:
if($array) foreach($array as $foo){};

The correct way to test for $array would be "if (isset($array))",
however. Not such a big deal that it needs its own construct, IMO.


--
I am dae3 and I approve my own message.
--
History is on our side (as long as we can control the historians).
Jerry Stuckle
Guest
 
Posts: n/a
#6: Jul 3 '08

re: foreachif suggestion


dae3 wrote:
Quote:
Hugh Oxford <arestes@fas.comwrote:
>
Quote:
>if($array) foreach($array as $foo){};
>
>
The correct way to test for $array would be "if (isset($array))",
however. Not such a big deal that it needs its own construct, IMO.
>
>
Incorrect.

$array=5;
isset($array) is true.
is_array($array) is false.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

dae3
Guest
 
Posts: n/a
#7: Jul 3 '08

re: foreachif suggestion


Jerry Stuckle <jstucklex@attglobal.netwrote:
Quote:
Incorrect.
>
$array=5;
isset($array) is true.
is_array($array) is false.

Well, of course. I assume $array to be an... array.


--
I am dae3 and I approve my own message.
---------------------------------------
What if everything is an illusion and nothing exists? In that case, I
definitely overpaid for my carpet. (Woody Allen)
Jerry Stuckle
Guest
 
Posts: n/a
#8: Jul 3 '08

re: foreachif suggestion


dae3 wrote:
Quote:
Jerry Stuckle <jstucklex@attglobal.netwrote:
>
Quote:
>Incorrect.
>>
>$array=5;
>isset($array) is true.
>is_array($array) is false.
>
>
Well, of course. I assume $array to be an... array.
>
>
Don't *assume* anything :-)

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Closed Thread