PHP became a monk halfway (2)_PHP tutorial

WBOY
Release: 2016-07-13 17:50:57
Original
829 people have browsed it

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 output

echo $myArray["This is OK"]; //will output "This is really OK"


Index automatically incremented

$myArray[] =0;

$myArray[] =1;

$myArray[] =2;

$myArray[] =3;

echo $myArray[3]; //will output 3


The powerful list() can extract arrays---involves the matching function explore(), which will be discussed later~


Range() function also has a filtering effect

$myArray = range("a", "z",3); //From "a" to "z", output every 3

//Output a d g j m p s v y

Array expansion www.2cto.com

Return the value corresponding to the index:
print_r($myArray);

Judge whether it is an array:

boolean is_array(mixed var);

Hybrid --- queue + stack --- very convenient operation before and after

array_unshift($vars, $key, $value); //Add

from the beginning

array_push($vars, $key, $value); //Add

from the end

array_shift($vars); //Delete from the beginning:

array_pop($vars); //Delete from the end:


Four Array Questions --- Query

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

print_r($keys);Array question: Who lives here?

$values= array_keys($vars); //Return key value array

When the index is not a number, the for(int i=0;;i++) I have been using is laid off...

//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 reset($vars) //Starting point

$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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478243.htmlTechArticleFunction is essential for every programming language. I have to understand what is different about PHP. The most sharp thing is return Type, you don’t need to indicate it in the function declaration. The only thing you need to pay attention to is that you have to use the corresponding type...
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