Connecting Tech Pros Worldwide Help | Site Map

implode notice

Geoff Berrow
Guest
 
Posts: n/a
#1: Jul 17 '05
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/
Janwillem Borleffs
Guest
 
Posts: n/a
#2: Jul 17 '05

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



Hartmut Holzgraefe
Guest
 
Posts: n/a
#3: Jul 17 '05

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
Hartmut Holzgraefe
Guest
 
Posts: n/a
#4: Jul 17 '05

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
Janwillem Borleffs
Guest
 
Posts: n/a
#5: Jul 17 '05

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



Janwillem Borleffs
Guest
 
Posts: n/a
#6: Jul 17 '05

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



Closed Thread