Skip to content
Home » Web » PHP » Right-Side Principle for Function

Right-Side Principle for Function

Using mixed function arguments should notice the right-side principles, which means you must put the default arguments to the rightmost position in the arguments list of the function declaration. Let's see an incorrect example first:

<?php
function testDefaultArguments($name = "Apple", $shape) {
  echo "Name: " . $name . "<br />";
  echo "Shape: " . $shape . "<br />";
}
testDefaultArguments("Round");
?>

This is incorrect. It will output a warning message:

Warning: Missing argument 2 for testDefaultArguments(), called in /var/www/html/test.php on line 6 and defined in /var/www/html/test.php on line 2
Name: Round

Notice: Undefined variable: shape in /var/www/html/test.php on line 4
Shape:

Right-Side for Default Arguments

As I said, you should put the default argument to the right side. Let's see the correction:

<?php
function testDefaultArguments($shape, $name = "Apple") {
  echo "Name: " . $name . "<br />";
  echo "Shape: " . $shape . "<br />";
}
testDefaultArguments("Round");
?>

This will output:

Name: Apple
Shape: Round

If the function has more than two arguments with mixed combination, the principle is the same: Put the default arguments to the rightmost position. In other words, put non-default arguments to the left-side.

This is wrong in declaration:

function testDefaultArguments($shape = "Round", $name = "Apple", $size) {
...

This is also wrong:

function testDefaultArguments($shape = "Round", $size, $name = "Apple") {
...

This is correct, pushing all the default function arguments to the right side:

function testDefaultArguments($size, $shape = "Round", $name = "Apple") {
...

Positional Notation

The next questions are: How to call the function with the mixed combination? How to arrange the values list? Basically, calling PHP functions literally follows positional notation. That is, you must indicate the values by their position, respect to the arguments. If the number of values are less than the number of arguments, PHP engine will assign them from left to right until all values are assigned.

Let's go further:

<?php
function testDefaultArguments($size, $shape = "Round", $name = "Apple") {
  echo "Name: " . $name . "<br />";
  echo "Shape: " . $shape . "<br />";
  echo "Size: " . $size . "<br />";
}
testDefaultArguments("Big", "Watermelon");
?>

This is syntactically correct, but it's wrong in logic, because we expect "Watermelon" to be assigned to $name, in fact, it was assigned to $shape.

Solutions

To correct above result, you have two ways to solve this:

1. Switch places of the 2nd and 3rd arguments in declaration.

As I said, PHP engine will assign values from left to right in the arguments list until all values are assigned. You can switch the places to correct the result.

function testDefaultArguments($size, $name = "Apple", $shape = "Round") {
...

2. For safety, list all the values when calling.

The most secure way is to list all the values that match all the arguments.

...
testDefaultArguments("Big", "Round", "Watermelon");
...

These can solve the problem of positional notation.

Leave a Reply

Your email address will not be published. Required fields are marked *