Home > Backend Development > PHP Tutorial > You must know the commonly used built-in functions in PHP

You must know the commonly used built-in functions in PHP

怪我咯
Release: 2023-03-11 17:12:01
Original
1388 people have browsed it

//================================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");

$date=date("Y-m-d H:i:s");//With Hours, minutes and seconds

//include,include_once.require,require_once

//require("file.php") Before the PHP program is executed, the file specified by require will be read in. An error is 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");//Formatting only, return 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

//================================ Common characters String function================================

//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 , concatenate each element in the following array and return a string.

$str=implode("-",array("a","b","c"));

//String splitting method, returns an array, using the The characters in one parameter are used to split the following string, and the characters before, after, and between the specified characters are intercepted. 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 strings, a null 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);

//Eliminate the left side of the string spaces at the beginning, and returns

//If there is a second parameter, the spaces at the beginning on the left are removed and the string in the second parameter is removed

$str=ltrim(" a asd ","a");

//Remove the spaces at the beginning of the right side of the string

$str=rtrim(" asd ");

//Remove the first Strings starting with the second parameter on both sides of a string are eliminated. 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");

//From the first string The length (number) of characters starting from the specified position in the parameter. The first character position in the string is calculated from 0.

//If the second parameter is negative, how long is the string starting from the last number at the end of the string. The last character at the end counts as -1, and the interception direction is always from left to right

$str=substr("abcdefgh",0,4);

//Change the third parameter The first parameter string is replaced with the second parameter string

$str=str_replace("a","","abcabcAbca");

//The usage is the same as str_replace, except Case-insensitive

//$str=str_ireplace("a"," ","abcabcAbca");

//Return the characters of the string in brackets All uppercase string

$str=strtoupper("sdaf");

//Convert the first string in brackets to uppercase and return

$str =ucfirst("asdf");

//Use echo to print the string in the brackets on the web page, and the string in the brackets will be printed out as it is, including the label characters

$ str=htmlentities("
");

//Return the number of times the second parameter string appears in the first string

$int=substr_count("abcdeabcdeablkabd" ,"ab");

//Return the position where the second string appears for the first time in the first string, the first character position is counted as 0

$int=strpos ("asagaab","ab");

//Return the position where the second string last appears in the first string, the first character position is counted as 0

$ int=strrpos("asagaabadfab","ab");

//Intercept and return the string from parameter two that appears first from left to right in parameter one to the last character of parameter one

$str=strstr("sdafsdgaababdsfgs","ab");

//Interception returns the last parameter two from left to right in parameter one to the last parameter one Character string

$str=strrchr("sdafsdgaababdsfgs","ab");

//Add "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 (the number of single characters ), parameter three is the specified padding string, do not write the default space

//Parameter four padding position, 0 is padded at the beginning of the left side of parameter one, 1 is padded at the right side, and 2 is padded at the beginning of both sides. If not written, it will be padded at the beginning on 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 turn. If the first pair is different, if the first pair is greater than the second parameter, 1 will be returned. Otherwise, -1 will be returned. If the two strings are exactly the same, 0 will be returned.

$int1=strcmp("b","a");

//Return the formatted number format of the first parameter, and the second parameter is to retain a few decimal places, Parameter three is to replace the decimal point with parameter three. Parameter four is what characters are used to separate every three digits in the integer part.

//If the last three parameters are not written, the decimal part will be removed by default, and the integers will be separated every three Separate the three digits with commas. 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 the array element of the second parameter There is the first parameter element

$bool=in_array("b",$arr);

//Returns a new array composed of all the key values ​​​​of the array in brackets. The original array does not change

$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 values ​​​​increment from 0 and the original array remains unchanged

$array=array_values($arr);

//Return the key value pointed to by the current array pointer

$key=key($arr);

//Return the element value pointed to by the current array pointer

$value=current($arr);

//Return the key value and element value of the element pointed to by the current array pointer, Then push the pointer to the next bit, and finally the pointer points to an empty element and returns empty

//There are four fixed key values ​​in the returned array. The element values ​​corresponding to the return element are the key value and the element. Value, where 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 moves

$value=next($arr);

//Push the array pointer up One bit, and then return the element value pointed to after the pointer moves

$value=prev($arr);

//Let the array pointer reset to point to the first element and return the element value

$value=reset($arr);

//Point the array pointer to the last element and return the last element value

$value=end($ arr);

//Append the parameters after the first parameter as elements to the end of the first parameter array, the index starts from the smallest unused value, and returns the subsequent array length

$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 is

$int=array_unshift($arr,"t1","t2");

//Return to extract the last element value from the end 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 value of the array, 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, add the third parameter as an element to the end of the first parameter array, and the index will start from the smallest unused value and return , the original array does not change

$array1=array_pad($arr,10,"t10");

//Return a new array with the redundant duplicate elements in the original array removed. The original array The array does not change

$array=array_unique($array1);

//Break the original array key value and re-sort the Asker code value of the element value from small to large, and the index is from the number 0 Start recalculating

$int=sort($array);

//In contrast to sort, re-sort in descending order of the element value Asko code value, and re-calculate the index from 0

$int=rsort($array);

//Returns an 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 Consistent, 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 it. The first element position of the array is 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 in the array. 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 default. 1

$array=range(3,9,2);

//Rearrange the corresponding relationship between the original array elements and the corresponding key values ​​randomly and return true and false

$bool=shuffle($arr);

//Calculate the sum of all numeric element values ​​in the array

$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. True is retained, and the default is false.

$array=array_chunk(array("a"=>"a" ,"b","c","d","e","f","g","h"),2,true);

//json_encode() converts the array into JSON format string return

$arr ​​= array('k1'=>'val1','k2'=>'val2','k3'=>array('v3','v4 '));

echo $encode_str = json_encode($arr);

//json_decode() converts a JSON format string into an object that can be coerced into an array and returns, JSON format characters Double quotes must be used when the keys and values ​​in the string need to be enclosed in quotes

$decode_arr = (array)json_decode($encode_str);

var_dump($decode_arr);

The above is the detailed content of You must know the commonly used built-in functions in PHP. For more information, please follow other related articles on the PHP Chinese website!

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template