Nov 7, 2011

Functions in PHP



One of the most important tools in any programming language are the functions. A function is a set of routines and actions throughout the script will be executed multiple times grouped in a function and from anywhere in the script can be called and executed. In turn, this function can take external parameters of which depend on the result of a function.
The functions should always be placed before the function call (of course). The function syntax is as follows:
function name (parameters) { 
function instructions 
} 
to call the function would be as follows: name (parameters)
An example to understand the use of functions is as follows: 
We will create a function that performs the sum of two numbers and display the result
function add ($ sumando1, $ sumando2) { 
$ Sum = $ sumando1 + $ sumando2 
echo $ sumando1 ."+".$ ."=".$ sumando2 sum; 
}
sum (5.6)
An important fact to note is that the variables declared within the function or will only exist this value within the function.
There are cases where we do not know the number of parameters you pass to the function and in these cases we use the functions created for this purpose as:
func_num_args () Number of parameters that have been passed to the function 
func_get_args () Returns an element that form the argument list



Introduction

This would be a call to a function without parameters:
  <? Php
function Hello () {
$ Message = "Hello friend";
echo $ message;
}
?>
<html>
<head>
<title> My Page </ title>
</ Head>
<body>
<? Php
Hello ();
?>
</ Body>
</ Html>
The above code executes a function call
 Hello ()
, Which will print the text "Hello friend."

Functions with parameters.
If you need a function to which you can pass parameters, be written in the same way, with the name of the parameters between the parentheses, separated by commas. For a two-parameter function, the code might look something like:

Crystal Clear app konsole.png
Sum function
  <? Php 
function sum ($ a, $ b) {
return $ a + $ b;
}

$ A = 1, $ b = 2;
echo "Let's add $ a + $ b =". sum ($ a, $ b);
?>



Note that in this example we use the keyword return. This word is used for function return values ​​to the place where it was called.
For practical purposes we say that the function is executed in a separate and "replaced" at runtime by the result returned by return in the context in which they had called. In the above example, add ($ a, $ b) === 3 (being the identity operator ===).

Default Parameters

In the formal definition of the function can specify default settings, so that if one is not passed at the time of call, the parameter takes a value. If this value is not specified, the interpreter will return a runtime error for invalid number of parameters.
To specify, here's how:
Example of default
   function test ($ foo = 'bar') {
/ * Operations * /
return $ foo
}



 Function Overloading

From PHP4 [Note 1] , functions can be overloaded, ie, define functions that accept parameters and can have a different internal logic.
An overloaded function can be something like:
First attempt
 overload function () {
return array (1);
}

overload function ($ a) {
return array (2, $ a);
}

overload function ($ a, $ b) {
return array (3, array ($ a, $ b));
}

Ovelar function ($ a, $ b, $ c) {
return array (4, array ($ a, $ b, $ c));
}



Needless to say, since PHP5 [Note 1] functions with no parameters specified (with an empty list) can receive any number of arbitrary parameters. The list of functions above could be rewritten as one, as follows:

Best approximation
 not_overloaded function () {
return array (
func_num_args () PHP Manual ,
func_get_args () PHP Manual
 );
}



An example shows the parameters used, typically in the books, is as follows:

function abc
  function abc () {
$ Array = func_get_args ();
$ ArrayCount = count ($ array);
for ($ i = 0; $ i <$ arrayCount, $ i + +) {
echo "$ i used as a parameter: {$ array [$ i]}";
}
}



Abc function call
 abc ('test', 'test2', 3, 4);
Result
 Was used as a parameter 0: test
Was used as parameter 1: test2
Was used as parameter 2: 3
Was used as parameter 3: 4



You can also create anonymous functions at runtime. These functions are very useful in the lambda-calculus, and are created with create_function PHP-Manual , with a string in the second parameter would be the PHP code must perform the function. In PHP6 will introduce a new style of anonymous functions that also allows to assign functions to variables, as well as in other languages ​​such as JavaScript.
Thus, the following code would be valid in PHP6 or higher.

Experimental code
  $ A = function ($ b) {return $ b;};
if ($ a (45) == 45) echo "Good!" / / It prints 'OK!'



 Notes

  1. ↑ 1.0 1.1 The author does not remember from what minimum version, but surely all in PHP5

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Premium Wordpress Themes