subscribe: Posts | Comments

Functions of php

0 comments


Statement

A typical skeleton of a function is:
outputcol function ($ strCadena, $ strColor) (
/ / Get a string with the desired color
Echo ( "$ strCadena <FONT COLOR="#$strColor"> </ FONT>");
)
Y is called with:
outputcol ( "Red", "FF0000");
outputcol ( "Green", "00FF00");
Giving the result:
<font Color="#FF0000"> Red </ FONT>
<FONT COLOR="#00FF00"> Green </ FONT>

Parameters

Default parameters

If you want the default function set the text color blue, for example, redefine
as follows:
outputcol function ($ strCadena, $ strColor = "0000FF") (
/ / Get a string with the desired color
Echo ( "$ strCadena <FONT COLOR="#$strColor"> </ FONT>");
)

and could be called with:
outputcol ( "Default");
outputcol ( "Green", "00FF00");

Giving the result:
<FONT COLOR="#0000FF"> Default </ FONT>
<FONT COLOR="#00FF00"> Green </ FONT>
Obviously, whenever a function has n parameters by default, they shall be
declared the n parameters.

Parameters by reference

In PHP the default parameters are passed by value, ie, that if changed within
the function around the function of the variables passed as parameter is not
changed (these are called variable parameters of the function) .

If you want the caller’s variables are modified (the above parameters), you must
pass parameters by reference:
Concatenate function (& $ strDest, $ strSrc) (
/ / This function concatenates two strings and returns
/ / String passed in the first
/ / $ StrDest is passed by reference.
strDest $ = $ strDest. $ strSrc;
/ / As $ strSrc not be passed by reference, the following
/ / Instruction does not affect the current parameter
$ strSrc = "";
)
$ strOrigen = "World";
$ strDestino = "Hello";
Echo ( "Origin and destination is $ strOrigen is $ strDestino <BR>");
Concatenates ($ strDestino, $ strOrigen);
Echo ( "Origin and destination is $ strOrigen is $ strDestino <BR>");
Offered by the result:
Origin and destination is Hello World
Origin and destination is World Hello World
As you see, to pass a parameter by reference, simply put "&" before the
parameter name in function declaration, ie, put "&" before the name of the
formal parameter.

You can also pass a parameter by reference in function but not declared as such,
making the ampersand "&" to the current parameter (when you invoke the
function).

Variables in functions

Local variables

To define a local variable, simply assign a value to the variable:
Iva function ($ fValue) (
/ / $ FIVA is a local variable
FIVA fValue $ * = $ 0.16;
Echo ( "The VAT is fValue $ $ FIVA <BR>");
)
Iva (2350);

Variables

If the local variable retains the value of invocation to invocation of the
function, simply declare it as static:
function Counter () (
static $ count = 0;
$ count2 = 0;
$ count2 + +;
Echo ( "count is $ count and count2 <BR> worth $ count2");
$ count + +;
)
Counter ();
Counter ();
Counter ();
displayed on the page:
Count value of 0 and 1 voucher count2
Count 1 and count2 voucher worth 1
Count 2 and count2 voucher worth 1
Initializing the only variable is the first time that Counter is invoked.

Access to global variables

This is one of the points where it differs from PHP and C is an important cause
of many headaches for the first time in PHP.

To access a global variable from within a function is essential to declare it
within the function as global $variable since otherwise PHP will think you want
to reference a local variable:
TouchGlobal function () (
global $ strCadena;
$ strCadena = "hit";
$ nValue = 7;

Echo ( "Within TouchGlobal now $ strCadena goes."
$ strCadena. "and $ nValue worth $ nValue <BR>");
)
$ strCadena = "Hello World";
$ nValue = 4;
Echo ( " $ strCadena worth $ strCadena and $ nValue worth $ nValue <BR>");

TouchGlobal ();
Echo ( " $ strCadena and now worth $ strCadena $ nValue continues."
"worth $ nValue <BR>");

As you see, it is not necessary that the global variable in the file is
physically in front of the function, which has just been initialized before
calling the function. Changes made to a global variable inside a function,
remain when the function returns.

Another way to access the global variables is indexed by an associative array $GLOBALS

Returning a value

To return a value using the clause return
function Factorial ($ nValue) (
if ($ nValue <= 1) (
return 1;
Else ()
return Factorial ($ nValue-1) * $ nValue;
)
)
$ nNumber = 7;
Echo ( "The factorial of $ nNumber is." Factorial ($ nNumber));

PHP functions can call themselves (recursive), and functions can be declared
inside functions or classes of functions.