PHP: Defining A Function

By Xah Lee. Date:

A function is defined using keyword “function”. Like this:

<?php
/* ff($x,$y) returns x + y */
function ff($x,$y) {
  return($x+$y);
}

echo ff(3,4); // prints 7
?>

Optional Parameters

<?php
/* ff($x,$y) returns x + y. If $y is not given, default to 1. */
function ff($x,$y=1) {
  return($x+$y);
}

echo ff(3); // prints 4
?>