RE: [PHP] Passing an array as an argument.

From: Date: Tue, 06 Feb 2001 18:24:33 +0000
Subject: RE: [PHP] Passing an array as an argument.
Groups: php.general 
Request: Send a blank email to php-general+get-38409@lists.php.net to get a copy of this message
try: function process_members($asker_rank, $total_members, $email=array()) { global $database_mysql; mysql_select_db($database_mysql); while (list ($key, $val) = each ($email)) { echo "$key => $val<br>"; } } process_members($asker_rank, $total_members, $email); Your original function declaration only has two variables passed to it. You are passing three variables when you call the function. What's happening is this: function process_members($asker_rank, $email) { global $database_mysql; mysql_select_db($database_mysql); while (list ($key, $val) = each ($email)) { echo "$key => $val<br>"; } } process_members($asker_rank, $total_members, $email); step one: php sets $asker_rank (in function declaration) equal to $asker_rank step two: php sets $email (in function declaration) equal to $total_members step three: php ignores $email array passed to process_members() step four: php passes $email to each(). $email is set to the value of $total_members, which is not an array, and chokes. Using : function process_members($asker_rank, $total_members, $email=array()) { ... } allows you to have a default value for $email. So you can either pass it or not. -----Original Message----- From: April [mailto:april@farstrider.org] Sent: Tuesday, February 06, 2001 12:18 PM To: PHP General Subject: [PHP] Passing an array as an argument. How do you pass an array as an argument to a function? My code (or at least the important parts): function process_members($asker_rank, $email) { global $database_mysql; mysql_select_db($database_mysql); while (list ($key, $val) = each ($email)) { echo "$key => $val<br>"; } } ######### This will display fine. ######### while (list ($key, $val) = each ($email)) { echo "$key => $val<br>"; } ######### Doing the same thing with the function here returns this error, though. ######## // Warning: Variable passed to each() is not an array or object in lib.inc on line 447 process_members($asker_rank, $total_members, $email); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#38409) next »