Creating Your Own PHP Functions

Creating Your Own PHP Functions

Creating Your Own PHP Functions

PHP has thousands of built-in functions and besides the built-in PHP functions, we can create our own functions easily. PHP Function is a block of statements that we can use them repeatedly in our code. PHP functions do not execute immediately when a page loads but a function can be executed only when you call the function.

How to Create a PHP Function

A PHP Function starts with word function and then name of the function. Name of the function CAN NOT start with a number.
function functionName()
{ // opening curly brace
code to be executed;
} // closing curly brace

Lets write a function that prints a name:
function familyName()
$name = "John";
echo $name;

We can call the function:

Arguments / Variables for the Functions

We can use variables to pass information to the functions.

For an example, the function above can be re-written by passing a value:
function familyName($name)
echo $name;
Now we can pass values out of the function.
We can also return values with functions.
Popup now! Make a Difference!