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