Re: note 8545 added to Variable Functions
| From: | Lars Torben Wilson | Date: | Sun, 17 Sep 2000 01:00:33 +0000 |
| Subject: | Re: note 8545 added to Variable Functions | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-5108@lists.php.net to get a copy of this message | ||
These notes have been deleted from the manual. The manual errata
system is for submissions of text to be included in the manual itself,
not as a question-and-answer forum; please make sure to read the
instructions at http://www.php.net/manual/add-note.php before
adding
another note. You'll typically have better luck asking on the mailing
list, at php-general@lists.php.net (send an email to
php-general-subscribe@lists.php.net to subscribe, or go to
http://www.php.net/support.php for more information and
a signup form.
As to the questions, Cedric is quite correct that you can use array
notation in this way, but an easier way to get at the key/value pairs
is as follows:
while (list($key, $value) = each($ss)) {
echo "$key => $value\n";
}
...which would print out something like this:
1 => firstvalue
2 => secondvalue
. . . etc . . .
where firstvalue and secondvalue would be the values entered into the
$ss[1] and $ss[2] form fields, respectively.
Cedric, the 'root array' you are looking for is called '$GLOBALS',
which holds a reference to every variable which exists in the global
scope of the script. Note that this means that $GLOBALS also contains
a reference to itself, since it does exist in the global
scope. Therefore, be careful when recursively accessing $GLOBALS. :)
For a complete list of all server-related variables, you can use the
globally defined $HTTP_SERVER_VARS; however, it will not exist if
track_vars is not turned on. In versions of PHP >= 4.0.3, track_vars
is always turned on.
The following snippet:
while (list($key, $value) = each($HTTP_SERVER_VARS)) {
echo "$key => $value\n";
}
will print out something like the following:
DOCUMENT_ROOT => /home/torben/public_html/php3test
HTTP_ACCEPT => image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*
HTTP_ACCEPT_CHARSET => iso-8859-1,*,utf-8
HTTP_ACCEPT_ENCODING => gzip
HTTP_ACCEPT_LANGUAGE => en
. . . etc . . .
For more information on these variables, check out the following pages:
http://www.php.net/manual/language.variables.predefined.php
Hope this helps,
Torben
cedricshock@spamless.iname.com writes:
> Joe,
>
> By renaming your variables $ss[1], $ss[2], ... , $ss[n] you will recieve an array $ss.
>
> You can then loop through them like so:
> <?
> reset $ss; //Reset the variable so you can loop through it with each.
> while ($r = each($ss)) {
> $keyname=$r[0]; //This will be 1, 2, ..., n
> $value=$r[1]; //This will be its respective value.
> }
> ?>
>
> > I have a form submit n variables of the form
> > $ss_1, $ss_2, ... , $ss_n . I now want to write a
> > while loop that can go through and access > each of these variables sequentially,
> > but php
> > won't recognize $ss_$i as a variable or
> > $"ss_$i" as a variable where $i is the number of
> > times through the while loop. Does anyone
> > know how to get around this?
>
> This is still a problem when trying to access the complete HTTP
> headers, which get bundled up as HTTP_Header-Name. I don't know of
> any way to get a listing of all variables in memory. There should be
> a root array object which contains all the variables currently in
> memory.
>
> --Cedric Shock
>
> http://www.php.net/manual/ref.var.php
--
+----------------------------------------------------------------+
|Torben Wilson <torben@php.net> Netmill iTech|
|http://www.coastnet.com/~torben http://www.netmill.fi|
|Ph: 1 250 383-9735 torben@netmill.fi|
+----------------------------------------------------------------+