Re: (newbie) questions abot vars and args

From: Date: Fri, 26 Jul 2002 16:12:40 +0000
Subject: Re: (newbie) questions abot vars and args
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-109778@lists.php.net to get a copy of this message
on 27/07/02 1:40 AM, Alexander Ross (alexross@bleen.net) wrote: > 1) Given a variable how can I echo the var's name, not its value?? ummm provide an example... i can't think of a time when i know the name of a $var, but can't type it... when would you have a $var but not know about it? > 2) Is there a way for PHP function to handle a dynamic number of arguments? > In other words, I want to write a function that takes any number of > arguments (strings) and echos each of them with a <br> appended to each of > them. I'd be doing that with an array: <? $colors = array('red','blue','green','yellow'); foreach ( $colors as $color ) { echo "Do you like $color?<BR>\n"; } /* output: Do you like red? Do you like blue? Do you like green? Do you like yellow? */ ?> Or, to wrap it in a function, pass the array colors to a function: <? // untested function spitThemOut($array) { $output = ''; foreach ($array as $item) { $output .= "{$item}<BR>"; } return $output; } $colors = array('red','blue','green','yellow'); echo spitThemOut($colors); /* should echo red blue green yellow */ ?> The reason I'd start with an array is because you might want to sort it, add to it later, etc etc. Since you're a newbie, I can highly recommend looking at arrays... after a while, almost EVERYTHIGN seems easier with arrays. But to answer your question, yes, functions can take unlimited arguments... check out the 1st user-contributed note on this page: http://www.php.net/manual/en/functions.arguments.php Failing that, read the chapter on functions, because there are plenty of options. Justin French

« previous php.general (#109778) next »