View Full Version : Implode error

01-29-2010, 07:11 AM
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?

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

and the function:



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

Do proper checks.

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

01-29-2010, 12: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.