A detailed _php example based on the usage of PHP common functions

Source: Internet
Author: User
Tags time and seconds
Copy CodeThe code is as follows:
=============================== Time Date ===============================
Y returns the last two digits of the year, the Y-year four digits, the M-month number, and the M-month English. D Month Date number, D week a few English
$date =date ("y-m-d");
$date =date ("y-m-d h:i:s");//Time and seconds

Include,include_once.require,require_once
Require ("file.php") will be read into the file specified by require before the PHP program executes, if the error is fatal.
The Include ("file.php") can be placed anywhere in the PHP program, and the PHP program will read into the include specified file when it is executed, such as an error prompting

=============================== Output Print ===============================
sprintf ("%d", "3.2");//format only, return formatted string, not output.
printf ("%d", "3.2");//format and output
Print ("3.2");//Output only
echo "Nihao", "AA";//can output multiple strings
Print_r (Array ("A", "B", "C"));//The array's key values and elements are displayed sequentially

=============================== commonly used String functions ===============================

Gets the string length, the number of characters, the space is also counted
$STR = "Sdaf SD";
$len =strlen ($STR);

Use the string in the first argument to concatenate each element in the following array and return a string.
$str =implode ("-", Array ("A", "B", "C"));

String segmentation method, return an array, with the characters in the first argument to split the following string, the specified characters are truncated before and after the character, if the specified character at the beginning or end of the returned array of the beginning or end of the element is an empty string
A null value is returned to the corresponding element of the array without splitting it into a string. The last limit returns the length of the array, which is not limited, and is always split down.
$array =explode ("A", "Asddad addsadassd dasdadfsdfasdaaa", 4);
Print_r ($array);

Rejects the space at the beginning of the string and returns
If there is a second argument, remove the space at the beginning of the left and replace the string in the second argument.
$str =ltrim ("A ASD", "a");

Remove whitespace from the right of the string
$str =rtrim ("ASD");

Rejects the string that begins with the second argument on both sides of the first string. If there is no second argument, the default is to remove the space between the beginning of the string
$str =trim ("Sdsdfas", "a");

How long (how many) characters are taken from the specified position in the first argument of the string, and the first character position in the string is calculated from 0.
If the second argument is negative, the string is taken from the end of the string to the beginning of the last number. The last character at the end counts-1, and the intercept direction is always left to right.
$str =substr ("Abcdefgh", 0,4);


Replace the first argument string of the third argument with the parameter two string
$str =str_replace ("A", "" "," ABCABCABCA ");
Same as Str_replace usage, only case insensitive
$str =str_ireplace ("A", "" "," ABCABCABCA ");

String that returns the character of the string in parentheses in all uppercase letters
$str =strtoupper ("Sdaf");

Returns the first string in parentheses after it becomes uppercase
$str =ucfirst ("asdf");

Use Echo to print the bracketed string in parentheses, including the label signature
$str =htmlentities ("
");

Returns the number of occurrences of the second argument string in the first string
$int =substr_count ("Abcdeabcdeablkabd", "AB");

Returns the position of the second string at the first occurrence of the first string, the first character position 0
$int =strpos ("Asagaab", "AB");

Returns the position of the second string at the last occurrence of the first string, the first character position 0
$int =strrpos ("Asagaabadfab", "AB");

Intercepts a string that returns the first argument from left to right in the return parameter one to the last character of the argument
$str =strstr ("Sdafsdgaababdsfgs", "AB");

Intercepts a string that returns the last argument from left to right in the return parameter one to the last character of the argument
$str =strrchr ("Sdafsdgaababdsfgs", "AB");

Add "\" to each character in parameter two before the same character in parameter one
$str =addcslashes ("Abcdefghijklmn", "AKD");

Fills the string of parameter one to the length specified in parameter two (number of single characters), parameter three is the string that specifies padding, does not write the default space
Parameter four fills the position, 0 fills at the left of the parameter one, 1 right begins, and 22 sides begin at the same time. Do not write default at the right start padding
$str =str_pad ("Abcdefgh", Ten, "at", 0);

Compare the two strings corresponding to the character of the Andean Code value, the first pair is not the same, if the parameter one is greater than the return 1 in the parameter two, and vice versa returns-1, the two strings return exactly the same 0
$int 1=strcmp ("B", "a");

Returns the number format after the first parameter is formatted, the second parameter is to retain a few decimals, the parameter three is to change the decimal point to the parameter three, the parameter four is the integer part every three bits with what character divide
After the three parameters are not written, the default is to remove the fractional part, integer every three bits with commas, split. Parameter three, parameter four must exist simultaneously
$str =number_format (1231233.1415,2, "D", "a");

=============================== Common Array Method ===============================

$arr =array ("k0" = "a", "k1" = "B", "K2" and "C");

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

Determines whether the first parameter element in the array element of the second argument
$bool =in_array ("B", $arr);

Returns a new array of all the key values in parentheses that do not change the original array
$array =array_keys ($arr);

Determines whether the second parameter's array has the first parameter's key value, returns the True or False
$bool =array_key_exists ("K1", $arr);

Returns a new array of all the element values in the original array, with the key value starting from 0 and the original array unchanged
$array =array_values ($arr);

Returns 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 of the current array pointers to the element's key values and element values, then pushes the pointer to the next bit, and the last pointer to an empty element that returns an empty
The returned array has four fixed key values corresponding to the element values are the key value of the return element and the element value, where 0, ' key ' key value corresponds to the return element key value, 1, ' value ' key value corresponds to the returned element value
$array =each ($arr);

First, the array pointer is pushed to the next bit, and then the value of the element pointed to after the pointer moves
$value =next ($arr);

Pushes the array pointer to the previous bit and returns the value of the element pointed to when the pointer moves
$value =prev ($arr);

Resets the array pointer to the first element and returns the value of the element
$value =reset ($arr);

Points the array pointer to the last element and returns the last element value
$value =end ($arr);

Appends the argument of the first argument as an element to the end of the first parameter array, the index is counted from the smallest unused value, and the array length after the return
$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 at 0, the key value of the original non-numeric value remains unchanged, the original element sort position is unchanged, and the array length after the return
$int =array_unshift ($arr, "T1", "T2");

Returns the last element value extracted from the end of the array and rejects the last element from the original array
$value =array_pop ($arr);

Array_pop instead, extracts the value of an element from the header of an array and rejects an element from the original array.
$value =array_shift ($arr);

Let the first parameter array reach the second parameter value length, add the third argument as an element to the end of the first parameter array, the index is counted from the minimum unused value, and the original array does not change
$array 1=array_pad ($arr, ten, "T10");

Returns a new array that rejects the extra repeating elements in the original array, without changing the original array
$array =array_unique ($array 1);

Break the original array key value to re-order the value of the value of the aspartame code from small to large, the index from the number 0, the re-meter
$int =sort ($array);

As opposed to sort, Ascol the value of the element value in descending order of size, the index is recalculated from 0
$int =rsort ($array);

Returns an array of the values of each element in the first parameter array, followed by the value of the key, to the parameter two arrays, the length of the two arrays must be identical, the original array does not change
$array =array_combine (Array ("A", "B", "C", "D", "E"), $arr);

Combines two numbers and returns the original array unchanged
$array =array_merge ($arr, Array ("A", "B", "C"));

In the first parameter array, starting from the second parameter value position, intercept the array key value of the third parameter value length + element and return, array first element position from 0
$array =array_slice ($arr, 2, 1);

The interception function is just like array_slice (), except that the intercept is removed from the original array.
$array =array_splice ($arr, 2, 1);

The first parameter as the first element, each increment of the value of the parameter three, and then as an element in the array, until the value reaches the value of the parameter two in the array and returns the array
Parameter one, the parameter two can be a number, can be a single character, the individual characters are Ascol code value, the third parameter does not write the default 1 per increment
$array =range (3,9,2);

The corresponding relationship between the original array element and corresponding key value is re-randomly returned to true or false.
$bool =shuffle ($arr);

Computes the value of all numeric elements in the array and
$int =array_sum (Array ("A", 2, "CSSF"));


Splits an array into new array blocks, each element of the new array is an array, and the new array has several elements in each element that are determined by parameter two.
The third parameter determines whether the key value of the element retains the original key value is not writable, true is reserved, and false by default does not persist
$array =array_chunk (Array ("a" = "a", "B", "C", "D", "E", "F", "G", "H"), 2,true);


Json_encode () converts an array into a JSON-formatted string to return
$arr = Array (' k1 ' = ' val1 ', ' k2 ' = ' val2 ', ' K3 ' =>array (' v3 ', ' v4 '));
echo $encode _str = Json_encode ($arr);

Json_decode () converts a JSON format string to an object that can cast an array of values, which must be enclosed in double quotation marks when the key and value in the JSON format string are quoted.
$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.