0

I am playing with php default argument values and confused in passing more then one arguments.

I have created my own function just like bellow, which is example of php.net (Example #3)

function makecoffee($type = "cappuccino") {
    return "Making a cup of $type.\n";
}

echo makecoffee();
echo makecoffee(null);
echo makecoffee("espresso");

I have created this:

$table_fields = array('id', 'fname', 'lname', 'phone', 'message');
$table_name = 'Some Table';

function table($table_name, $table_fields= "*", $limit= 10) {

    echo 'Table Name '. $table_name;

    echo "<br />";

    echo 'Table Fields '. $com_table_fields = implode(", ", (array)$table_fields);  

}

table($table_name, $table_fields);

I have passed both arguments in function and I am getting this result:

Table Name Some Table
Table Fields id, fname, lname, phone, message

And If I will not pass $table_name in argument I will get this result with default which is perfect.

Table Name Some Table
Table Fields *

Now I have added one more argument with default(check bellow) and when I removed this argument $table_fields variable values changed which in not right.

$table_fields = array('id', 'fname', 'lname', 'phone', 'message');
$table_name = 'Some Table';
$limit = 5;

function table($table_name, $table_fields= "*", $limit= 10) {

    echo 'Table Name '. $table_name;

    echo "<br />";

    echo 'Table Fields '. $com_table_fields = implode(", ", (array)$table_fields);

    echo "<br />";

    echo 'Limit '. $limit;

}

table($table_name, $limit);

I want above result like this:

Table Name: Some Table
Table Fields: *
Limit: 10
Mr.Happy
  • 2,639
  • 9
  • 40
  • 73
  • I'm not seeing a difference between the second and third example or how the result changes. – deceze Apr 21 '14 at 08:22
  • You have not post output of third example ? What you expect & what you're getting ? – Rikesh Apr 21 '14 at 08:22
  • @deceze Sorry for that now I have updated my question. – Mr.Happy Apr 21 '14 at 08:23
  • You're not passing `$table_fields`...!? – deceze Apr 21 '14 at 08:24
  • Yes If I will not pass it will get default. `http://www.php.net/manual/en/functions.arguments.php` check Example 3. – Mr.Happy Apr 21 '14 at 08:26
  • You cannot NOT specify one parameter and specify the ones after that.. so you are forced to pass the fields parameters in your example – David Apr 21 '14 at 08:26
  • 1
    Quote from manual (http://www.php.net/manual/en/functions.arguments.php#functions.arguments.default): `Note that when using default arguments, any defaults should be on the right side of any non-default arguments; otherwise, things will not work as expected.` – hindmost Apr 21 '14 at 08:31
  • Okay I have implemented that way. My First one is `$table_name` which non-default arguments and `$table_fields, $limit` are with default arguments. Check this `table($table_name, $table_fields, $limit);` Is it right? – Mr.Happy Apr 21 '14 at 08:38
  • If you really want to leave the default values choices up to your function, the other solution is to pass null values to the parameters for which you want to default values (table($table_name, null, $limit);)and then apply default values to null parameters inside the function – David Apr 21 '14 at 09:04
  • @David `table($table_name, null, $limit)` I have tried your code and getting null. Can you please check my first implemented code and remove this `$table_fields` argument from function and check result. I want similar result in my third example:) I hope you understand my question. – Mr.Happy Apr 21 '14 at 09:09
  • See [this post](http://stackoverflow.com/questions/9166914/php-using-default-arguments-in-a-function?rq=1) for idea's similar to David. You have to rewrite the code though – Lepanto Apr 21 '14 at 09:13
  • Is that what you want? http://sandbox.onlinephpfunctions.com/code/ddab09dc0cc8da1e1bb9c00bbb7b8bbd90dd5e42 – David Apr 21 '14 at 09:55
  • What's kind of magic is this line of code supposed to do? `echo 'Table Fields '. $com_table_fields = implode(", ", (array)$table_fields);` – Sergiu Paraschiv Apr 22 '14 at 11:27
  • Also, default arguments do not mean optional arguments _in any order_. – Sergiu Paraschiv Apr 22 '14 at 11:28

2 Answers2

0

Function arguments are passed by their position, i.e. the order in which they are passed. The second function argument will always be the second function argument.

function foo($arg1, $arg2 = null, $arg3 = 42) { ... }
               ^      ^             ^
               |      |             |
               v      v             v
         foo($bar,  $baz,         $quorx);

To "skip" arguments, even arguments with a default value, you need to pass some value in its place. Typically you're going to pass the default value:

function table($table_name, $table_fields = "*", $limit = 10) { ... }
table($table_name, "*", $limit);
deceze
  • 510,633
  • 85
  • 743
  • 889
-1
$table_fields = array('id', 'fname', 'lname', 'phone', 'message');
$table_name = 'Some Table';
$limit = 5;

function table($table_name, $limit= 10, $table_fields= "*") {

    echo 'Table Name '. $table_name;

    echo "<br />";

    echo 'Table Fields '. $table_fields;

    echo "<br />";

    echo 'Limit '. $limit;

}

table($table_name, $limit);

Output is:

Table Name Some Table
Table Fields *
Limit 5

in your code, $com_table_fields = implode(", ", (array)$table_fields); is confusion

TechStone
  • 141
  • 6