Home >Backend Development >PHP Tutorial >PHP became a monk halfway (2)_PHP tutorial
Function is essential for every programming language, I have to understand what’s different about PHP
The sharpest thing is the return type, there is no need to indicate
in the function declaration
The only thing you need to pay attention to is that you have to use the corresponding type to receive the return value~
Old faces in C++
Pass value by reference "&"
Default parameters
Recursive function---a powerful tool for drawing forms!
Heroes don’t care about their origin, weak types create arrays with souls
Assignment method:
$say = array("Chinese" , "English" , "French");
//Equivalent to
$say[0] = "Chinese" ; $say[1] = "English" ; $say[2] = "French" ;
$say = array("China"=>"Chinese" , "England"=>"English" , "France"=>"French");
//Equivalent to
$say["China"] = "Chinese" ;
$say["England"] = "Chinese" ;
$say["France"] = "Chinese" ;
PHP’s array is different from Java’s array. It feels like a combination of array and map
$myArray = array(0=>"0th","2"=>"2","This is OK"=>"This is really OK");
echo $myArray[0]; //Normal mode
echo $myArray[1]; //If there is no value, it will be counted as empty
echo $myArray[2]; // "2" will be output
echo $myArray["2"]; //"2" will also be outputecho $myArray["This is OK"]; //will output "This is really OK"
$myArray[] =0;
$myArray[] =1;
$myArray[] =2;
$myArray[] =3;
echo $myArray[3]; //will output 3
$myArray = range("a", "z",3); //From "a" to "z", output every 3
Array expansion www.2cto.com
Return the value corresponding to the index:
print_r($myArray);
boolean is_array(mixed var);
array_unshift($vars, $key, $value); //Add
from the beginning
array_push($vars, $key, $value); //Addfrom the end
array_shift($vars); //Delete from the beginning:array_pop($vars); //Delete from the end:
Array Question: Are you there?
//For key :
boolean array_key_exists($key , $vars );
//For value :
boolean in_array($value , $vars );Array question: If you are here, tell me where you are
$key array_search(&value, $vars);Array question: Show me your address book
$keys = array_keys($vars); //Return key value array
$values= array_keys($vars); //Return key value array
//Return key
while( $key=key($vars) )
{
next($vars);
}
//Return value
while( $value=current($vars) )
{
next($vars);
}
The method of taking next() is good, you can also continue positioning~
$value prev($vars) //Previous
$value end($vars) //End point, comprehensive view of the array
How big is the total?
Int count($vars) //Return array size
//Recursive counting:
Int count($vars, 1); //The second parameter is how many times does each type of mode selection appear? Find the frequency~
$vars_f array_count_values($vars); I just want to know how many categories there are! Remove duplicates
$vars_u array_unique($vars); Let’s sort it out and take a look
Sort by ASCII code from smallest to largest (default, optional)
sort($vars); //Why did the key values after you sorted the basic sorting change
asort($vars); //If the key value does not change, can the sorting from large to small only be done in reverse?
resort($vars) //A1--a10-a2 is better, or a1-a2-a10? Natural sorting
natsort($vars) Don’t ignore the beauty of sorting because of case
Natcasesort($vars) can’t only sort by content, right? Key sorting
?ksort();
It’s time to DIY~
usort ($vars , 'myFunction');
//The return of myFun(): less than - negative number, equal to -0, greater than - positive number
This array structure is not working, I want to change it!
Merge:
array_merge(&vars1, $vars2);Recursively merge:
array_merge_recursive(&vars1 , $vars2);
//During recursion, the same value will be merged into an array and appended
array_combine(&vars1 , $vars2);Connect
array_merge(&vars1 , $vars2);Divide 1:
$vars_r = array_slice($vars, 4);//Remove the first four divisions 2:
$vars_r = array_slice($vars,2,-2); //Remove the first 2 and the last 2. Divide the original array unchanged
Split
$vars_r = array_slice($vars, 4,-2); Original array $vars, retain the first 4
The returned $vars_r has the last 2
Intersection:
$vars_r = array_intersect($vars1, $vars2,$vars3); Difference set: the first one has it, the others don’t
$vars_r = array_diff($vars1, $vars2,$vars3);Shuffling of array elements~ :
Shuffle($vars);Sum
$sum = array_sum($vars);
Excerpted from matter605924657