4

Following code is very simple, but why PHP is allowing a function to be called with n number of arguments?

<?php

    function ab(){
        echo "yes";
    }

    ab(2);
?>

//output
yes

However following code giving Warning: Missing argument 2 for ab(), but still giving output

<?php
    function ab($a, $b){
        echo "yes";
    }

    ab(2);
?>

//output
yes

I know that this warning is ok but my question is about the first code.

Vijay Dohare
  • 731
  • 5
  • 22
  • Actually not @Chris because their they suggested how to achieve this but i wanna know why it is supported? is this not opposing the OOP concepts? – Vijay Dohare Apr 24 '18 at 06:04
  • I agree that this is a bit weird, but I don't think it has anything to do with "OOP concepts". – ChrisGPT was on strike Apr 24 '18 at 06:06
  • It's a 'useful' feature called variadic functions - http://php.net/manual/en/functions.arguments.php#functions.variable-arg-list and http://php.net/manual/en/migration56.new-features.php#migration56.new-features.variadics – Nigel Ren Apr 24 '18 at 06:13
  • 1
    note that even `error_reporting(E_STRICT);` does not report anything here, which is... questionable to say the least – eis Apr 24 '18 at 15:03

2 Answers2

1

I can give you the answer to may way and this is the simple way because no any person have still give the answer. (I appreciate if someone give the answer in better way better than me.)

Well, according to me as I understood by this link.

As per your question and this is the general thing we can see that you have defined two parameters and you are calling out with one parameter.

The Warning you are getting because this function is making you attention what you are doing wrong.

When you call this function, it will give you the runtime warning and very likely to cause errors.

As this is not the fatal errors so, the execution of the script is not halted.

And as the execution of the script is not halted, will give you the output with warning.

UPDATED:

As you questioned, will try to give my best for the answer.

The function (function ab($a, $b)) will initialize the variable but at the runtime will not check for the initiation.

The function count the number of parameters itself. When you call the function less than the number of parameter which you have defined in the function, will give you the warning. (Warning: Missing argument 2 for ab())

<?php
    function ab($a, $b){
        echo "yes";
    }

    ab(2);
?>

Now, if you pass the parameters more than the defined parameters, will not give you any warning because it satisfied with the (two) parameters.

<?php
    function ab($a, $b){
        echo "yes";
    }

    ab(1,2,3,4,5);
?>

Here, I can have only this answer as I tried my best. Hope you understood. Thanks.

Virb
  • 1,639
  • 1
  • 16
  • 25
  • Hey @Virb thanks for the answer, but my question is about the first code, why PHP is allowing a function to be called with n number of arguments? – Vijay Dohare Apr 24 '18 at 06:08
  • @VijayDohare: I have updated my answer. Hope it understands you. – Virb Apr 24 '18 at 06:22
1

Being able to pass a variable number of arguments is used in variadic functions, an example in PHP would be array_merge() which the manual defines as

array array_merge ( array $array1 [, array $... ] )

So you can have code which is...

$a4 = array_merge($a1, $a2);
$a5 = array_merge($a1, $a2, $a3);

Both which are perfectly valid and just merge as many arrays as you pass in.

In newer versions of PHP 5.6+, they added the ... operator so your definition could be more formally defined as

function ab(...$values){
    echo "yes";
}

This is from http://php.net/manual/en/functions.arguments.php#functions.variable-arg-list.

Prior to 5.6, you had to use func_get_args() to access these values (you still can).

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • 1
    I don't think this answers the question. Variable number of arguments is in use similarily in for example java, but you still wouldn't be able to do thing OP asked, it would be compile error – eis Apr 24 '18 at 15:01
  • 1
    @eis, I thought the question was 'why PHP is allowing a function to be called with n number of arguments?' So OP isn't asking to do anything particular but to understand why they are allowed to pass more parameters than declared in the declaration. – Nigel Ren Apr 24 '18 at 15:04
  • 1
    "to understand why they are allowed to pass more parameters than declared in the declaration"...but your examples *do* declare varargs in the declaration, so isn't that a different case? – eis Apr 24 '18 at 18:10
  • @eis is `( array $array1 [, array $... ] )` valid syntax? I added varargs in the second function (`function ab(...$values)`) as a way of showing how it can be done. – Nigel Ren Apr 24 '18 at 18:29
  • I don't think it still explains "why PHP is allowing a function to be called with n number of arguments" in the function OP provided in the question – eis Apr 24 '18 at 18:34