Php function definition and Function & amp; reference usage _ PHP Tutorial

Source: Internet
Author: User
Php function definition and Function & amp; reference usage. FunctionwriteMyName () {echodavid Yang;} writeMyName ();? Another output method: Now we need to use this function in the PHP script: htmlfunctionwriteMyName () {echo/* function definition:
* 1. the function is named
* 2. independent code segments
* 3. the Function executes a specific task.
* 4. you can return a value to the program that calls it.
*
* Advantages of functions:
* 1. improve program reusability
* 2. Improve the maintainability of the program
* 3. improved development efficiency
* 4. Improve Software Reliability
* 5. complexity of the control program
*
* Function declaration
* Function name (){
*
*}
*
* Function name (parameter 1, parameter 2, parameter ...)
*{
* Function body
*}
*
* Function name ()
*{
* Function body;
* Return value;
*}
* Function name (parameter list ...)
*{
* Function body;
* Return value
*}
*
* Note:
* 1. functions must be called before execution. they can be called before declaration or after declaration.
* 2. the function name is the same as the variable name. aaa bbb ccc aaaBbbCcc (the first word is lowercase, and the last letter is uppercase)
* 3. the function name cannot be repeated during declaration.
* 4. you can change the function behavior by passing parameters to the function.
* Form parameters: when declaring a function, the declared parameters are variables. multiple parameters are used and separated.
* Real parameter: the parameter value (data or variable) is passed when a function is called)
* 5. a process is called if no return value exists.
* 6. return data using the return statement
* 7. when the function is executed, the return statement ends. you can use return to end the function execution instead of writing code after this statement.
*
* Function name:
* 1. call the function and start executing the function.
* 2. data can be transferred to the function.
* 3. the function name is the returned value.
*
*
*/
PHP functions:
All functions start with the keyword "function ()".
Name function-the function name should prompt its function. The function name must start with a letter or underscore.
Add "{"-the section after the opening curly braces is the function code.
Insert function code
Add a "}"-the function ends by closing curly braces.


Function writeMyName ()
{
Echo "David Yang ";
}

WriteMyName ();
?>


// Another output method
Now, we need to use this function in the PHP script:

<

Html>


Function writeMyName ()
{
Echo "David Yang ";
}

Echo "Hello world!
";
Echo "My name is ";
WriteMyName ();
Echo ".
That's right ,";
WriteMyName ();
Echo "is my name .";
?>


Output of the above code:

Hello world!
My name is David Yang.
That's right, David Yang is my name

Function Reference &

Function & c ()
{

}

What does & c mean and what are the advantages and disadvantages?

Function c (& $ var)
{
$ GLOBALS ['Mall'] = & $ var;
}
& $ What is the difference between var and common values!
This is a reference. you can modify the value of $ var in the function.
For example:
Function c ($ var ){
$ Var = $ var + 1;
}
$ Var = 10;
C ($ var );
Echo $ var; the output is 10.

If the function is changed to function c (& $ var ){}


System function usage

Small issues with the intersection of PHP array array_intersect (), array_intersect_assoc (), and array_inter_key () functions
To solve the intersection problem of two arrays, use array_intersect (), array_inersect_assoc, and array_intersect_key. the array_intersect () function is used to calculate the intersection of two numbers, returns an array of all elements in the intersection (only the array is worth comparing). The array_intersect_assoc () function binds the key value and value to compare the intersection part and the array_intersect_key () function () A function is an array that compares the key values of two arrays and returns the intersection of key values.

However, some minor problems have also been encountered in practical applications, as shown below:

Instance:


"Red", "green" => "red4", "Red15" => "Red", 7 => "Level", "Width" => "Red ", "azzzz1" => "art", "peak" => 158); $ array1 = array ("red" => "Red2", "greena" => "red ", "Red15" => "Red", 7 => "Level", "Width" => "Red", "azzzz" => "art ", "peak" = & gt; 158); $ num = array_intersect ($ array, $ array1); print_r ($ num); echo"
"; $ Num = array_intersect_assoc ($ array, $ array1); print_r ($ num); echo"
"; $ Num = array_intersect_key ($ array, $ array1); print_r ($ num);?>

Running result:


Array ([red] => Red [Red15] => Red [7] => Level [Width] => Red [azzzz1] => art [peak] => 158) array ([Red15] => Red [7] => Level [Width] => Red [peak] => 158) array ([red] => Red [Red15] => Red [7] => Level [Width] => Red [peak] => 158)
Summary:

1. the array_intersect () function only compares the array values. if "Red" and "Red2" are compared, "Red" is returned. otherwise, "Red2" is not returned ";

2. the array_intersect_assoc () function compares the array value with the key value without the case of array_intersect. This function is suitable for strict comparison;

3. the array_intersect_key () function is applicable to comparing the intersection of two array key values. instead of only the key value, it returns the key value and the corresponding array value.


Instance

$ A = array (
'A' => 'hello ',
'B' => 'BB'
);
// Header ('content-Type: application/json; charset = UTF-8 ');
Echo json_encode ($ a). "n ";
Function replace_unicode_escape_sequence ($ match)
{
Return mb_convert_encoding (pack ('H * ', $ match [1]), 'utf-8', 'ucs-2be ');
}
Echo preg_replace_callback ('/u ([0-9a-f] {4})/I', 'replace _ unicode_escape_sequence ', json_encode ($ a). "n ";
?>


Http://www.bkjia.com/PHPjc/445346.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445346.htmlTechArticlefunction writeMyName () {echo David Yang;} writeMyName ();? // Another output method. now we need to use this function in the PHP script: html function writeMyName () {echo...

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.