Connecting Tech Pros Worldwide Help | Site Map

The $_POST array

  #1  
Old November 10th, 2005, 11:25 AM
Jørn Dahl-Stamnes
Guest
 
Posts: n/a
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/
  #2  
Old November 10th, 2005, 12:35 PM
IWT
Guest
 
Posts: n/a

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;

  #3  
Old November 10th, 2005, 02:15 PM
Jørn Dahl-Stamnes
Guest
 
Posts: n/a

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/
  #4  
Old November 10th, 2005, 02:25 PM
Ewoud Dronkert
Guest
 
Posts: n/a

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
  #5  
Old November 10th, 2005, 10:05 PM
Jørn Dahl-Stamnes
Guest
 
Posts: n/a

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/
  #6  
Old November 10th, 2005, 10:05 PM
Justin Koivisto
Guest
 
Posts: n/a

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
  #7  
Old November 10th, 2005, 10:15 PM
Ewoud Dronkert
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pass equivalent $_POST[] array via command line code green answers 6 June 4th, 2008 08:44 AM
$_POST array question William Gill answers 22 June 2nd, 2008 11:35 AM
Checkboxes, $_POST array, and switch statement Berko answers 1 January 11th, 2006 03:55 PM
how does PHP5 process POST data in creating $_POST array? billy.becker@gmail.com answers 14 July 17th, 2005 02:07 PM