|
Posted by Steve on 01/25/08 22:30
"Giovanni" <blastingproton@gmail.com> wrote in message
news:bfe96afe-98dc-4850-98a8-890b0de68daf@j20g2000hsi.googlegroups.com...
> Hi! I've an array of string like this:
>
> $arraystring["name"]="John";
> $arraystring["age"]="50";
> $arraystring["work"]="teacher";
>
> $arraystring is global variable.
>
> I'm trying to code a function that get a string as parameter and print
> the relative arraystring's value.
>
> function printArrayStr($key)
> {
> global $arraystring;
> echo $arraystring["$key"];
> }
>
> What do I wrong?! :(
well, for one, you don't need a function for that. last time i checked,
this:
echo $arraystring['name'];
took less effort than:
printArrayStr('name');
second, functions should be *generic* !!! if your function only works on
this one variable, what good is it? if would be better to pass the array and
the key to the function...that way, the function's importance remains with
its ability to 'print' an array value. ex., what good would php's print_r()
function be if it only worked on one data type, or if it relied on a global
variable. get it?
finally, you don't need quotes around $key in your function's echo
statement.
as for debugging your specific code...here's the answer. delete everything
starting with 'f' in 'function', down to and including the ending function
bracket. then use this:
echo $arraystring['name']; // or whatever key you want to echo from the
array.
Navigation:
[Reply to this message]
|