Drill down to the usage summary of common php functions

  • 2020-06-12 08:39:21
  • OfStack

As shown below:


<?php
//=============================== Time to date ===============================
//y Go back to the last two of the year, Y years 4 Digits, m Monthly figures, M Month English. d What's the date of the month? D What day of the week 
$date=date("Y-m-d");
$date=date("Y-m-d H:i:s");// Minutes of the zone 
//include,include_once.require,require_once
//require("file.php")  in PHP The program is read in before execution require An error in the specified imported file can be fatal. 
//include("file.php")  Can be placed in PHP Anywhere in the program, PHP Don't read in until the program executes include Specifies the file to be imported and will be prompted if an error occurs 
//=============================== The output to print ===============================
//sprintf("%d","3.2") ;// Format only, return the formatted string, no output. 
//printf("%d","3.2") ;// Format and output 
//print("3.2") ;// Only the output 
//echo "nihao","aa";// Multiple strings can be output 
//print_r(array("a","b","c"));// Displays the array's keys and elements in turn 
//=============================== Common string functions ===============================
// Gets the length of the string, how many characters, Spaces 
$str=" sdaf sd ";
$len=strlen($str);
// In the first 1 String, concatenates each element in the subsequent array, returns 1 A string. 
$str=implode("-",array("a","b","c"));
// String segmentation method, returns 1 An array of, let's say, a row 1 If the specified character is at the beginning or the end, the element at the beginning or end of the array is returned as an empty string 
// Returns the corresponding element of the array without splitting into a string 1 A null value. The last 1 Returns the length of the array without limitation, then 1 I'm going to go straight down. 
$array=explode("a","asddad addsadassd dasdadfsdfasdaaa",4);
//print_r($array);
// Removes the space at the beginning of the string to the left , And return 
// If you have the first 2 The parameter is to remove the left leading space and replace it with the first one 2 A string in an argument 
$str=ltrim("a asd ","a");
// Removes the whitespace at the beginning of the string to the right 
$str=rtrim(" asd ");
// The first 1 The string is preceded by a 2 Culling of strings beginning with arguments. If not the first 2 By default, the whitespace at the beginning of both sides of the string is removed 
$str=trim(" sdsdfas ","a");
// From string control to 1 The specified position in an argument begins with the length (number) of characters, the first in a string 1 Two character positions from 0 Calculate. 
// If the first 2 How long a string is taken from the last but one of the strings. At the end of the last 1 A character is -1 Always intercept from left to right 
$str=substr("abcdefgh",0,4);

// Will be the first 3 Parameter 1 Parameter string with parameters 2 String substitution 
$str=str_replace("a","","abcabcAbca");
// with str_replace Same usage, but case insensitive 
//$str=str_ireplace("a"," ","abcabcAbca");
// Returns a string with all uppercase characters of the string in parentheses 
$str=strtoupper("sdaf");
// Put the first in parentheses 1 Returns a string that is capitalized 
$str=ucfirst("asdf");
// with echo When you print the string in parentheses on the web page, print the string in parentheses, including the label character 
$str=htmlentities("<br/>");
// Returns the first 2 The number of arguments in the string 1 Number of occurrences of a string 
$int=substr_count("abcdeabcdeablkabd","ab");
// Returns the first 2 The string is at number one 1 String control 1 Position of secondary occurrence, no 1 Two character positions count 0
$int=strpos("asagaab","ab");
// Returns the first 2 The string is at number one 1 At the end of a string 1 Position of secondary occurrence, no 1 Two character positions count 0
$int=strrpos("asagaabadfab","ab");
// Intercept return parameter 1 Center from left to right 1 The number of parameters that appear 2 To the parameter 1 The last 1 A string of two characters 
$str=strstr("sdafsdgaababdsfgs","ab");
// Intercept return parameter 1 Center from left to right last 1 The number of parameters that appear 2 To the parameter 1 The last 1 A string of two characters 
$str=strrchr("sdafsdgaababdsfgs","ab");
// The parameter 2 Each of the 1 Two characters in a parameter 1 Prefix the same character in "\"
$str=addcslashes("abcdefghijklmn","akd");
// The parameter 1 The string to populate the parameter 2 The specified length (number of single characters), parameter 3 Does not write the default space for the specified populated string 
// parameter 4 Filling position, 0 In the parameter 1 Fill in at the left, 1 At the beginning on the right, 2 Both sides start at the same time. Do not write the default fill at the beginning of the right 
$str=str_pad("abcdefgh",10,"at",0);
// Compare the two strings in turn to the corresponding character asker code value, no 1 For not 1 Like if parameters 1 Greater than parameter 2 In the back 1, Conversely return -1 , two strings complete 1 The sample return 0
$int1=strcmp("b","a");
// Returns the first 1 The number format after the formatting of the parameters, the 2 Number of parameters to reserve a few decimal, parameters 3 To replace the decimal point with a parameter 3 , the parameter 4 Is the integer part of each 3 What characters are bits separated by 
// behind 3 If none of the arguments are written, the decimal part is deleted by default, and every integer is separated 3 A comma , Segmentation. parameter 3 , the parameter 4 Must be present at the same time 
$str=number_format(1231233.1415,2,"d","a");
//=============================== Common array methods ===============================
$arr=array("k0"=>"a","k1"=>"b","k2"=>"c");
// Returns the number of elements in the array 
$int=count($arr);
// Determine the first 2 Parameter has a row in the array element 1 Parameter elements 
$bool=in_array("b",$arr);
// Returns a new array of all the key values of the array in parentheses. The original array does not change 
$array=array_keys($arr);
// Determine the first 2 Is there a row in the array of parameters 1 The key value of the parameter, return true or false 
$bool=array_key_exists("k1",$arr);
// Returns a new array of all the element values from the original array 0 I'm going to start multiplying by myself, but I'm not going to change the array 
$array=array_values($arr);
// Returns the key value of the current array pointer 
$key=key($arr);
// Returns the value of the element to which the current array pointer points 
$value=current($arr);
// Returns the current array pointer to the element's key and array of element values, and pushes the pointer down 1 Bit, and finally the pointer is pointing to 1 Null returns null 
// Returns an array of 4 The element value corresponding to the fixed key value is the key value of the return element and the element value respectively, where 0,'key' Each of the key values corresponds to the element's key value, 1,'value' The key value corresponds to the element value returned 
$array=each($arr);
// First, push the array pointer down 1 Bit, and then returns the value of the element to which the pointer moved 
$value=next($arr);
// Push the array pointer up 1 Bit, and then returns the value of the element to which the pointer moved 
$value=prev($arr);
// Set the array pointer to the first 1 Returns the value of the element 
$value=reset($arr);
// Point the array pointer to the end 1 Bit elements, and returns the last 1 An element value 
$value=end($arr);
// Will be the first 1 The parameter after is added as element trace 1 At the end of an array of parameters, the index starts with the smallest unused value and returns the length of the subsequent array 
$int=array_push($arr,"d","dfsd");
// Will be the first 1 After the array of parameters, all parameters are added as elements to the first 1 The key value is at the beginning of an array of parameters 0 From the first 1 The original non-numeric key value remains the same, the sorting position of the original element remains the same, and the length of the array after that is returned 
$int=array_unshift($arr,"t1","t2");
// Returns the last extract from the end of the array 1 The element values, and put the last 1 The elements are removed from the original array 
$value=array_pop($arr);
//array_pop Instead, extract returns the array header 1 The element value, and the head 1 The elements are removed from the original array 
$value=array_shift($arr);
// For the first 1 The parameter array reaches the value 2 Is the length of the parameter value 3 Three parameters are added as elements to the 1 At the end of an array of parameters, the index is counted and returned from the smallest unused value, leaving the original array unchanged 
$array1=array_pad($arr,10,"t10");
// return 1 A new array that removes redundant duplicate elements from the original array, leaving the original array unchanged 
$array=array_unique($array1);
// Break the original array key value to reorder the element value's asker code value from small to large, index from number 0 Began to recount 
$int=sort($array);
// and sort Instead, it is reordered in descending order of the ascot code value size of the element value, indexed from 0 Back gauge 
$int=rsort($array);
// Return to the first 1 Each of an array of parameters 1 The values of the elements are in turn paid as keys to the parameters 2 Array of arrays, two array lengths must 1 To, the original array does not change 
$array=array_combine(array("a","b","c","d","e"),$arr);
// Merge the two arrays and return the original array unchanged 
$array=array_merge($arr,array("a","b","c"));
// In the first 1 An array of parameters from the 2 The value position of the parameter starts to intercept to the no 3 An array key value of the numeric length of an argument + Element and returns the array number 1 The elements are located from 0 meter 
$array=array_slice($arr,2,1);
// Interception function and array_slice()1 Sample, just delete the intercepts from the original array 
$array=array_splice($arr,2,1);
// Will be the first 1 A parameter as a control 1 Three elements, each self-augmenting parameter 3 The value of, and then as 1 The elements are stored in an array until the value reaches the parameter 2 Is stored in an array and returns the array 
// parameter 1 , the parameter 2 It can be a number, it can be a single character, a single character is calculated as the ascot code value, no 3 Do not write the default parameters per increment 1
$array=range(3,9,2);
// Rearranging the relationships between the original array elements and the corresponding key values returns true or false 
$bool=shuffle($arr);
// Calculates the sum of the values of all numeric elements in an array 
$int=array_sum(array("a",2,"cssf"));

// the 1 The array is split into new blocks, each element of the new array 1 The new array contains several elements per element by parameter 2 decision 
// The first 3 The parameters determine whether the original key value of the element is retained or not. true To preserve, default false Don't keep 
$array=array_chunk(array("a"=>"a","b","c","d","e","f","g","h"),2,true);

//json_encode() Converts an array to JSON Format string return 
$arr = array('k1'=>'val1','k2'=>'val2','k3'=>array('v3','v4'));
echo $encode_str = json_encode($arr);
//json_decode() will JSON Format string converted to an object that can be cast into an array, JSON Format strings must be enclosed in double quotation marks when the key and value need to be enclosed in quotation marks 
$decode_arr = (array)json_decode($encode_str);
var_dump($decode_arr);
?>


Related articles: