PHP function definition and function & reference usage_PHP tutorial

WBOY
Release: 2016-07-20 11:02:46
Original
903 people have browsed it

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 name

Function 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 10

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


System function usage

Small 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 "
"; " $num = array_intersect_assoc($array,$array1); "; " print_r($num); " "; " $num = array_intersect_key($array,$array1); "; " print_r ($num); "?>
Running results:

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 array values, and if there is a comparison between "Red" and "Red2", it will return "Red", otherwise it will not return "Red2";

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


http://www.bkjia.com/PHPjc/445346.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445346.htmlTechArticlefunction writeMyName() { echo David Yang; } writeMyName(); ? //Another way to output now, We are going to use this function in a PHP script: html function writeMyName() { echo...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!