First of all, copy came, you crossing do not spit groove, I was left to review and supplement the knowledge point.
PHP data type includes 8 types of
The corresponding judgment functions are: Is_bool (), Is_int (), Is_integer (), Is_long () (same as the integer type), Is_real () (same as the Judgment float), is_float (), is_string (), Is_ Array (), Is_object (), Is_null () to detect if the variable is null
Other common functions
The Array_values () function can return all values in an array and give it a numeric index
<?php$arr = [0,1,2,3];unset ($arr [1]), $arr = Array_values ($arr);/*array (size=3) 0 = int 0 1 = int 2
2 = int 3 */var_dump ($arr);? >
The list () function assigns the values in the array to the variable
List ($a, $b, $c) = [1,2,3];echo $a;//1echo $b;//2echo $c;//3//receives only one list (,, $power) = [1,2,3];echo $power;//3//list () cannot function to String list ($bar) = "ABCDE"; Var_dump ($bar); Null
A callback function refers to the function name of a function passed as a parameter to another function
function Demo ($num, $n) {for ($i =0; $i < $num; $i + +) { if ($n ($i)) { echo $i. ' <br> '; }}} function test ($i) { if ($i%5) { return true; }} Demo ("Test");
Recursive functions
<?php function Test ($n) { echo $n; if ($n >0) { Test ($n-1); } echo $n; } Test (5);//543210012345?>
Key-value operation
array_values:array_values () returns all the values in the input array and gives them a numeric index (described in the document)
Array_keys:Array_keys () returns the key name of the number or string in the input array
Array Array_keys (array $array [, Mixed $search _value [, bool $strict = false]])
If an optional parameter, Search_value, is specified, only the key name of the value is returned. Otherwise, all the key names in the input array will be returned
If an optional parameter, strict, is specified to determine if a strict comparison should be used when searching (= = =)
<?php$array = Array (0 = +, "color" = "red"),//array ([0] = 0 [1] = color) print_r (Array_keys ($array ); $array = Array ("Blue", "Red", "green", "Blue", "blue");//Array ([0] = 0 [1] = 3 [2] = 4) print_r (array_k Eys ($array, "Blue")); $array = Array ("color" = = Array ("Blue", "Red", "green"), "size" = = Array ("small", " Medium "," large ")); Array ([0] = color [1] = size) Print_r (Array_keys ($array));? >
In_array:in_array-Checks if a value exists in the array
BOOL In_array (mixed $needle, array $haystack [, bool $strict = FALSE])
If the value of the third parameter strict is TRUE then the In_array () function also checks whether the needle type is the same as in haystack
<?php$os = Array ("Mac", "NT", "Irix", "Linux"), if (In_array ("Irix", $os)) { //got Irix echo "Got Irix";} if (In_array ("Mac", $os)) { echo "Got mac";}? >
array_flip:array_flip-switches the keys and values in the array, returns the swapped array if unsuccessful, and returns NULL if it fails
Array Array_flip (array $trans)
Array_flip () returns a reversed array, such as the key name in trans becomes a value, and the value in trans is a key name
If the same value occurs more than once, the last key will be its value, and all the others are lost.
[Note that the value in]trans needs to be able to be a valid key name, such as an integer or string. If the value is of the wrong type, a warning will be issued and the problematic key/value pair will not be reversed
<?php$trans = Array ("a" = = 1, "b" = 1, "c" = 2); $trans = Array_flip ($trans);//array ([1] = b [2] = = c) Print_r ($trans);? >
array_reverse:array_reverse-Returns an array of cells in reverse order
Array array_reverse (array $array [, bool $preserve _keys = false])
If the parameter Preserve_keys is set to TRUE, the key of the number is preserved. Non-numeric keys are not affected by this setting and are always retained
array_count_values:Number of occurrences of all values in the array_count_values-statistics array
Array array_count_values (array $input)
<?php$array = Array (1, "Hello", 1, "World", "Hello");//array ([1] = 2 [Hello] + 2 [world] = 1) print_r (arr Ay_count_values ($array));? >
Array_unique:array_unique-The duplicate value in the divisor group
Array Array_unique (array $array [, int $sort _flags = sort_string])
[note] The key name remains unchanged. Array_unique () Sorts the values first as a string, then retains only the first key name encountered for each value, and then ignores all subsequent key names. This does not mean that the key name of the first occurrence of the same value in an unordered array is preserved
<?php$input = Array ("A" = "green", "Red", "b" = "green", "Blue", "Red"), $result = Array_unique ($input);//array ([A] = green [0] = red [1] = blue) Print_r ($result);? >
array_sum:array_sum-computes the and of all the values in the array, and Array_sum () returns all the values in the array and the results of the integers or floating-point numbers
<?php$a = Array (2, 4, 6, 8);//sum (a) = 20echo "sum (a) =". Array_sum ($a). "\ n"; $b = Array ("a" = = 1.2, "b" = = 2.3, "c" = 3.4);//sum (b) = 6.9echo "sum (b) =". Array_sum ($b). "\ n";? >
array_merge:array_merge-Merge one or more arrays
array_intersect:array_intersect-computes the intersection of an array
Array Array_intersect (array $array 1, array $array 2 [, array $ ...])
Array_intersect () returns an array that contains all the values in Array1 that also appear in all other parameter arrays
[note] The key name remains unchanged
<?php$array1 = Array ("A" = "green", "Red", "Blue"), $array 2 = Array ("b" = = "green", "yellow", "red"); $result = AR Ray_intersect ($array 1, $array 2);//array ([A] = green [0] = red) print_r ($result);? >
Array_diff:array_diff-The difference set of the computed array
Array Array_diff (array $array 1, array $array 2 [, array $ ...])
The comparison returns the values in the array1 but not in the array2 and any other parameter arrays
<?php$array1 = Array ("A" = "green", "Red", "Blue", "Red"), $array 2 = Array ("b" = "green", "yellow", "red"); $resu lt = Array_diff ($array 1, $array 2);//array ([1] = blue) Print_r ($result);? >
Array_fill:array_fill-fills the array with the given value, returning the populated array
Array array_fill (int $start _index, int $num, mixed $value)
Array_fill () populates an array of num entries with the values of the value parameter, starting with the key name specified by the Start_index parameter
php$a = Array_fill (5, 6, ' banana '); $b = Array_fill ( -2, 4, ' pear ');//array ([5] = = Banana [6] = = Banana [7] = b Anana [8] = banana [9] = banana [ten] = banana) print_r ($a);//array ([-2] = = Pear [0] = = Pear [1] = = Pear [2] = pear) print_r ($b);? >
Stacks and queues
array_pop:array_pop-pops the last cell of the array (out of the stack)
Mixed Array_pop (array & $array)
Array_pop () pops up and returns the last cell of the array, minus one of the array's length. Null is returned if array is empty (or not an array)
<?php$stack = Array ("Orange", "banana", "apple", "raspberry"), $fruit = Array_pop ($stack);//array ([0] = orange [1] = Banana [2] = apple) print_r ($stack);? >
Array_push:array_push-pushes one or more cells into the end of the array (into the stack), returning the number of elements of the array after processing
int Array_push (array & $array, mixed $var [, mixed $ ...]
Array_push () treats the array as a stack and presses the incoming variable into the end of the array. The length of the array will be incremented according to the number of variables in the stack
<?php$stack = Array ("Orange", "banana"); Array_push ($stack, "apple", "raspberry");//array ([0] = orange [1] = b Anana [2] = apple [3] = Raspberry) Print_r ($stack);? >
array_shift:array_shift-moves the cell at the beginning of the array to a group
Mixed Array_shift (array & $array)
Array_shift () Moves the first cell of the array out and returns as a result, reducing the length of the array and moving all other cells forward one bit. All numeric keys are changed from zero to start, and the text key name is unchanged
<?php$stack = Array ("Orange", "banana", "apple", "raspberry"), $fruit = Array_shift ($stack);//array ([0] = = Banana [ 1] = Apple [2] = Raspberry) Print_r ($stack);? >
Array_unshift:array_unshift-inserts one or more cells at the beginning of the array, returning the number of new cells in array arrays
int Array_unshift (array & $array, mixed $var [, mixed $ ...]
Array_unshift () Inserts the incoming cell at the beginning of the array. Note that the units are inserted as a whole, so incoming cells will remain in the same order. All numeric key names are modified to count back from zero, and all text key names remain unchanged
<?php$queue = Array ("Orange", "banana"); Array_unshift ($queue, "apple", "raspberry");//array ([0] = + apple [1] => ; Raspberry [2] = orange [3] = banana) Print_r ($queue);? >
Trim (): Use this function to return a string that strips the end of a string
LTrim ():ltrim function to remove white space characters (or other characters) at the beginning of a string
RTrim ():RTrim function to remove white space characters (or other characters) at the end of a string
strtolower ():strtolower-Converts a string to lowercase
strtoupper ():strtoupper-convert string to uppercase
Ucfirst ():ucfirst-Converts the first letter of a string to uppercase
ucwords ():ucwords-Converts the first letter of each word in a string to uppercase
Htmlspecialchars (): Converts the specified special symbol into an entity
Addslashes (): Use a backslash to reference a string, return a string
Stripslashes (): dereference a reference string
Strrev ():strrev-Reverse string
String Strrev (String $string)
<?phpecho Strrev ("Hello world!"); Output "!dlrow Olleh"?>
strlen ():strlen-Get string length
MD5 (): Computes the MD5 hash value of a string
strstr (): finds the first occurrence of a string that returns the haystack string from the beginning of the first occurrence of the needle to the end of the haystack
String Strstr (String $haystack, mixed $needle [, bool $before _needle = false])
Before_needle if TRUE,STRSTR () returns the portion of the needle before the position in haystack
<?php$email = ' [email protected] '; $domain = Strstr ($email, ' @ '); echo $domain;//@example. Com$user = Strstr ($ email, ' @ ', true); Echo $user; Name?>
Strpos ():Strpos () find where the string first appears
Mixed Strpos (String $haystack, mixed $needle [, int $offset = 0])
<?php//ignores the position offset before the character is searched $newstring = ' abcdef abcdef '; $pos = Strpos ($newstring, ' a ', 1); $pos = 7, not 0?>
Strrpos ():Strrpos () calculates the position of the last occurrence of the specified string in the target string
int Strrpos (string $haystack, string $needle [, int $offset = 0])
<?php$foo = "0123456789a123456789b123456789c"; Var_dump (Strrpos ($foo, ' 7 ',-5)); Starting at the 5th position of the trailer //Result: Int (var_dump) ($foo, ' 7 ', +)); Starting from the 20th position find //Result: Int (var_dump) ($foo, ' 7 ', ()); Result: bool (false)?>
substr ():substr () returns a substring of a string
String substr (string $string, int $start [, int $length])
<?php$rest = substr ("abcdef",-1); Return "F" $rest = substr ("ABCdef",-2); Returns "EF" $rest = substr ("ABCdef",-3, 1); Return "D"? ><?php$rest = substr ("abcdef", 0,-1); Return "ABCDE" $rest = substr ("ABCdef", 2,-1); Return to "CDE" $rest = substr ("ABCdef", 4,-4); Return "" $rest = substr ("ABCdef",-3,-1); Back to "de"?>
str_replace ():str_replace () returns a string or array. The string or array is the result of replacing all search in subject with replace
Mixed Str_replace (mixed $search, mixed $replace, mixed $subject [, int & $count])
If count is specified, its value is set to the number of times the substitution occurred
<?php//assignment: <body text= ' black ' > $bodytag = Str_replace ("%body%", "Black", "<body text= '%body% ' >");//Assignment: Hll Wrld F php$vowels = Array ("A", "E", "I", "O", "U", "a", "E", "I", "O", "U"), $onlyconsonants = Str_replace ($vowels, "", "Hello World of PHP");//assignment: You should eat pizza, beer, and ice cream every day$phrase = "You should eat fruits, veg Etables, and fiber every day. "; $healthy = Array ("Fruits", "vegetables", "fiber"), $yummy = Array ("Pizza", "beer", "ice Cream"), $newphrase = Str_ Replace ($healthy, $yummy, $phrase);//assignment: 2$str = Str_replace ("ll", "" "," Good Golly Miss Molly! ", $count); Echo $count; >
Split: Explode ()
Explode () splits another string with one string, returns an array of strings, each of which is a substring of string, separated by the delimiter as a boundary point
Array explode (string $delimiter, string $string [, int $limit])
If the limit parameter is set and is a positive number, the returned array contains a maximum of limit elements, and the last element will contain the remainder of the string, and if the limit parameter is negative, all elements except the last-limit element are returned, and if limit is 0, It will be treated as 1
<?php//example 1$pizza = "Piece1 piece2 piece3 piece4 piece5 piece6"; $pieces = Explode ("", $pizza); Echo $pieces [0]; Piece1echo $pieces [1]; piece2//example 2$data = "foo:*:1023:1000::/home/foo:/bin/sh"; list ($user, $pass, $uid, $gid, $gecos, $home, $shell) = Explo De (":", $data); Echo $user; Fooecho $pass; *?>
implode (): Implode () converts the value of a one-dimensional array to a string
String implode (string $glue, array $pieces) string implode (array $pieces)
<?php$array = Array (' LastName ', ' email ', ' phone '), $comma _separated = Implode (",", $array); Echo $comma _separated; lastname,email,phone//empty string when using an Empty array:var_dump (implode (' Hello ', Array ())); String (0) ""?>
Common functions in PHP