Usage of common PHP Functions

Source: Internet
Author: User

Copy codeThe Code is as follows: <? Php
/// ================================================ ======================================
// Y returns the last two digits of the Year. Y indicates the four digits of the Year. m indicates the number of the month. M indicates the English Number of the month. D: The number of months. D: the number of weeks.
$ Date = date ("Y-m-d ");
$ Date = date ("Y-m-d H: I: s"); // time, minute, and second

// Include, include_once.require, require_once
// Require ("file. php") reads the file specified by require before executing the PHP program. If an error occurs, it is fatal.
// Include ("file. php") can be stored in any position of the PHP program. Only when the PHP program is executed can the file specified by include be read. If an error occurs, a prompt is displayed.

// ==================================== Output print ======== ======================================
// Sprintf ("% d", "3.2"); // format only. The formatted string is returned without output.
// Printf ("% d", "3.2"); // format and Output
// Print ("3.2"); // output only
// Echo "nihao", "aa"; // multiple strings can be output.
// Print_r (array ("a", "B", "c"); // displays the key values and elements of the array in sequence.

// ================================ Common string functions ====== ======================================

// Obtain the length of a string, including the number of characters and spaces.
$ Str = "sdaf sd ";
$ Len = strlen ($ str );

// Use the string in the first parameter to connect each element in the following array and return a string.
$ Str = implode ("-", array ("a", "B", "c "));

// String segmentation method. An array is returned. The string is separated by the characters in the first parameter, and the front and back of and between the specified characters are intercepted, if the specified character starts or ends, the element at the beginning or end of the returned array is an empty string.
// Returns a null value to the element corresponding to the array if the string is not split. The last limit returns the length of the array. If there is no limit, the array will be split.
$ Array = explode ("a", "asddad addsadassd dasdadfsdfasdaaa", 4 );
// Print_r ($ array );

// Remove spaces starting with the left of the string and return
// If there is a second parameter, remove spaces starting with the left and replace the string in the second parameter.
$ Str = ltrim ("a asd", "");

// Remove spaces at the right of the string
$ Str = rtrim ("asd ");

// Remove the strings that start with the second parameter on both sides of the first string. If there is no second parameter, spaces starting with both sides of the string are removed by default.
$ Str = trim ("sdsdfas", "");

// The length (number of characters) starting from the specified position in the first parameter of the string. The position of the first character in the string is counted from 0.
// If the second parameter is negative, the length of the string starting from the last few to the end of the string. The last character at the end is-1, and the truncation direction is always from left to right.
$ Str = substr ("abcdefgh", 0, 4 );

// Replace the first parameter string of the third parameter with the string of the second parameter
$ Str = str_replace ("a", "", "abcabcAbca ");
// Use the same method as str_replace, but it is case insensitive.
// $ Str = str_ireplace ("a", "", "abcabcAbca ");

// Returns all uppercase strings of strings in parentheses.
$ Str = strtoupper ("sdaf ");

// Convert the first string in parentheses into uppercase and then return
$ Str = ucfirst ("asdf ");

// Print the character strings in the brackets, including the tag characters, in the original style when printing the strings in the brackets on the web page.
$ Str = htmlentities ("<br/> ");

// Returns the number of times the second parameter string appears in the first string
$ Int = substr_count ("abcdeabcdeablkabd", "AB ");

// Returns the position where the second string appears for the first time. The value of the first character is 0.
$ Int = strpos ("asagaab", "AB ");

// Returns the position of the second string at the last occurrence of the first string. The value of the first character is 0.
$ Int = strrpos ("asagaabadfab", "AB ");

// Intercept the string of the second to the last character of parameter 1 that appears from left to right in return parameter 1
$ Str = strstr ("sdafsdgaababdsfgs", "AB ");

// Intercept the string of the second to the last character of parameter 1 that appears from left to right in the return Parameter
$ Str = strrchr ("sdafsdgaabdsfgs", "AB ");

// Add "\" to each character in parameter 2 before the same character in Parameter "\"
$ Str = addcslashes ("abcdefghijklmn", "akd ");

// Fill the string of parameter 1 with the length specified by parameter 2 (number of single characters). Parameter 3 is the string of the specified filling, with no default space written
// Fill position of parameter 4. 0 is filled at the beginning of parameter 1 on the left, 1 on the right, and 2 on both sides at the same time. If this parameter is left blank, it is filled at the beginning of the right by default.
$ Str = str_pad ("abcdefgh", 10, "at", 0 );

// Compare the values of the two strings in sequence. The first pair is different. If the value in parameter 1 is greater than the return value in parameter 2, the return value is-1, and the return value is 0.
$ Int1 = strcmp ("B", "");

// Return the number format after formatting the first parameter. The second parameter is to retain a few decimals. The third parameter is used to replace the decimal point with the third parameter. The fourth parameter is the character used to separate each three digits of the integer part.
// If none of the following three parameters are specified, the fractional part is removed by default. integers are separated by commas (,) at every three digits. Parameter 3 and parameter 4 must exist at the same time
$ Str = number_format (1231233.1415, 2, "d", "");

// ================================ Common array Methods ==== ======================================

$ Arr = array ("k0" => "a", "k1" => "B", "k2" => "c ");

// Returns the number of array elements.
$ Int = count ($ arr );

// Determine whether the first parameter element exists in the array element of the second parameter
$ Bool = in_array ("B", $ arr );

// Return the new array consisting of all the key values in the array. The original array is not changed.
$ Array = array_keys ($ arr );

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

// Return a new array consisting of all element values in the original array. The key value starts 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 );

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

// Returns an array consisting of the key value and element value of the current array pointer pointing to the element, and then pushes the pointer to the next position. Finally, the pointer points to an empty element and returns NULL.
// The returned array contains the element values corresponding to four fixed key values, which are the key values and element values of the returned elements. The 0 and 'key' values correspond to the return element key values, 1. The 'value' key value corresponds to the returned element value.
$ Array = each ($ arr );

// First push the array pointer to the next position, and then return the element value pointed to after the pointer is moved.
$ Value = next ($ arr );

// Pushes the array pointer to the previous position, and returns the element value pointed to after the pointer is moved.
$ Value = prev ($ arr );

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

// Point the array pointer to the last element and return the value of the last element.
$ Value = end ($ arr );

// Add the parameters after the first parameter to the end of the first parameter array as an element. The index starts from the smallest unused value and returns 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, and the key value is accumulated from the first element with 0, the key value of the original non-numeric value remains unchanged, the sorting position of the original element remains unchanged, and the returned array Length
$ Int = array_unshift ($ arr, "t1", "t2 ");

// Returns the result of extracting the last element value from the end of the array and removing the last element from the original array.
$ Value = array_pop ($ arr );

// Opposite to array_pop, extract an element value from the array header and remove the 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 end of the first parameter array. The index starts from the minimum value and returns the result, the original array does not change
$ Array1 = array_pad ($ arr, 10, "t10 ");

// Return a new array that removes unnecessary duplicate elements from the original array. The original array does not change.
$ Array = array_unique ($ array1 );

// Break the key value of the original array and re-sort the key value of the element value from small to large. The index starts from the number 0.
$ Int = sort ($ array );

// Opposite to sort, the index is recalculated from 0 in descending order of the element value
$ Int = rsort ($ array );

// Return the array that uses each element value in the first parameter array as the key value to be paid to the two arrays. The two arrays must have the same length 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, extract the array key value + element from the second parameter value position to the length of the third parameter value and return it. The first element position of the array is counted from 0.
$ Array = array_slice ($ arr, 2, 1 );

// The Truncation function is the same as that of array_slice (), but the truncation part is removed from the original array.
$ Array = array_splice ($ arr, 2, 1 );

// Use the first parameter as the first element. The value of parameter 3 is automatically added each time and then an element is included in the array, this array is returned until the value reaches the value of parameter 2 and is saved to the array.
// Parameter 1. Parameter 2 can be a number or a single character. If a single character is entered, the value is calculated based on the astic code. If the third parameter is left blank, the value is automatically increased by 1.
$ Array = range (3, 9, 2 );

// Re-randomly arrange the correspondence between the original array element and the corresponding key value to 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 "));

// Divide an array into new array blocks. Each element in the new array is an array. The elements in each element of the new array are determined by parameter 2.
// The third parameter determines whether the element's key value is retained. The value true indicates that the original key value is retained, and the default value false indicates that the original key value is retained.
$ Array = array_chunk (array ("a" => "a", "B", "c", "d", "e", "f", "g ", "h"), 2, true );

// Json_encode () converts the array into a JSON string and returns
$ Arr = array ('k1 '=> 'val1', 'k2' => 'val2', 'k3' => array ('v3 ', 'v4 '));
Echo $ encode_str = json_encode ($ arr );

// Json_decode () converts a JSON string into an array object and returns the result. Double quotation marks are required when the key and value of the JSON string need to be enclosed in quotation marks.
$ Decode_arr = (array) json_decode ($ encode_str );
Var_dump ($ decode_arr );
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.