In any programming language, there are routines. In C, they are called functions in assembly language – routines, and even Pascal there are two kinds of subprograms: procedures and functions.

Sub – this specially designed program fragment, which can be accessed from anywhere within the program. Routines greatly simplify life for programmers, improving the readability of the source code, as well as reducing it, as some parts of the code do not need to write a few times

In PHP, such routines are user-defined functions.

In addition to the built-in PHP functions, it is often necessary to create custom functions that perform certain tasks.

Features custom PHP functions

Here are the features of user-defined functions in PHP:

  • Available default settings. It is possible to cause the same function with a variable number of parameters;
  • Custom Functions can return any type;
  • The scope of variables inside a function is a hierarchical (tree);
  • It is possible to change the variables passed as an argument.

In PHP programmer is given a high enough freedom to create user-defined functions. Unlike the C ++ user-defined functions available in the default settings.

The downside of custom PHP functions are:

1. Inability to declare local functions. In PHP, you can not declare a local function, as it can be done in other programming languages. Simply put, you can not create a function within another function in such a way that the first (embedded) function is visible only in the second function. In PHP, a nested function will be available throughout the program (script), and therefore will not be local.

Consider the following example:

<?php
function first_function() {
  echo "<h4>Some text</h4>";
    function second_function() {
     echo "<h5>Some more text</h5>";
    }
}
first_function();
second_function();
?>

In this example, the script will output:

Some Text
Some More Text

Hence, both functions are accessible by the program. This indicates that the second function is not local.

2. The second drawback custom PHP functions associated with the function scope. To understand this category, you need to explain what variables are global and which – local.

Global variables – variables that are available throughout the program, including the subprogram (function).

Local variables – variables defined within the subroutine (function). They are available only within the function in which they are defined.

For PHP all declared and used in the function are local variables of the default function. That is, the default is not possible to change the value of the global variable in the function body.

If you are in the body of user-defined function will use a variable with the same name as a global variable (located outside the user-defined function), then there is no relationship to the global variable, this local variable will not have. In this scenario, the custom function will create a local variable with the same name as a global variable, but is available for this local variable is only within this user-defined function.

Let us explain this fact by way of example:

<?php
$a = 100;

function funct() {
$a = 70;
 echo "<h4>$a</h4>";
}
funct();
echo "<h2>$a</h2>";
?>

The script displays the first 70, and then 100:

70
100

To get rid of the above drawback, in PHP there is a special instruction global , allowing user-defined function to work with global variables. 

Creating custom functions

A custom function can be declared in any part of the program (script) to the place of its first use. One does not need any pre-announcement, as in other programming languages, in particular C. Advantages PHP approach used in the following.

Reaching the user-defined function, the compiler will check the correctness of the definition and will perform translation functions in the definition of the internal representation, but to translate the code itself it will not. And rightly so – why should broadcast code that may not even be used. The syntax for declaring functions as follows:

Announcement function begins official word function , followed by the name of the function after the function name – the list of arguments in parentheses. The function body is enclosed in braces and can contain any surprising number of operators.

Requirements for function names:

  • Function names can contain Russian letters, but to give the names of functions, consisting of Russian letters is not recommended;
  • Function names can not contain spaces;
  • The name of each user-defined function must be unique. At the same time, it must be remembered that the case when declaring the functions and are accessed are not taken into account. That is, for example, funct () and FUNCT () have the same name;
  • The functions can be given the same names as the variables, but without the $ sign at the beginning of the names.

The types of values that are returned by user-defined functions, can be arbitrary. To transfer the results of the work of custom functions in the main program (script) used construction return statement . If the function does not return anything, constructreturn does not indicate. The design of return can return anything you want, including arrays.

Here are some examples of using user-defined functions:

<?php

function funct() {
$number = 777;
 return $number;
}
$a = funct();
echo $a;

?>

In this example, the function returns funct using design return the number 777. The return value of the function assigned to the global variable $ a , then echo statement displays the value of the variable $ a in the browser. As a result, we will see in the browser the number 777.

(Visited 28 times, 1 visits today)
adminPHPTips
In any programming language, there are routines. In C, they are called functions in assembly language - routines, and even Pascal there are two kinds of subprograms: procedures and functions. Sub - this specially designed program fragment, which can be accessed from anywhere within the program. Routines greatly simplify life...

Do you want to be notified about new DBA updates, releases, jobs and free tips? Join our email newsletter. It's fast and easy. You will be among the first to know about hot new DBA updates and stuff, it will help you enhance your DBA skills.
We take your privacy very seriously