function writeMyName()
{
echo "David Yang";
}
writeMyName();
?>
//Another output method
Now, we are going to use this function in our 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 nameFunction reference&
function &c()
{}
What does &c mean? What are the advantages and disadvantages? I don’t understand this either
function c(&$var)
{
$GLOBALS['MALL'] =&$var;
}
What is the difference between &$var and ordinary value transfer?
This means reference. You can modify the value of $var inside the function
For example:
function c($var){
$var=$var+1;
}
$var=10;
c($var);
echo $var;This output is 10If the function is changed to function c(&$var){}
System function usageSmall problems with PHP array intersection array_intersect(), array_intersect_assoc(), array_inter_key() functions
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). The array_intersect_assoc() function It binds the key value and the value and compares the intersection part together. The array_intersect_key() function compares the key values of the two arrays and returns the array of key value intersection.However, some minor problems have also been encountered in practical applications, as follows:
Example:
"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 "Running results:
"; " $num = array_intersect_assoc($array,$array1); "; " print_r($num); " "; " $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. The array_intersect() function only compares array values, and if there is a comparison between "Red" and "Red2", it will return "Red", otherwise it will not return "Red2";
Summary:
2. The array_intersect_assoc() function compares the value of the array with the key value, and there will be no array_intersect situation, which is suitable for stricter comparisons;
3. The array_intersect_key() function is suitable for comparing the intersection of two array key values. It returns not only the key value, but the key value and the corresponding array value
Example
$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";
?>