Jump to content

how do i get it to ouput the next 2 arguments?


senglor

Recommended Posts

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

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.

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";
   }
}

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.