Connecting Tech Pros Worldwide Help | Site Map

implode notice

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 11:29 AM
Geoff Berrow
Guest
 
Posts: n/a
Default implode notice

I don't particularly need to do this but I've just found out that

print implode("<br>", $_SERVER);

Gives the warning :
Notice: Array to string conversion in c:\phpdev\www...

Doesn't do it with any other array and of course

foreach($_SERVER as $value){
print "$value<br>";
}

does exactly the same thing with no warning.

Curious, I am.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/

  #2  
Old July 17th, 2005, 11:29 AM
Janwillem Borleffs
Guest
 
Posts: n/a
Default Re: implode notice

Geoff Berrow wrote:[color=blue]
> I don't particularly need to do this but I've just found out that
>
> print implode("<br>", $_SERVER);
>
> Gives the warning :
> Notice: Array to string conversion in c:\phpdev\www...
>[/color]

That's because $_SERVER is an associative array and implode() only works
with indexed arrays.

Therefore the following will work:

print implode("<br>", array_values($_SERVER));


JW



  #3  
Old July 17th, 2005, 11:29 AM
Hartmut Holzgraefe
Guest
 
Posts: n/a
Default Re: implode notice

Janwillem Borleffs wrote:[color=blue]
> That's because $_SERVER is an associative array and implode() only works
> with indexed arrays.[/color]

This is *not* true, implode() doesn't care about the array type at
all, it just joins the array contents in its current sort order
without looking at the index values at all.

--
Hartmut Holzgraefe, Senior Support Engineer .
MySQL AB, www.mysql.com

Are you MySQL certified? www.mysql.com/certification
  #4  
Old July 17th, 2005, 11:29 AM
Hartmut Holzgraefe
Guest
 
Posts: n/a
Default Re: implode notice

Geoff Berrow wrote:[color=blue]
> I don't particularly need to do this but I've just found out that
>
> print implode("<br>", $_SERVER);
>
> Gives the warning :
> Notice: Array to string conversion in c:\phpdev\www...
>[/color]

$_SERVER["argv"] is also an array, that's what is causing the
warning
[color=blue]
> Doesn't do it with any other array and of course[/color]

You just have to construct the right example and it does:

<?php
error_reporting(E_ALL);
$a = array(1,2, array(3,4));
implode("-",$a);
?>
[color=blue]
> foreach($_SERVER as $value){
> print "$value<br>";
> }
>
> does exactly the same thing with no warning.[/color]

The "Array to String" conversion warning ist not thrown in all
situations where such a conversion happens

--
Hartmut Holzgraefe, Senior Support Engineer .
MySQL AB, www.mysql.com

Are you MySQL certified? www.mysql.com/certification
  #5  
Old July 17th, 2005, 11:29 AM
Janwillem Borleffs
Guest
 
Posts: n/a
Default Re: implode notice

Hartmut Holzgraefe wrote:[color=blue]
> This is *not* true, implode() doesn't care about the array type at
> all, it just joins the array contents in its current sort order
> without looking at the index values at all.[/color]

I stand corrected...


JW



  #6  
Old July 17th, 2005, 11:29 AM
Janwillem Borleffs
Guest
 
Posts: n/a
Default Re: implode notice

Hartmut Holzgraefe wrote:[color=blue]
> The "Array to String" conversion warning ist not thrown in all
> situations where such a conversion happens[/color]

Appears to happen only when explicit casting is performed:

// Does not throw a notice:
print array(1,2);

// Does throw a notice:
print (string) array(1,2);


JW



 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.