2

Following a Tutorial I am struggling with an issue in a PHP function. I have some basic background on C# and Java and according to my knowledge this code shouldn't work since I am not passing any parameter in the add() function, but , surprisingly!, it works!
According to PHP Manual the func_num_args() Gets the number of arguments passed to the function.so how we can echo the result of the add() function while we are not passing any parameter in the function?! Also, if the function is for getting the number of arguments how we can use it to calculate the numbers?!

<?php
 function add(){
  $args  = func_num_args();
  $sum  = 0; 
  $i    = 0;
  for($i; $i< $args; $i++ ){
   is_int(func_num_args($i)) ? $sum+= func_num_args($i) : die('Use Only Numbers');
 }
}

echo add(2,5,10,12);
?>

Thanks for your comments

Ian Atkin
  • 6,302
  • 2
  • 17
  • 24
Mona Coder
  • 6,212
  • 18
  • 66
  • 128

3 Answers3

3

Use func_get_args():

function add(){
    if(!func_num_args())return 0;

    $args = func_get_args();
    $sum  = 0;

    foreach($args as $arg){
        if(is_int($arg)){
            $sum += $arg;
        } else {
            die('Use Only Numbers');
        }
    }

    return $sum;
}

As I mentioned in comments for "no args" case:

func_num_args()s return value is 0. for-loop in your code will not work as of $i < $args simplifies to 0 < 0, which is false.

To prevet that, you may try to use:

if(!func_num_args()){
    die('There are no args!');
}

Your line echo add(); will work anyway, because:

PHP has support for variable-length argument lists in user-defined functions. This is really quite easy, using the func_num_args(), func_get_arg(), and func_get_args() functions.

No special syntax is required, and argument lists may still be explicitly provided with function definitions and will behave as normal.

BlitZ
  • 12,038
  • 3
  • 49
  • 68
  • or alternatively `func_num_args` and `func_get_arg` – Orangepill May 29 '13 at 04:55
  • Hi Corrupt, I think you didn't get my question! I am asking why the second part of code : echo add(2,5,10,12); is working while I didn't pass any argument through add() functin? – Mona Coder May 29 '13 at 04:56
  • @MonaCoder `$i < $args` in `for`-loop condition section is `false` (in "no args" case). Cycle ignored. – BlitZ May 29 '13 at 04:57
  • @CORRUPT thanks for correction but can you please focous on my question for now? – Mona Coder May 29 '13 at 04:58
  • @Mona You *are passing arguments* into the function. Right there: `add(2, 5, 10, 12)`. That's one function call with 4 arguments passed. The rest of your original code should indeed not work though, as you're misusing `func_num_args`. – deceze May 29 '13 at 05:38
1

I think you are confused because you know what function overloading is, but php does not support function overloading in this manner.

Please go through this link. It will really help you out of your confusion.

php function overloading

Community
  • 1
  • 1
mjdevloper
  • 1,553
  • 8
  • 33
  • 69
1

Use func_get_args()

func_num_args()s return value is 0. for-loop in your code will not work as of $i < $args simplifies to 0 < 0, which is false.

To prevet that, you may try to use:

if(!func_num_args()){
    die('There are no args!');
}
Thomas Tkr
  • 49
  • 1
  • 9