Tutorial 24: Custom Functions


While PHP has a massive library of built-in functions you have at your disposal, sometimes you need to build your own functions — for both small and large tasks.

Think of a custom functions as a mini-program that performs an action for you — better yet, think of a custom function as a cute little robot that does a specific task you tell it to do!

Let's look at the basic syntax of a function:

<?php

	function myCustomFunction() {
		
		// things my custom function will do
		// go in here
		
	}

?>

To build your custom function, you need to type function myCustomFunction(). Keep in mind that "myCustomFunction()" can be whatever words you want them to be, but no spaces allowed. Then add opening and closing curly brackets (or curly braces, as some call it). In between the curly brackets is where you define what your function will perform.

There are two important things to know about functions:

  1. There are functions that require "arguments", and;
  2. There are functions that don't require "arguments".

Let's start by building a custom function that does not require an argument.

<?php
	
	// Build the function
	function hangTen() {
		
		echo "Surf's up! Grab your board!";
		
	}
	
	// Call the function
	hangTen();
	
?>

Feel free to experiment with the above Sort Function with some PHP using the practice.php page provided in this folder.

Check out the final example
©2019 Brad Hussey