On Wednesday 01 January 2003 01:41 pm, Timothy Hitchens (HiTCHO) wrote:
> Use the count like following inside to ensure that you don't
> call on a non existent index.
>
> for ($i = 0, $x = count($comment); $i < $x; $i++)
> {
>     echo $comment[$i].'<br />';
> }
>
> Why do you want to echo out $comment_1 ???

i think he wanted to print the value of $comment_1.  he had variables as
$comment_# where # is in (1...n, n==20 in his example).  the $comment_#s
are probably fields in an HTML form and as has been discussed many
times on this list, using array variables directly is better.

Philip, in your HTML, instead of saying name=comment_1, you would
instead say name=comment[1].

that way, you can access the variables in your PHP script as
$comment[1] (depending on register_globals and other settings).

alternatively, if you don't want to change to array syntax, you can use
variable variables.  e.g.,

$variable_name="comment_".nIndex;       // now comment_1, for example.
   // assume that $comment_1 is "xxx111"
echo $$variable_name ;   // prints xxx111

what you did was, form the variable name as a string, then
take the string and, using the variable variable syntax, ask PHP
what the value was of the string which was contained in the
variable "variable_name".

this is a neat feature and helpful in many situations, but for readability
i'd go with naming the fields as array entries "comment[1]".

tiger

-- 
Gerald Timothy Quimpo  tiger*quimpo*org gquimpo*sni-inc.com tiger*sni*ph
Public Key: "gpg --keyserver pgp.mit.edu --recv-keys 672F4C78"
                   Veritas liberabit vos.
                   Doveryai no proveryai.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to