Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

PHP Functions

PHP Built-in Functions

PHP includes over 1,000 built-in functions that can be called directly from a script to execute specific tasks.

PHP User Defined Functions

In addition to built-in PHP functions, you can also create your own functions.

  • A function is a block of statements that can be reused throughout a program.
  • It does not execute automatically when a page loads.
  • It is executed by calling the function.

Create a Function

A user-defined function declaration begins with the keyword function, followed by the function’s name:

Example

function myMessage() {
 echo "Hello world!";
}
Note: A function name must begin with a letter or an underscore. Function names are NOT case-sensitive.

Tip: Choose a function name that clearly indicates its purpose!

Call a Function

To invoke the function, simply write its name followed by parentheses ():

Example

function myMessage() {
 echo "Hello world!";
}

myMessage
();

In our example, we define a function called myMessage().

The opening curly brace { marks the start of the function code, while the closing curly brace  signifies the end of the function.

The function prints “Hello world!”.