PHP Functions


The real power of PHP comes from its functions.

In PHP, there are more than 1000 built-in functions.


PHP Built-in Functions

For the complete reference manual and examples of all array functions, please visit ourPHP Reference Manual。


PHP Functions

In this chapter, we will show you how to create your own functions.

To execute a script when the page loads, you can put it in a function.

Functions are executed by calling the function.

You can call a function from anywhere in the page.


Creating PHP Functions

Functions are executed by calling the function.

Syntax

<?php function functionName() { //Code to be executed } ?>

PHP function guidelines:

  • The function name should indicate what it does
  • Function names start with a letter or underscore (not with a number)

Example

A simple function that outputs my name when called:

Example

<?php function writeName() { echo "Kai Jim Refsnes"; } echo "My name is "; writeName(); ?>

Output:

My name is Kai Jim Refsnes


PHP Functions - Adding Parameters

To add more functionality to a function, we can add parameters. Parameters are like variables.

Parameters are specified inside the parentheses after the function name.

Example 1

The following example outputs different first names, but the same last name:

Example

<?php function writeName($fname) { echo $fname . " Refsnes.<br>"; } echo "My name is "; writeName("Kai Jim"); echo "My sister's name is "; writeName("Hege"); echo "My brother's name is "; writeName("Stale"); ?>

Output:

My name is Kai Jim Refsnes.
My sister's name is Hege Refsnes.
My brother's name is Stale Refsnes.

Example 2

The following function has two parameters:

Example

<?php function writeName($fname,$punctuation) { echo $fname . " Refsnes" . $punctuation . "<br>"; } echo "My name is "; writeName("Kai Jim","."); echo "My sister's name is "; writeName("Hege","!"); echo "My brother's name is "; writeName("Ståle","?"); ?>

Output:

My name is Kai Jim Refsnes.
My sister's name is Hege Refsnes!
My brother's name is Ståle Refsnes?

PHP Functions - Return Values

To let a function return a value, use the return statement.

Example

<?php function add($x,$y) { $total=$x+$y; return $total; } echo "1 + 16 = " . add(1,16); ?>

Output:

1 + 16 = 17

PHP Variable Functions

Variable functions are functions in PHP where a variable is used as the function name to call the function.

Variable functions allow us to dynamically decide which function to call at runtime.

Example

<?php
function foo() {
    echo "In foo()<br />\n";
}

function bar($arg = '')
{
    echo "In bar(); argument was '$arg'.<br />\n";
}

// Wrapper function using echo
function echoit($string)
{
    echo $string;
}

$func = 'foo';
$func();        // Call foo()

$func = 'bar';
$func('test');  // Call bar()

$func = 'echoit';
$func('test');  // Call echoit()
?>

You can also use the variable function syntax to call an object's method.

Example

<?php
class Foo
{
    function Variable()
    {
        $name = 'Bar';
        $this->$name(); // Call the Bar() method
    }

    function Bar()
    {
        echo "This is Bar";
    }
}

$foo = new Foo();
$funcname = "Variable";
$foo->$funcname();  // Call $foo->Variable()

?>
Other Extensions