PHP Function Create

In this chapter you will learn:

  1. How to create a PHP function
  2. Syntax to write your own functions
  3. Example - define a function

Description

We define functions with the function keyword, followed by the name of the function and two parentheses.

The actual code your function will execute lies between braces.

Syntax

Defining a function use the following syntax:


function myFunc() { 
   // (do stuff here) 
}   

Example

Define a function


<?PHP//from j a va 2 s . c om
         function hello() { 
            echo "Hello, world!\n"; 
         } 
         hello(); 
?>

The code above generates the following result.

As you can see, this script defines a function, hello() , that simply displays the string "Hello, world! "

The code within the hello() function is only run when the function is later called, not when the function itself is created.

Next chapter...

What you will learn in the next chapter:

  1. What is PHP Function Parameter
  2. Syntax for PHP Function Parameter
  3. Example - Add parameter to a function
Home » PHP Tutorial » PHP Function Create
PHP Function
PHP Function Create
PHP Function Parameter
PHP Function Default Parameters
PHP Variable Length Parameter
PHP Function Reference Parameter
PHP Function Return
PHP Return Reference
PHP Variable Scope in Functions
PHP Global Variables
PHP Recursive Functions
PHP Anonymous Function
PHP static variable