473,387 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

foreachif suggestion

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.
Jun 28 '08 #1
7 1266
Hugh Oxford 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.
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.
Jun 28 '08 #2
Greetings, Dikkie Dik.
In reply to Your message dated Saturday, June 28, 2008, 16:24:57,
>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.
$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.
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 <an*******@freemail.ru>

Jun 30 '08 #3
On 28 Jun, 12:40, Hugh Oxford <ares...@fas.comwrote:
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.
Jun 30 '08 #4
Hugh Oxford <ar*****@fas.comwrote:
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).
Jul 3 '08 #5
dae3 wrote:
Hugh Oxford <ar*****@fas.comwrote:
>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.
js*******@attglobal.net
==================

Jul 3 '08 #6
Jerry Stuckle <js*******@attglobal.netwrote:
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)
Jul 3 '08 #7
dae3 wrote:
Jerry Stuckle <js*******@attglobal.netwrote:
>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.
js*******@attglobal.net
==================

Jul 3 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: John Wellesz | last post by:
Hello, It would be great if there was an option to tell PHP to let the user manage all the HTTP headers instead of sending what it thinks is good for the programmer... For example when you...
7
by: J.Marsch | last post by:
I don't know whether this is the appropriate place to give product feedback, but here goes: I would love to see some kind of diagnostic to let me know when implicit boxing has occurred. We...
17
by: Jedrzej Miadowicz | last post by:
I recently (re)discovered data binding in Windows Forms thanks to its advances in Visual Studio 2005. As I looked a little deeper, however, I realize that it still suffers from an irksome tendency...
20
by: Allan Ebdrup | last post by:
I have a suggestion for C# I would like reader/writer locks to be built in to the language. When you want to aquire a loct on an object o you write lock(o) { ...//critical region } I would...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.