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
?>

If you have a question, put $5 at patreon and message me.

  1. PHP Install
  2. PHP Basics
  3. String Syntax
  4. String Operations
  5. Array
  6. Hash Table
  7. Looping Thru Array
  8. Function
  9. Write Package
  10. Send HTML Mail
  11. Send Mail with Attachment
  12. MySQL Database Query