PHPsu
MANUAL ZH  |  EN
     


Current Position :| index>PHP FUNCTION> PHP Programming

PHP Programming

FROM: AUTHOR: TIME:2008-08-19 HITS:

Introduction

Functions (often called methods) are a way to group common tasks or calculations to be re-used easily. phpsu.com

Functions ss in computer programming are much like mathematical functions: You can give the function values to work with and get a result without having to do any calculations yourself. phpsu is a phpschool

You can also find a huge list of predefined functions built into PHP in the PHP Manual's function reference.

www.phpsu.com

phpsu is a phpschool

[edit] How to call a function

Note that echo is not a function.[1] "Calling a function" means causing a particular function to run at a particular point in the script. The basic ways to call a function include:

http://www.phpsu.com

  • calling the function to write on a new line (such as after a ";" or "}")
print('I am Naresh, I am.');
phpsu.com is a free phpscool
  • calling the function to write on a new line inside a control
<?php
if ($a==72){
print('I am Naresh, I am.');
}
?>
http://www.phpsu.com
  • assigning the returned value of a function to a variable "$var = function()"
<?php
$result = sum ($a, 5);
?>
phpsu is a phpschool
  • calling a function inside the argument parentheses (expression) of a control
<?php
while ($i < count($one)){

}
?>

do you kown phpsu.com?

In our earlier examples we have called several functions. Most commonly we have called the function print() to print text to the output. The parameter for echo has been the string we wanted printed (for example print("Hello World!") prints "Hello World!" to the output). phpsu提供的php教程

If the function returns some information we assign it to a variable with a simple =:

www.phpsu.com

$var1 = func_name();
phpsu提供的php教程

phpsu提供的php教程

[edit] Parameters

Parameters are variables that exist only within that function. They are provided by the programmer when the function is called and the function can read and change them locally (except for reference type variables, which are changed globally - this is a more advanced topic).

phpsu提供的php教程

When declaring or calling a function that has more than one parameter, you need to separate between different parameters with a comma ','. phpsu is a phpschool

A function declaration can look like this:

http://www.phpsu.com

function print_two_strings($var1, $var2)
{
echo $var1;
echo "\n";
echo $var2;
return NULL;
}
phpsu

To call this function you must give the parameters a value. It doesn't matter what the value it, as long as there is one.

phpsu提供的php教程

function call:

phpsu is a phpschool

print_two_strings("hello", "world");

phpsu

Output:

phpsu is a phpschool

hello
world

phpsu.com is a free phpscool


When declaring a function you sometimes want to have the freedom not to use all the parameters, therefore PHP allows you to give them default values when declaring the function:

http://www.phpsu.com

function print_two_strings($var1 = "Hello World", $var2 = "I'm Learning PHP")
{
echo($var1);
echo("\n");
echo($var2);
}
www.phpsu.com

These values will only be used if the function call does not include enough parameters. If there is only one parameter provided then $var2 = "I'm Learning PHP". www.phpsu.com

function call:

phpsu is a phpschool

print_two_strings("hello");

phpsu.com is a free phpscool

Output: phpsu is a phpschool

hello
I'm Learning PHP
phpsu

Another way to have a dynamic number of parameters is to use PHP's built-in func_num_args, func_get_args, and func_get_arg functions. phpsu提供的php教程

function mean()
{
$sum = 0;
$param_count = func_num_args();
for ($i = 0; $i < $param_count; $i++)
{
$sum += func_get_arg($i);
}
$mean = $sum / $param_count;
echo "Mean: {$mean}";
return NULL;
}

http://www.phpsu.com

Or:

phpsu提供的php教程

function mean()
{
$sum = 0;
$vars = func_get_args();
for ($i = 0; $i < count($vars); $i++)
{
$sum += $vars[$i];
}
$mean = $sum / count($vars);
echo "Mean: {$mean}";
return NULL;
}
phpsu.com is a free phpscool

The above functions would calculate the arithmetic mean of all of the values passed to them and output it. The difference is that the first function uses func_num_args and func_get_arg, while the second uses func_get_args to load the parameters into an array. The output for both of them would be the same. For example: welcome to phpsu.com

mean(35, 43, 3);

www.phpsu.com

Output:

phpsu is a phpschool

Mean: 27

www.phpsu.com

phpsu提供的php教程

[edit] Returning a value

This function is all well and good, but usually you will want your function to return some information. Generally there are 2 reasons why a programmer would want information from a function:

phpsu

  1. The function does tasks such as calculations, and we need the result.
  2. A function can return a value to indicate if the function encountered any errors.

To return a value from a function use the return() statement in the function. phpsu提供的php教程

function add_numbers($var1 = 0, $var2 = 0, $var3 = 0)
{
$var4 = $var1 + $var2 + $var3;
return $var4;
}
www.phpsu.com


Example PHP script:

http://www.phpsu.com

<?php
function add_numbers($var1 = 0, $var2 = 0, $var3 = 0)
{
$var4 = $var1 + $var2 + $var3;
return $var4;
}

$sum = add_numbers(1,6,9);
echo "The result of 1 + 6 + 9 is {$sum}";
?>
www.phpsu.com

Result:

phpsu提供的php教程

The result of 1 + 6 + 9 is 16
phpsu is a phpschool

Notice that a return() statement ends the function's course. If anything appears in a function declaration after the return() statement is executed, it is parsed but not executed. This can come in handy in some cases. For example:

http://www.phpsu.com

<?php
function divide ($dividee, $divider) {
if ($divider == 0) {
//Can't divide by 0.
return false;
}
$result = $dividee/$divider;
return $result;
}
?>

phpsu.com

Notice that there is no else after the if. This is due to the fact that if $divider does equal 0, the return() statement is executed and the function stops.

www.phpsu.com

If you want to return multiple variables you need to return an array rather than a single variable. For example: welcome to phpsu.com

<?php
function maths ($input1, $input2) {
$total = ($input1 + $input2);
$difference = ($input1 - $input2);
$ret = array("tot"=>$total, "diff"=>$difference);
return $ret;
}
?>
phpsu is a phpschool

When calling this from your script you need to call it into an array. For example: phpsu提供的php教程

<?php
$return=maths(10, 5);
?>
phpsu提供的php教程

In this case $return['tot'] will be the total (eg 15), while $return['diff'] will be the difference (5).

phpsu.com

welcome to phpsu.com

[edit] Runtime function usage

A developer can create functions inside a PHP script without having to use the function name($param...) {} syntax. This can done by way of programming that can let you run functions dynamically.

http://www.phpsu.com

welcome to phpsu.com

[edit] Executing a function that is based on a variable's name

There are two ways to do it, either using direct call or call_user_func or call_user_func_array: http://www.phpsu.com


ss====Using call_user_func* functions to call functions==== call_user_func and call_user_func_array only differ that the call_user_func_array allows you to use the second parameter as array to pass the data very easily, and call_user_func has an infinite number of parameters that is not very useful in a professional way. In these examples, a class will be used for a wider range of using the example: phpsu提供的php教程

<?php
class Some_Class {
function my_function($text1,$text2,$text3) {
$return = $text1."\n\n".$text2."\n\n".$text3;
return $return;
}
}
$my_class=new Some_Class();
?>
phpsu.com is a free phpscool

Using call_user_func:

http://www.phpsu.com

<?php
$one = "One";
$two = "Two";
$three = "Three";
$callback_func = array(&$my_class,"my_function");
$result = call_user_func($callback_func,$one,$two,$three);
echo $result;
?>
do you kown phpsu.com?

Using call_user_func_array:

phpsu.com

<?php
$one = "One";
$two = "Two";
$three = "Three";
$callback_func = array(&$my_class,"my_function");
$result = call_user_func_array($callback_func,array($one,$two,$three));
echo $result;
?>
phpsu

Note how call_user_func and call_user_func_array are used in both of the examples. call_user_func_array allows the script to execute the function more dynamically. www.phpsu.com

As there was no example of using both of these functions for a non-class function, here they are: Using call_user_func: phpsu.com

<?php
$one = "One";
$two = "Two";
$three = "Three";
$callback_func = "my_function";
$result = call_user_func($callback_func,$one,$two,$three);
echo $result;
?>
do you kown phpsu.com?

Using call_user_func_array:

http://www.phpsu.com

<?php
$one = "One";
$two = "Two";
$three = "Three";
$callback_func = "my_function";
$result = call_user_func_array($callback_func,array($one,$two,$three));
echo $result;
?>
phpsu提供的php教程

http://www.phpsu.com

[edit] More complicated examples

<?php
$my_func($param1, $param2);
${$my_class_name}->$my_func($param1, $param2);

call_user_func($my_func, $param1, $param2);

call_user_func(array(&${$my_class_name}, $my_func), $param1, $param2);

call_user_func_array($my_func, array($param1, $param2));
// Most powerful, dynamic example
call_user_func_array(array(&${$my_class_name}, $my_func), array($param1, $param2));
?>
phpsu提供的php教程

TODO: Somebody add what the -> in line 3 means and what the & in line 5 means phpsu提供的php教程

phpsu提供的php教程

[edit] Creating runtime functions

Creating runtime functions is a very good way of making the script more dynamic:
<?php
$function_name=create_function('$one, $two','return $one+$two;');
echo $function_name."\n\n";
echo $function_name("1.5", "2");
?>

phpsu is a phpschool

create_function creates a function with parameters $one and $two, with a code to evaluate return... When create_function is executed, it stores the function's info in the memory and returns the function's name. This means that you cannot customise the name of the function although that would be preferred by most developers. do you kown phpsu.com?


TITLE:PHP Programming
Copyright 2008 The PHPsu All rights reserved. This mirror generously provided by: .Hp Inc.
Last updated: Tue Aug 19 21:34:27 GMT-8 2008