PHP 7.0.6 Released

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.

User Contributed Notes

Muneeb Aslam
5 months ago
following is a function that can be used to convert numeric date to alphabetic date, e-g from 2015-11-16 to 16 Nov, 2015.

1. Function takes 3 parameters, numeric date, locale and length of month
2. Function currently supports EN and ES month names.
3. Function can be calles as <?php convertDate("2015-11-16","en","full"); ?>

<?php

   
function convertDate($date,$locale,$length){
       
       
$monthNames = array(
               
"en" => array(
                   
"full" => array(1=>'January','February','March','April','May',
                   
'June','July','August','September','October','November','December'),
                   
                   
"short" => array(1=>'Jan','Feb','Mar','Apr','May','Jun',
                   
'Jul','Aug','Sep','Oct','Nov','Dec')
                ),
               
"es" => array(
                   
"full" => array(1=>'Enero','Febrero','Marzo','Abril','Mayo',
                   
'Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Deciembre'),
                   
                   
"short" => array(1=>'Ene','Feb','Mar','Abr','May','Jun',
                   
'Jul','Ago','Sep','Oct','Nov','Dec')
                ),
            );
           
           
$exploded = explode("-",$date);
           
$year = $exploded[0];
           
$month = $exploded[1];
           
$day = $exploded[2];
           
           
$month = $monthNames[$locale][$length][$month];
           
$date = $day . " " . $month . ", " . $year;
            return
$date;
    }

?>
aydinantmen [at] hotmail [dot] com
2 years ago
I want to use multidimentional arrays in a callback function what accepts second parameter.

Solution:

<?php

$arr1
= array("a" => "b", "c", "d's", "e" => array("f's", "g" => array("h's", "i" => "j's")));
$arr2 = mdarr_parameter($arr1);
$arr3 = mdarr_parameter($arr2, true);

function
mdarr_parameter($needle, $job=false) {
    if (
is_array($needle)) {
        foreach(
$needle as $name => $value) {
           
$needle[$name] = mdarr_parameter($value, $job);
        }
    } else {
       
// Now you do anything you want...
       
if ($job === true) {
           
$needle = stripslashes($needle);
        } else {
           
$needle = addslashes($needle);
        }
    }
    return
$needle;
}

print_r($arr2);
print_r($arr3);

/**
Outputs:

Array
(
    [a] => b
    [0] => c
    [1] => d\'s
    [e] => Array
        (
            [0] => f\'s
            [g] => Array
                (
                    [0] => h\'s
                    [i] => j\'s
                )

        )

)
Array
(
    [a] => b
    [0] => c
    [1] => d's
    [e] => Array
        (
            [0] => f's
            [g] => Array
                (
                    [0] => h's
                    [i] => j's
                )

        )

)
**/

?>
ayyappan dot ashok at gmail dot com
1 month ago
Type Checking in PHP 7

case A: //Using return
-------------------------
function welcome($name):string
{
   return $name;
}
echo welcome(100);

Results :  100

case B: //Using echo
-------------------------
function welcome($name):string
{
    echo $name;
}
welcome("100");

Results : Fatal error</b>:  Uncaught TypeError

function welcome(string $name)
{
    echo $name;
}
welcome(100);

Results :  100

case C: // Using strict_types
--------------------------------
declare(strict_types=1);

function welcome($name):string
{
    return $name;
}
echo welcome(90.99);

Results : Fatal error</b>:  Uncaught TypeError

case D: // Using strict_types
--------------------------------
//declare(strict_types=1);   On comment to strict_types

function welcome($name):string
{
    return $name;
}
echo welcome(90.99);

Results :  90.99

Note:
Behaviour of echo and return on welcome function show in different reflection. Representation of function type checking can be in either type as follows

function welcome(string $name){ }
or
function welcome($name):string{ }

Executed on PHP Version : 7.0.3
ayyappan dot ashok at gmail dot com
1 month ago
//Calling function with in a function or inner function

function add($a,$b){
    return $a+$b;
}

function sub($a,$b){
    return $a-$b;
}

function math($first, $second) {
    $res =  add($first, $second)/sub($first, $second);
    return (int)$res;
}
echo math(200,100);  //Results  3
ayyappan dot ashok at gmail dot com
1 month ago
For a good note, we can pass function as argument to function.

Take a look at the code. Passing function as argument can be achieved by Closure class ( A class to represent anonymous function).

function math(Closure $type, $first, $second) {
    // Execute the closure with parameters
    return $type($first, $second);
}

// Create an addition closure.
$addition = function ($first, $second) {
    // Add the values.
    return $first + $second;
};

// Create an subtraction closure.
$subtraction = function ($first, $second) {
    // Subtract the values.
    return $first - $second;
};

// Execute math function.
Note :anonymous  function is passed as an argument to function math.
echo math($addition, 2, 2);
echo PHP_EOL; // New line!
echo math($subtraction, 5, 3);

Courtesy : PHP Panda. Inspired from PHP panda.
ayyappan dot ashok at gmail dot com
1 month ago
As posted by ohcc at 163 dot com

    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.

  But when executed by passing various datatypes, it doesn't throw error  
  other than array and object.

  Please look over the code

  function abc($var) :string { return $var;}
  echo abc(true);    // Results 1
 
  function abc($var) :string { return $var;}
  echo abc(88.99);  // Results 88.99

  function abc($var) :string { return $var;}
  echo abc(array());
 
  //Results
  Fatal error : Uncaught TypeError: Return value of abc() must be of the  
  type string

  Note :
  Even though function is forced to return only string, it still considers the  
  other datatype arguments as string.
 
  echo gettype(abc(99.88));  // Returns string.
php at xenhideout dot nl
1 year ago
Please be advised that the code block defining the function, within the function_exists() call, has to be executed for the function to get defined, whereas this is not the case for regular, unenclosed functions.

Meaning, if you write code like this:

<?php

do_function
();

if (!
function_exists('my_undefined')) {
    function
my_undefined() {
    }
}

function
do_function() {
   
my_undefined();
}
?>

..Then my_undefined will not be defined before the code in do_function calls it. Some people put their function sections below the regular executing code of the script. Making any of it 'pluggable' can then cause problems.
ohcc at 163 dot com
5 months 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+
N Atanackovic
1 year ago
You can also call function from itself.  For example, I want to reach the deepest value in multidimensional array and I call function from inside the very same function. In this example function behave as some meta-loop.
   
<?php

$arr1
=array('a'=>array('e'=>array('f'=>array('g'=>'h', 'n' )),'b','c'));
$arr2=array('a'=>array('e'=>array('f'=>array('g'=>array('l'=>array('m'=>'w','q')), 'n' )),'b','c'));

function
Deep($array){
    foreach(
$array as $key){
        if(
is_array($key)){
             return
Deep($key);//calling the function inside the function
}else {
echo
$key;
        }
    }
}

echo
Deep($arr1); //outputs: hn
echo Deep($arr2); //outputs: wq

?>
info at namasteui dot com
4 months ago
Functions that are written by the user are User defined functions.

function function name [(argument1, argument 2, ...argument n)]
{any PHP code }

For example,

<?php
function hello()
{
print(
"Hello!");
}
hello();
?>

Using the function hello() anywhere in the PHP code will display the word "Hello".
To Top