Functions in PHP

Besides control and variables are PHP scripts through function calls. If the same code is executing this program code is often outsourced to a function. So instead of having the same code every time you call in the appropriate places on only the function, which then performs the actual work.<?php?>


The behavior of a function is affected by their parameters. A function can have any number of parameters, including parameters not exactly as an infinite number of parameters. Further, a function return value. This is also the standard use of a function, it will pass parameters and the function returns a value based on this back. For example, the sin function returns the sine of the given radian value.

Define your own functions<? Php?><? Php?><? Php?><? Php   / / Here now follows}For typical programming style to indent the code within the function by 4 spaces. Thus it can be seen where the function starts (the function keyword) and where it ends (at the closing brace}).<? Php   echo $ var / / help, where is defined $ var?$ Var = ‘HTML’;                / / Super globals are available3 Optional parameters<? Php   echo ‘x has the value: “‘ $ x.. ‘” test and has the value: “‘ $ test.. ‘”‘;myFunction (4, ‘word’) / / outputs 4 and word ofmyFunction () / / not possible since at least one parameter is required?>Although you use any functions in PHP code, as well as echo, it is customary return a calculated value for a function. This is implemented in PHP with the language construct return. It got to a value or a variable that you want to return.function myFunction () {   return 300;}?>It is also possible to exit a function and return with it to indicate no return value. This makes only sense if not expected is that a function has a return value.function myFunction () {   return;myFunction () / / works without problems?>Functions that are provided by PHP described in detail on the official website. Accordance with specially created features missing such a documentation. You may guess the functionality of the function name. For complex functions are hoping for a good documentation. For this purpose they PHPDocs. Before the actual function to add a PHPDoc comment which describes the function./ **** Square of this function back.* @ Param x The value of the you want to have a perfect square* /   return $ x * $ x;?>Array is a special variable in php, it is used to store multiple values in a single varible. I f you want to store a list data, you can store these values in a sigle variable as followsor$car [0]= “BMW”;array() is used to declare an array in php, There are three types of arrays available in php.Indexed, Associative, Multidimensional are the three types of arrays

$car [0]= “Nizan”;

$car [0]= “Maruthi”;

$car =  array(“maruthi”,”BMW”,”Nizan”);

Array in php


function square ($ x) {

* @ Return The number of square


* This function is passed a parameter and returns

* Returns a square number.

<? Php

5 Documentation of own functions

My $ var = function () / / works, $ var got the value NULL (a special data type)


/ / Code anderere

<? Php

With the return function is terminated at the same time. This can be done at any point, if it makes sense. In calculating the sine of the value 0 for example be expected not great but there is a case distinction quite early to the value 0 is returned.

echo myFunction () / / outputs 300 from

mach_das () / / is not running, as was leaving the function / closed

do_that () / / is still running

<? Php

4 Return value of functions

myFunction (1, 2, 3) / / possible, but the third parameter is lost

myFunction (5) / / outputs 5 and foobar


function myFunction ($ x, $ test = ‘foobar’) {

In PHP, it is allowed to define which one is not used later in a function call parameters. Thus it is e.g. possible to call the function although theoretically supports 3 parameters a function with only one parameter. So as to define a function assigns default values ​​to the parameters in the parameter list. It is done by specifying an assignment for the parameter. When the function is called with a parameter value that is used for the variable parameters. When the function is called without the parameter as the parameter variable contains the specified default value.


myFunction () / / will not work because the only function parameters and variables


function myFunction () {

Within the function, the parameter variables can be used. In addition to the superglobal variables no further variables are otherwise available. This is called also variable scope. The variable parameters are then filled in accordance with the values ​​of the function call. If the parameters for the function are variables whose values ​​are passed, but not the variable itself in first place, it is therefore not possible to change variables that are used outside the function.


/ / Normal php code

function myFunction ($ param1, $ param2) {

The contents of the function starts with an opening curly brace {. Now you can write any PHP code. At the end of the function body ends with a closing curly chamber }.

function myFunction ($ param1, $ param2)

Like a function call now follows an open parenthesis (. According to the clamp can now variables for the parameters are given. With the number of variables (comma separated) so you need to decide the number of parameters. However if you are planning a function with any to define many parameters you are at this point no parameters at all and use the functions in the function body func_get_args, func_get_arg and func_num_args. The parameter list is then finished with the closing parenthesis).

function myFunction

This is followed by the actual name of the function. The name itself is subject to common rules such as not begin with numbers. Furthermore, the functions have two underscores (__) begin special functionality. You should use this function only when they want exactly the functionality that is described in the manual.


Sooner or later we want not only the functionality provided by using special PHP define your own functions. A function definition starts with the keyword function in PHP.

Leave a Reply

Your email address will not be published. Required fields are marked *