Use global. Read the docs about variable scoping.
If you use php >=4.1.0 then use $_POST, $_GET,... they not need to use global. Global 
scope.
<?
function show($name)
{ global $HTTP_POST_VARS;
  $cnt = count($HTTP_POST_VARS[$name]);
  echo "Count for $name is $cnt";
  echo "<br />";
}
show("countries");
show("cities");
?>

Regards,
Andrey Hristov

----- Original Message ----- 
From: <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Saturday, February 23, 2002 7:31 PM
Subject: [PHP] Associative array key as a variable


> Hi.
> 
> I have the following PHP program executed from an HTML form where I pass
> the associative array key name as a variable:
> 
> <?
> function show($name)
> {
>   $cnt = count($HTTP_POST_VARS[$name]);
>   echo "Count for $name is $cnt";
>   echo "<br />";
> }
> show("countries");
> show("cities");
> ?>
> 
> The result is always 0. The form fields do have name=countries[] and
> name=cities[] to indicate an array.
> When I hardcode the count line in the function as follows just to test
> to make sure the transfer from the browser works, it displays a correct
> non-zero result:
> 
>   $cnt = count($HTTP_POST_VARS["cities"]);
> 
> I even tried these statements in the function but all give a 0 result:
> 
>   $cnt = count($HTTP_POST_VARS["".$name.""]);
> 
> How do I get the count line to work with a variable in the function?
> Thanks,
> 
> Eurico
> 
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
> 


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

Reply via email to