too long

What is the difference between these two code excerpts, respectively, in PHP?:

function eat_apple($apple)
{
    return true;
}

And:

function eat_apple()
{
    $apple = func_get_arg(0);
    return true;
}

Or is there a difference? If there is not, what is the point of func_get_arg()? Surely it serves some purpose I'm not aware of?

func_get_arg (and func_get_args) make it possible to have functions with a variable number of parameters, so called variadic functions.

Take array_merge as an example, which takes a variable number of arrays to be merged.

The other possible reason I could think of is avoiding a fatal error.

You have a function:

<?php
function display ($a, $b, $c) {
    echo $a . " is " . $b . " " . $c;
}
?>

and you could call

<?php
display ("Rock", "good", "boy");
- this will not throw any error.

display ("Rock", "good");
- this will throw fatal error.
?>

So, now think of the second call, you could avoid the fatal error, see how many arguments are passed with func_get_args and process your code with func_get_arg(1) etc.

Several reasons:

  1. You can use that magic to emulate overloading
  2. You can write function (like printf) which can take undetermined number of arguments.

Example for first:

class a{
  function X(MyOBJ $obj){...}
}

class b extends a{
  function X(string $s,int $i){...}
}
class c extends b{
  function X(){...}
}

This will throw a warning. If you use func_get_args() inside the function to get relevant parameters, no warning will be thrown.

Example for the second:

//not good, you can only send 3 strings
function merge_strings($s1, $s2, $s3){
     return $s1 . $s2 . $s3;
}

//good one
function merge_strings(){
   return join('',func_get_args());
}

In truth, there isn't much difference, and a standard passed argument is usually preferable over func_get_arg().

There are two occasions when you might want to use func_get_arg() rather than using defined arguments:

  1. If you want to have an unlimited number of arguments. Typically, this would be better done with a single array argument, but there are times when you may want to allow unlimited arguments. A real example of a function that works this way is PHP's built-in printf function.

  2. Where you have a complex function where the number of arguments given may alter what the arguments are used for and/or their data type. For example, passing a single argument may cause the function to expect a string, whereas passing two arguments, it may expect an integer first, and then the string. I can't think of any examples of this off the top of my head, but I'm fairly sure there are some. Ordinarily, I would say that this sort of behaviour is bad practice, but I can see how it may be useful for adding features while maintaining backward compatibility.

There are two things that you will definitely lose if you use fung_get_arg() instead of standard function arguments:

  1. The ability to pass by reference.

  2. The ability for your IDE to do any type hinting or auto-completion.

Hello. What is the difference between these two code excerpts, respectively, in PHP?:

-function eat_apple($apple)
+function eat_apple()
 {
+    $apple = func_get_arg(0);
     return true;
 }

what is the point of func_get_arg()? Surely it serves some purpose I'm not aware of?

The officially stated purpose is to:

Return an item from the argument list

-- http://php.net/func-get-arg