senglor Posted June 28, 2011 Share Posted June 28, 2011 I've gotten it to output "david" but can't figure out how to get it to 55 and "john". i believe I need an loop here but can't figure it out. Any help or suggestions would be appreciated. <?php 2 function foo() 3 { 4 $numargs = func_num_args(); 5 echo "Number of arguments: $numargs<br />\n"; 6 if ($numargs >= 2) 7 { 8 echo "First argument is: " . func_get_arg(0) . "<br />\n"; 9 } 10 else 11 { 12 echo "Second argument is: " . func_get_arg(1) . "<br />\n"; 13 14 } 15 } 16 17 18 foo ("david", 55, "john"); 19 ?> Output so far: Number of arguments: 3 First argument is: david Link to comment https://forums.phpfreaks.com/topic/240653-how-do-i-get-it-to-ouput-the-next-2-arguments/ Share on other sites More sharing options...
TeNDoLLA Posted June 28, 2011 Share Posted June 28, 2011 function foo() { $numargs = func_num_args(); echo "Number of arguments: $numargs<br />\n"; if ($numargs >= 2) // 3 arguments is more than 2 so it goes inside this if { echo "1st argument is: " . func_get_arg(0) . "<br />\n"; echo "2nd argument is: " . func_get_arg(1) . "<br />\n"; echo "3rd argument is: " . func_get_arg(2) . "<br />\n"; } else { echo "Second argument is: " . func_get_arg(1) . "<br />\n"; } } foo ("david", 55, "john"); // You pass 3 arguments. Link to comment https://forums.phpfreaks.com/topic/240653-how-do-i-get-it-to-ouput-the-next-2-arguments/#findComment-1236037 Share on other sites More sharing options...
Zane Posted June 28, 2011 Share Posted June 28, 2011 that else should echo func_get_arg(0) not 1. Link to comment https://forums.phpfreaks.com/topic/240653-how-do-i-get-it-to-ouput-the-next-2-arguments/#findComment-1236040 Share on other sites More sharing options...
TeNDoLLA Posted June 28, 2011 Share Posted June 28, 2011 Depends what he wants =) But just answered to the question "hod do i get it to output the other 2 missing arguments" Link to comment https://forums.phpfreaks.com/topic/240653-how-do-i-get-it-to-ouput-the-next-2-arguments/#findComment-1236042 Share on other sites More sharing options...
AbraCadaver Posted June 28, 2011 Share Posted June 28, 2011 function foo() { $numargs = func_num_args(); echo "Number of arguments: $numargs<br />\n"; switch($numargs) { case '3': echo "3rd argument is: " . func_get_arg(2) . "<br />\n"; case '2': echo "2nd argument is: " . func_get_arg(1) . "<br />\n"; case '1': echo "1st argument is: " . func_get_arg(0) . "<br />\n"; break; default: echo "No arguments passed!<br />\n"; } } or function foo() { foreach(func_get_args() as $key => $val) { $num = $key + 1; echo "Argument $num is $val<br />\n"; } } Link to comment https://forums.phpfreaks.com/topic/240653-how-do-i-get-it-to-ouput-the-next-2-arguments/#findComment-1236053 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.