PHP Learning notes--functions

Source: Internet
Author: User

Function
First, format
function Fun_name ($str 1, $str 2 ... $strn)//fun_name is a function name, $str a variable of an incoming function such as 1
{
Fun_body;//fun_body as the body part of a function
}


Second, transfer parameters
1. By Value delivery method
The value of the argument is copied to the corresponding parameter, and the operation of the function memory is performed on the parameter, and the result is not affected by the argument, such as:
<?php
function Add ($a)
{
$a = $a *8;
echo $a;
}
$a = 10;
Add ($a);//print result is 80
echo $a; The print result is 10 and is not affected by the parameters in the function
?>
2. By-Reference delivery method
The memory address of the argument is passed to the formal parameter, and the parameter action in the function will be impressed with the argument, such as:
<?php
function Add (& $a)
{
$a = $a *8;
echo $a;
}
$a = 10;
Add ($a);//print result is 80
echo $a; The printed result is 80, and changes with the parameters in the function;
?>
3. Default parameter mode (optional parameter)
is to specify a default parameter, you can select parameters, such as:
<?php
function add ($a, $b = ")
{
$a = $a *8 + $b;
echo $a;
}
$a = 10;
Add ($a, 2);//optional parameter is not empty, print result is 82
Add ($a);//optional parameter is blank, printing result is 80
?>


Third, return value
Like most languages, the function return value of PHP also applies the keyword return, but it is important to note that in PHP, return returns only one value, cannot return multiple values at a time, and if you want to return multiple values, you need to define an array in the function and return an array directly.

Iv. variable functions
In PHP, a call to a function can be implemented by a variable, such as:

<?php
function add ($a, $b = ")
{
$a = $a *8 + $b;
echo $a;
}
$a = 10;
$func = "Add";
$func ($a, 3);//The function is called directly by adding a parenthesis to the variable name, and can also be passed directly into the argument
?>

V. References and dereferencing of functions
1. References
A reference to a function is actually a reference to the result of a function return, such as:
<?php
function &add ($a, $b = ")//Do not forget the &
{
$a = $a *8 + $b;
return $a;
}
$a = 10;
$inte = &add ($a, 3);//Assign the value to the variable inte by reference, and the & here is also not forgotten.
Echo $inte
?>
2. dereference
When you want to dereference, cancel directly using the unset () function, but be aware that it simply disconnects the reference from the function, not destroying the content, such as:
<?php
function &add ($a, $b = ")//Do not forget the &
{
$a = $a *8 + $b;
return $a;
}
$a = 10;
$inte = &add ($a, 3);//Assign the value to the variable inte by reference, and the & here is also not forgotten.
echo $inte. " <br> ";
Unset ($inte);//Dereference
echo "Reference canceled after:". $inte. " <br> "
echo Add ($a, 5)
?>

PHP Learning notes--functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.