PHP: Writing a Package

By Xah Lee. Date:

A collection of functions can be written and saved into a file. This file can then be loaded using require().

For example, save the following in a file and name it “myPackage.php”.

<?php
function ff($x) {
  return($x+1);
}
?>

Now, eval the following code:

<?php
require("myPackage.php");
echo ff(3); # prints 4
?>

“require()” works as if the whole file's content is inserted in-place.

As of PHP 5.2.4 (2007-08), there is no namespace mechanism for importing functions or packages.

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