_php example based on usage of common PHP functions

Source: Internet
Author: User

Copy Code code as follows:

<?php
=============================== Time and date ===============================
Y returns the last two digits of the year, Y-year four-digit, M-month number, M-month English. D month number, D week a few English
$date =date ("y-m-d");
$date =date ("y-m-d h:i:s");

Include,include_once.require,require_once
Require ("file.php") reads the file that the require specifies to be introduced before the PHP program executes, such as fatal if an error occurs.
Include ("file.php") can be placed in any location in the PHP program, the PHP program is executed to read into the include specified the introduction of the file, such as error will prompt

=============================== Output Print ===============================
sprintf ("%d", "3.2");//Only formatted, returns the formatted string, not output.
printf ("%d", "3.2");//is formatted and output
Print ("3.2");//Output only
echo "Nihao", "AA";//can output multiple strings
Print_r (Array ("A", "B", "C"));//To display the array's key values and elements in turn

=============================== Common String Functions ===============================

Gets the length of the string, how many characters it has, and the space
$STR = "Sdaf SD";
$len =strlen ($STR);

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

String-splitting method, which returns an array of characters in the first argument that is used to segment the string followed by the characters, both before and after the specified character, if the element at the beginning or end of the specified character is an empty string.
Returns an empty value to the corresponding element of an array without dividing it into a string. The last restriction returns the length of the array, without restriction, and is separated.
$array =explode ("A", "Asddad addsadassd dasdadfsdfasdaaa", 4);
Print_r ($array);

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

Remove the space at the beginning of the string
$str =rtrim ("ASD");

Remove the string at the beginning of the first string with the second argument. If there is no second argument, remove the space at the beginning of the string by default
$str =trim ("Sdsdfas", "a");

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


Replaces the first argument string of the third argument with a parameter two string
$str =str_replace ("A", "", "ABCABCABCA");
Same as str_replace usage, just not case-sensitive
$str =str_ireplace ("A", "", "ABCABCABCA");

Returns a string with all uppercase characters in parentheses
$str =strtoupper ("Sdaf");

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

Print a string in parentheses with Echo and so that the string in parentheses is printed on the Web page, including the signature symbol
$str =htmlentities ("<br/>");

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, where the first character position is 0
$int =strpos ("Asagaab", "AB");

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

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

Intercepts the string from left to right last occurrence of argument two to parameter one last character
$str =strrchr ("Sdafsdgaababdsfgs", "AB");

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

Fills the string of argument one to the length specified by parameter two (number of single characters), and parameter three is the specified padding string, not the default space
The parameter is a four-filled position, 0 is filled at the left of the argument, 1 on the right, and 22 at the beginning. Do not write default fill on right beginning
$str =str_pad ("Abcdefgh", "at", 0);

In order to compare the two string corresponding characters of the Basque code value, the first pair is not the same, if the argument is greater than two of the return 1, vice versa return-1, two strings exactly the same return 0
$int 1=strcmp ("B", "a");

Returns the number format after the first parameter is formatted, the second argument is to keep a few decimals, and the argument three is to change the decimal point to the parameter three, and the parameter four is the integer part, which is separated by what character every three bits.
The following three parameters are not written, then the default is to remove the fractional part, integer every three bits with commas, split. Argument three, parameter four must exist simultaneously
$str =number_format (1231233.1415,2, "D", "a");

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

$arr =array ("K0" => "a", "K1" => "B", "K2" => "C");

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

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

Returns a new array of all the key values in parentheses without changing the original array
$array =array_keys ($arr);

Determines whether the first parameter's key value is in the array of the second argument, and returns 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 values starting at 0 and the original array unchanged
$array =array_values ($arr);

Returns the key value that the current array pointer points to
$key =key ($arr);

Returns the element value that the current array pointer points to
$value =current ($arr);

Returns an array of the key values and element values of the current array pointer to the element, then pushes the pointer to the next bit, and the last pointer points to an empty element that returns an empty
The returned array has four fixed key values corresponding to the element value is the return element's key value and the element value, where 0, the ' key ' key value corresponds to the return element key value, 1, the ' value ' key value corresponds to the returned element value
$array =each ($arr);

First pushes the array pointer to the next bit, and then returns the element value that the pointer moves to.
$value =next ($arr);

Pushes the array pointer to the previous digit, and then returns the element value to which the pointer moves
$value =prev ($arr);

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

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

Appends the argument after the first argument as an element to the end of the first parameter array, with the index starting from the smallest unused value, and the length of the array after it returns
$int =array_push ($arr, "D", "DFSD");

Adds all the arguments after the first argument array as an element to the beginning of the first argument array, with the key value 0 from the first element, the original Non-numeric key value remains unchanged, the original element sort position is unchanged, and the array length is returned
$int =array_unshift ($arr, "T1", "T2");

Returns the last element value from the tail of the array and removes the last element from the original array
$value =array_pop ($arr);

Array_pop Instead, extract the value of the first element of the returned array, and remove the head from the original array with an element
$value =array_shift ($arr);

Let the first argument array reach the second parameter numeric length, add the third argument as an element to the end of the first parameter array, and return the index from the smallest number of values that are not used in the value, and the original array does not change
$array 1=array_pad ($arr, "T10");

Returns a new array that removes the extra repeating elements from the original array, and the original array does not change
$array =array_unique ($array 1);

The original array key value is broken back to the value of the value of the Apache code values from small to large sort, the index from the number 0 to start the recosting
$int =sort ($array);

In contrast to sort, reorder the element value Ascol the code value size in descending order, and the index is reordered from 0
$int =rsort ($array);

Returns an array of the values of each element in the first argument array that is paid as a key to the parameter two arrays, the lengths of which must be identical and the original array unchanged
$array =array_combine (Array ("A", "B", "C", "D", "E"), $arr);

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

In the first argument array, the array key value + element that intercepts the value length of the third parameter from the second parameter value position is returned, and the first element of the array is positioned from 0
$array =array_slice ($arr, 2, 1);

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

Take the first argument as the first element, each time the value of parameter three is added, and then as an element there is an array until the value that reaches parameter two is stored in the array and returns the array
Parameter one, the parameter two can be a number, can be a single character, according to the Ascol code value, the third parameter does not write the default each time increases 1
$array =range (3,9,2);

The corresponding relationship between the original array elements and corresponding key values is returned in random order to return TRUE or false
$bool =shuffle ($arr);

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


Dividing an array into a new array block, each element of the new array is an array, and the new array has several elements within each element determined by the parameter two
The third parameter determines whether the key value of the element retains the original key value and does not write, True is reserved, and default false is not retained
$array =array_chunk (Array ("A" => "a", "B", "C", "D", "E", "F", "G", "H"), 2,true);


Json_encode () converts an array to a 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 to an object that can cast a number of groups, which must be enclosed in double quotes when the key and value need to be quoted in a JSON format string
$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.