Php function definition and Function & reference usage

Source: Internet
Author: User
Php function definition and Function & amp; reference usage function definition: 1. the function is named 2. independent code segment, 3. function to execute a specific task, 4. you can return a value for the program that calls it.

Advantages of functions: 1. improving program reusability; 2. improving program maintainability; 3. improving development efficiency; 4. Improving Software reliability; 5. controlling program complexity.

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. the function can be executed only after being called. it 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.

Parameter: when declaring a function, the declared parameter is a variable. 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.

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 should start with a letter or underline. add "{"-the part after the opening curly braces is the function code.

Insert the function code and 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:

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 normal value transfer? 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: the intersection of PHP arrays, array_intersect (), array_intersect_assoc (), and array_inter_key (). to solve the intersection problem of two arrays, use array_intersect (), array_inersect_assoc, array_intersect_key is implemented. the array_intersect () function is used to calculate the intersection of two numbers, and returns an array with a common element of the intersection (just an array worth comparing), array_intersect_assoc () the function is to bind the key value and value, compare the intersection part together, the array_intersect_key () function is to compare the key values of the two arrays, return to the array of the key value intersection.

However, some minor problems have also been encountered in practical applications, as shown in the following example:

$ Array = array ("red" => "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" => 158 );

$ Num = array_intersect ($ array, $ array1 );

Print_r ($ num); echo"
";

$ Num = array_intersect_assoc ($ array, $ array1 );

Print_r ($ num); echo"
";

// Open source code phpfensi.com

$ 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 ";

?>

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.