SunshinePHP 2019 CFP Started

User-defined functions

A function may be defined using syntax such as the following:

Example #1 Pseudo code to demonstrate function uses

<?php
function foo($arg_1$arg_2/* ..., */ $arg_n)
{
    echo 
"Example function.\n";
    return 
$retval;
}
?>

Any valid PHP code may appear inside a function, even other functions and class definitions.

Function names follow the same rules as other labels in PHP. A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*.

Tip

See also the Userland Naming Guide.

Functions need not be defined before they are referenced, except when a function is conditionally defined as shown in the two examples below.

When a function is defined in a conditional manner such as the two examples shown. Its definition must be processed prior to being called.

Example #2 Conditional functions

<?php

$makefoo 
true;

/* We can't call foo() from here 
   since it doesn't exist yet,
   but we can call bar() */

bar();

if (
$makefoo) {
  function 
foo()
  {
    echo 
"I don't exist until program execution reaches me.\n";
  }
}

/* Now we can safely call foo()
   since $makefoo evaluated to true */

if ($makefoofoo();

function 
bar() 
{
  echo 
"I exist immediately upon program start.\n";
}

?>

Example #3 Functions within functions

<?php
function foo() 
{
  function 
bar() 
  {
    echo 
"I don't exist until foo() is called.\n";
  }
}

/* We can't call bar() yet
   since it doesn't exist. */

foo();

/* Now we can call bar(),
   foo()'s processing has
   made it accessible. */

bar();

?>

All functions and classes in PHP have the global scope - they can be called outside a function even if they were defined inside and vice versa.

PHP does not support function overloading, nor is it possible to undefine or redefine previously-declared functions.

Note: Function names are case-insensitive, though it is usually good form to call functions as they appear in their declaration.

Both variable number of arguments and default arguments are supported in functions. See also the function references for func_num_args(), func_get_arg(), and func_get_args() for more information.

It is possible to call recursive functions in PHP.

Example #4 Recursive functions

<?php
function recursion($a)
{
    if (
$a 20) {
        echo 
"$a\n";
        
recursion($a 1);
    }
}
?>

Note: Recursive function/method calls with over 100-200 recursion levels can smash the stack and cause a termination of the current script. Especially, infinite recursion is considered a programming error.

add a note add a note

User Contributed Notes 2 notes

up
0
everton3x at gmail dot com
2 months ago
Although functions have a global scope, we can use them in conjunction with namespaces.

This works perfectly:

<?php

function test(){
    echo
"test 1\n";
}

namespace
NS;

function
test(){
    echo
"test 2\n";
}

namespace
NS\Sub;
function
test(){
    echo
"test 3\n";
}

?>
up
-4
ohcc at 163 dot com
2 years ago
As of PHP 7.0, you can restrain type of return value of user defined functions.

Syntax is : function FunctionName ($arg1, $arg2, ...)  : TYPE { ... }

TYPE is a string representing the type of return value, TYPE can be a class name or a php variable type, such as array/string/bool/int/float.

When TYPE is one of the following value, it also stands for a classname

str/boolean/integer/real/double/resource/object/scalar

However,in my opion, boolean/bool, integer/int ... should have the same meaning, but at least in PHP7, they stand for different meanings respectively. This may be fixed in later versions of PHP.

<?php
   
function wxc ($var) : string {
        return
$var;
    }
?>

this function must return a string, if it return something else when called, a "Fatal error: Uncaught TypeError" error will be triggered.

code above is supported only in PHP 7+
To Top