...

View Full Version : Implode error



Crisp
01-29-2010, 08:11 AM
Hey,
I get this: Warning: implode() [function.implode (http://localhost/forum/function.implode)]: Invalid arguments passed
Once there is nothing in the array. (I use a while loop)
If there is something in the array, it will work.

Anyways, is there some way I can set a default value of...null?
All I want is it to be empty instead of saying the above when it's empty, and when there's something in there, it does it (Which works fine).

So what I'm asking is, how can I take away that error?
Code:

while($attach = mysql_fetch_array($attachments))
{
$attachment[] = htmlentities($attach['file']);
}
$attachment = implode(", ", $attachment);

and the function:

attachedFiles();

Thanks!

MattF
01-29-2010, 08:17 AM
So what I'm asking is, how can I take away that error?

Do proper checks.



if (!empty($attachment))
{
$attachment = implode(", ", $attachment);
}

kbluhm
01-29-2010, 01:30 PM
You can also just define $attachment as an array() before jumping into the while() loop. This ensures that $attachment will always exist as an array by the time you get to implode()... even when there is nothing to loop, which I believe is the case here. You will also avoid any "Undefined" notices that will undoubtedly be thrown.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum