Default arguments in PHP functions

This tutorial explains how to specify a default argument in a function, if an argument is expected but has not been provided to the function. Any provided argument will override the default argument, but the default argument will prevent problems from occurring if no argument is given when it is expected.

Providing default argument cam prevent errors from occurring if no argument is provided to a function when the function is called in the program. An argument may not be provided to a function, for example, if a user does not provide some information in a form or from some other method of user input.

Watch the video below and then scroll down to see the sample code.

Sample PHP code:

<?php
  function message($greeting, $name = "friend"){
    return "$greeting $name";
  }

  echo message("Hello there, " , "Batman");

  // Default arguments are used when no argument is given to the function.
  // In this example friend will be the default argument if no argument is given for $name.
  // However, if a second argument is provided (eg. "Batman"), then that
  // will override the default argument.
?>