Home > php教程 > php手册 > body text

Summary of commonly used functions in PHP

WBOY
Release: 2016-08-30 09:21:12
Original
923 people have browsed it

Generally speaking, string, array, and database class functions are relatively commonly used categories.


//==================================Time and date============ ================

//y returns the last two digits of the year, the four-digit number of year Y, the number of month m, and the English month of M. D The number of the month, D The day of the week in English
$date=date("Y-m-d");

//include,include_once.require,require_once
//require("file.php") will be generated before the PHP program is executed First read in the imported file specified by require. If an error occurs, it will be fatal.
//include("file.php") can be placed anywhere in the PHP program. The file specified by include will not be read until the PHP program is executed. If an error occurs, it will prompt

//======= ========================Output printing======================== =======
//sprintf("%d","3.2") ;//Only formatting, returns the formatted string, no output.
//printf("%d","3.2") ;//Formatting and output
//print("3.2") ;//Only output
//echo "nihao","aa";/ /Can output multiple strings
//print_r(array("a","b","c"));//Display the key values ​​and elements of the array in sequence

//======== ========================Commonly used string functions====================== =========

//Get the length of the string, how many characters there are, spaces are also counted
$str=" sdaf sd ";
$len=strlen($str);

//Use The string in the first parameter is concatenated with each element in the subsequent array to return a string.
$str=implode("-",array("a","b","c"));

//String splitting method, returns an array, and uses the characters in the first parameter to split the following String, intercepts before, after and between the specified characters. If the specified character is at the beginning or end, the element at the beginning or end of the returned array will be an empty string
//If it is not divided into a string, an empty value will be returned to the corresponding element of the array. . The last limit returns the length of the array. If there is no limit, it will continue to be divided.
$array=explode("a","asddad addsadassd dasdadfsdfasdaaa",4);
//print_r($array);

//Remove the leading spaces on the left side of the string and return
//If there is a second The parameter is to remove the spaces starting on the left instead of removing the string in the second parameter
$str=ltrim("a asd ","a");

//Remove the spaces starting on the right of the string
$str= rtrim(" asd ");

//Remove the strings starting with the second parameter on both sides of the first string. If there is no second parameter, the spaces at the beginning of both sides of the string will be removed by default
$str=trim(" sdsdfas ","a");

//How long to take from the specified position in the first parameter of the string (How many) characters, the first character position in the string is calculated from 0.
//If the second parameter is negative, the length of the string will be taken starting from the last number at the end of the string. The last character at the end is counted as -1, and the interception direction is always from left to right
$str=substr("abcdefgh",0,4);

//Use the first parameter string of the third parameter with the parameter Two string replacement
$str=str_replace("a","","abcabcAbca");
//The usage is the same as str_replace, but it is not case sensitive
//$str=str_ireplace("a"," ", "abcabcAbca");

//Return the string in which the characters in the string in brackets are all capitalized
$str=strtoupper("sdaf");

//Change the first string in brackets to uppercase and return
$str=ucfirst("asdf");

//When you use echo to print the string in the brackets on the web page, the string in the brackets will be printed out as it is, including the label characters
$str=htmlentities("< br/>");

//Return the number of times the second parameter string appears in the first string
$int=substr_count("abcdeabcdeablkabd","ab");

//Return the second The position of the first character string at the first occurrence of the first string. The first character position is counted as 0
$int=strpos("asagaab","ab");

//Return the position of the second string at the The last occurrence position of a string, the first character position is counted as 0
$int=strrpos("asagaabadfab","ab");

//Interception returns the first occurrence from left to right in parameter one The string from parameter two to the last character of parameter one
$str=strstr("sdafsdgaababdsfgs","ab");

//Intercept and return the last parameter two that appears from left to right in parameter one String to the last character of parameter one
$str=strrchr("sdafsdgaababdsfgs","ab");

//Add ""
$ to each character in parameter two before the same character in parameter one str=addcslashes("abcdefghijklmn","akd");

//Fill the string of parameter one to the length specified by parameter two (number of single characters). Parameter three is the specified filled string, not Write the default space
//Parameter four filling position, 0 is filled at the beginning of the left side of parameter one, 1 is filled at the beginning of the right side, and 2 is filled at the beginning of both sides. If not written, it will be padded at the beginning of the right side by default.
$str=str_pad("abcdefgh",10,"at",0);

//Compare the Asker code values ​​of the corresponding characters in the two strings in sequence. The first pair is different. If parameter one is greater than parameter two, 1 is returned, otherwise -1 is returned. If the two strings are exactly the same, 0 is returned.
$int1=strcmp("b","a");

//Return the first parameter Formatted number format, the second parameter is how many decimals should be retained, the third parameter is to replace the decimal point with parameter three, and the fourth parameter is what characters are used to separate every three digits of the integer part
// If the next three parameters are not written, the decimal part will be removed by default, and the integer will be separated by commas every three digits. Parameter three and parameter four must exist at the same time
$str=number_format(1231233.1415,2,"d","a");

//================ ===============Commonly used array methods================================

$arr=array("k0"=>"a","k1"=>"b","k2"=>"c");

//Return the number of array elements
$int= count($arr);

//Determine whether there is the first parameter element in the array element of the second parameter
$bool=in_array("b",$arr);

//Return all key values ​​of the array in brackets The new array formed does not change the original array
$array=array_keys($arr);

//Determine whether the array of the second parameter contains the key value of the first parameter and return true or false
$bool=array_key_exists(" k1",$arr);

//Return a new array composed of all element values ​​in the original array. The key value starts from 0 and increases automatically. The original array remains unchanged
$array=array_values($arr);

//Return The key value pointed to by the current array pointer
$key=key($arr);

//Returns the value of the element pointed by the current array pointer
$value=current($arr);

//Returns the value of the element pointed by the current array pointer An array composed of key values ​​and element values, and then push the pointer to the next bit. Finally, the pointer points to an empty element and returns empty
//There are four element values ​​corresponding to fixed key values ​​in the returned array, which are the returned elements. Key value and element value, among which 0, 'key' key value corresponds to the returned element key value, 1, 'value' key value corresponds to the returned element value
$array=each($arr);

//First Push the array pointer to the next bit, and then return the element value pointed to after the pointer is moved
$value=next($arr);

//Push the array pointer to the previous bit, and then return the element value pointed to after the pointer is moved
$ value=prev($arr);

//Reset the array pointer to point to the first element and return the element value
$value=reset($arr);

//Reset the array pointer to point to the last element, and Return the last element value
$value=end($arr);

//Add the parameters after the first parameter as elements to the end of the first parameter array, and the index starts from the smallest unused value Count, return the length of the array
$int=array_push($arr,"d","dfsd");

//Add all parameters after the first parameter array as elements to the beginning of the first parameter array, The key value is re-accumulated from the first element with 0, the original non-numeric key value remains unchanged, the sorting position of the original element remains unchanged, and the array length after return
$int=array_unshift($arr,"t1","t2 ");

//Returns to extract the last element value from the tail of the array, and remove the last element from the original array
$value=array_pop($arr);

//array_pop On the contrary, extract and return the first element of the array value, and remove the first element from the original array
$value=array_shift($arr);

//Let the first parameter array reach the length of the second parameter value, and add the third parameter as an element to the first At the end of the parameter array, the index starts from the smallest unused value and is returned. The original array does not change
$array1=array_pad($arr,10,"t10");

//Returns a redundant duplicate in the original array The new array with elements removed, the original array remains unchanged
$array=array_unique($array1);

//Break the original array key values ​​and re-sort the Asker code values ​​of the element values ​​from small to large, and the index starts from the number 0 Recalculate
$int=sort($array);

//The opposite of sort, re-sort the element value in descending order of Asko code value, and recalculate the index from 0
$int=rsort($array);

//Return the array in which each element value in the first parameter array is paid as a key value to the second parameter array in turn. The length of the two arrays must be the same, and 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 parameter array, intercept the array key value + element starting from the second parameter value position to the third parameter value length and return, the first one in the array Element positions are counted from 0
$array=array_slice($arr,2,1);

//The interception function is the same as array_slice(), except that the intercepted part is removed from the original array
$array=array_splice($arr,2, 1);

//Take the first parameter as the first element, increment the value of parameter three each time, and then store it in the array as an element after incrementing, until the value reaches the value of parameter two and save it to the array Stop and return this array
//Parameter one, parameter two can be a number or a single character. A single character is calculated according to the ASCO code value. If the third parameter is not written, it will increment by 1 each time by default
$ array=range(3,9,2);

//Rearrange the correspondence between the original array elements and the corresponding key values ​​randomly and return true or false
$bool=shuffle($arr);

//Calculate all the elements in the array The sum of numeric element values
$int=array_sum(array("a",2,"cssf"));

//Split an array into new array blocks. Each element of the new array is an array. The number of elements in each element of the new array is determined by parameter two
//The third parameter determines whether the key value of the element retains the original key value, which can be left unwritten, true means retained, and the default is false, which is not retained
$array=array_chunk(array("a"=>"a","b","c","d","e","f","g","h"),2,true);

Related labels:
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 Recommendations
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!