Home  >  Article  >  Backend Development  >  PHP function definition and function & reference usage

PHP function definition and function & reference usage

高洛峰
高洛峰Original
2016-11-29 11:57:301463browse

Function definition: 1. A function is a named, 2. Independent code segment, 3. The function performs a specific task, 4. And can return a value to the program that calls it.

Advantages of the function: 1. Improve the program Reusability, 2. Improve program maintainability, 3. Improve development efficiency, 4. Improve software reliability, 5. Control program complexity.

Declaration of function:

function function name () {

}

function function name (parameter 1, parameter 2, parameter...)

{

function body

}

function function function name ()

{

function body;

Return value;

}

function function name (parameter list...)

{

function body;

return value

}

Note:

1. The function must be called to be executed, and can be called before it is declared. It can also be called after declaration.

2. The function name is the same as the variable, aaa bbb ccc aaaBbbCcc (the first word is lowercase, and the first letter of each subsequent word is capitalized)

3. The function cannot have the same name when declaring

4 , you can change the behavior of the function by passing parameters to the function

Formal parameters: When declaring a function, the declared parameters are variables, multiple parameters are separated

Actual parameters: When calling the function, the formal parameter value is passed ( Data (can also be a variable)

5. If there is no return value, it is called a process

6. Return data by using the return statement

7. The function ends when it reaches the return statement. Do not write code after this statement. You can use return to end the execution of the function.

The role of the function name:

1. Call the function and start executing the function

2. You can pass data to the function

3. The function name is the returned value

PHP function: All functions start with the keyword "function()".

Name functions - The name of the function should suggest its function. The function name starts with a letter or an underscore. Add "{" - the part after the opening curly brace is the code of the function.

Insert the function code, add a "}" - the function is terminated by closing the curly brace.

{

echo "David Yang";

}

writeMyName();

?>

Another output method, now, We are going 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()

{

}

&c What does it mean, what are the advantages and disadvantages of this me I don’t understand the difference between

function c(&$var)

{

$GLOBALS['MALL'] =&$var;

}

&$var and ordinary value passing. This is the meaning of reference. , you can modify the value of $var inside the function, for example:

function c($var){

$var=$var+1;

}

$var=10;

c($var ; Question, the problem of finding the intersection of two arrays can be realized using array_intersect(), array_inersect_assoc, array_intersect_key. The array_intersect() function is to find the intersection of two numbers and return an array with common elements in the intersection (only the arrays are worthy of comparison), array_intersect_assoc( ) function is to bind the key value to the value and compare the intersection part together. The array_intersect_key() function is to compare the key values ​​of the two arrays and return the array of key value intersection.

However, some small problems have also been encountered in practical applications. , as shown below, 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 ""; 

//开源代码phpfensi.com 

$num = array_intersect_key($array,$array1);         

print_r ($num);     

?> 

 

//运行结果: 

 

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 ) 

总结:

1.array_intersect()函数进行的比较只有数组值的比较,而且存在如”Red“和”Red2“比较时会返回"Red",反之则不会返回"Red2";

2.array_intersect_assoc()函数是将数组的值与键值一起比较,而且不会存在array_intersect的情况,适用于较严格的比较;

3.array_intersect_key()函数适用于比较两个数组键值的交集,返回的并不只有键值,而是键值和对应的数组值

实例:

$a = array( 

        'a' => '你好', 

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

?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Strengthen the for loopNext article:Strengthen the for loop