Core PHP

Functions with Default Arguments

This PHP code is an example program that demonstrates how to write functions with default arguments.

FunctionsWithDefaultArguments.php

<?php

  // This function has default argument values.
  // Default argument must always be the last variables.
  // The defaults are set right to left as arguments are omitted.
  function WithDefaultValues($qNoDefault, $qDefault1 = "<b>XoaX.net</b>",
         $qDefault2 = "<b>Video</b>", $qDefault3 = "<b>Lessons</b>") {
    echo "First Argument: ".$qNoDefault."<br />";
    echo "Second Argument: ".$qDefault1."<br />";
    echo "Third Argument: ".$qDefault2."<br />";
    echo "Fourth Argument: ".$qDefault3."<br />";
  }

  // Default arguments are displayed in bold.
  echo "First call with four arguments: <b>No Defaults used</b><hr />";
  WithDefaultValues("Trinity", "Father", "Son", "Holy Ghost");
  echo "<br />Second call with three arguments: <b>One Default used</b><hr />";
  WithDefaultValues("Bible", "Old Testament", "New Testament");
  echo "<br />Third call with two arguments: <b>Two Defaults used</b><hr />";
  WithDefaultValues("Prophet", "Moses");
  echo "<br />Fourth call with one argument: <b>Three Defaults used</b><hr />";
  WithDefaultValues("About");

?>
 

Output

 
 

© 2007–2024 XoaX.net LLC. All rights reserved.