Connecting Tech Pros Worldwide Forums | Help | Site Map

The $_POST array

Jørn Dahl-Stamnes
Guest
 
Posts: n/a
#1: Nov 10 '05
I got the following code:

reset ($_POST);
while (list($key,$val) = each($_POST))
{
echo "$key => $val\n";
}

But this prints out nothing. If I replace the code above with:


print_r ($_POST);

I can see the content of the $_POST array. Why?

--
Jørn Dahl-Stamnes
http://www.dahl-stamnes.net/dahls/

IWT
Guest
 
Posts: n/a
#2: Nov 10 '05

re: The $_POST array



Jørn Dahl-Stamnes wrote:[color=blue]
> I got the following code:
>
> reset ($_POST);
> while (list($key,$val) = each($_POST))
> {
> echo "$key => $val\n";
> }
>
> But this prints out nothing. If I replace the code above with:
>
>
> print_r ($_POST);
>
> I can see the content of the $_POST array. Why?[/color]

Try this:

foreach($_POST as $key => $value)
echo $key."=".$value;

Jørn Dahl-Stamnes
Guest
 
Posts: n/a
#3: Nov 10 '05

re: The $_POST array


IWT wrote:
[color=blue]
>
> Jørn Dahl-Stamnes wrote:[color=green]
>> I got the following code:
>>
>> reset ($_POST);
>> while (list($key,$val) = each($_POST))
>> {
>> echo "$key => $val\n";
>> }
>>
>> But this prints out nothing. If I replace the code above with:
>>
>>
>> print_r ($_POST);
>>
>> I can see the content of the $_POST array. Why?[/color]
>
> Try this:
>
> foreach($_POST as $key => $value)
> echo $key."=".$value;[/color]

Tried that too. Still no output...

--
Jørn Dahl-Stamnes
http://www.dahl-stamnes.net/dahls/
Ewoud Dronkert
Guest
 
Posts: n/a
#4: Nov 10 '05

re: The $_POST array


Jørn Dahl-Stamnes wrote:[color=blue]
> I got the following code:
>
> reset ($_POST);
> while (list($key,$val) = each($_POST))
> {
> echo "$key => $val\n";
> }
>
> But this prints out nothing. If I replace the code above with:
>
>
> print_r ($_POST);
>
> I can see the content of the $_POST array. Why?[/color]

Can't think of anything but:
- it's a bug in your php version...,
- echo is buffered and/or redirected and print_r is not,
- "list" or "each" are redefined as constants earlier,
- the contents of $key or $val are preventing output.

If the last point, try using htmlspecialchars().

--
E. Dronkert
Jørn Dahl-Stamnes
Guest
 
Posts: n/a
#5: Nov 10 '05

re: The $_POST array


Ewoud Dronkert wrote:
[color=blue]
> Jørn Dahl-Stamnes wrote:[color=green]
>> I got the following code:
>>
>> reset ($_POST);
>> while (list($key,$val) = each($_POST))
>> {
>> echo "$key => $val\n";
>> }
>>
>> But this prints out nothing. If I replace the code above with:
>>
>>
>> print_r ($_POST);
>>
>> I can see the content of the $_POST array. Why?[/color]
>
> Can't think of anything but:
> - it's a bug in your php version...,
> - echo is buffered and/or redirected and print_r is not,
> - "list" or "each" are redefined as constants earlier,
> - the contents of $key or $val are preventing output.[/color]

Seems like a bug to me. I tried this:

$text = print_r ($_POST,true);
echo $text;

and it worked OK. So I'm able to get around this possible bug.

--
Jørn Dahl-Stamnes
http://www.dahl-stamnes.net/dahls/
Justin Koivisto
Guest
 
Posts: n/a
#6: Nov 10 '05

re: The $_POST array


Jørn Dahl-Stamnes wrote:[color=blue]
> I got the following code:
>
> reset ($_POST);
> while (list($key,$val) = each($_POST))
> {
> echo "$key => $val\n";
> }
>
> But this prints out nothing. If I replace the code above with:
>
>
> print_r ($_POST);
>
> I can see the content of the $_POST array. Why?
>[/color]

Try this:

$ar=$_POST;
while (list($key,$val) = each($ar))
{
echo "$key => $val\n";
}

IIRC, older PHP versions had a problem with using the each function (and
the foreach) with any of the superglobal arrays...

--
Justin Koivisto, ZCE - justin@koivi.com
http://koivi.com
Ewoud Dronkert
Guest
 
Posts: n/a
#7: Nov 10 '05

re: The $_POST array


Justin Koivisto wrote:[color=blue]
> IIRC, older PHP versions had a problem with using the each function (and
> the foreach) with any of the superglobal arrays...[/color]

I thought that might be the case, but couldn't find anything with some
quick searches earlier (googled "php list each superglobal" and looked at
the comments at php.net/each).

--
E. Dronkert
Closed Thread