PHP basic built-in functions

Source: Internet
Author: User
Tags ereg sorts

Basic knowledge

1. References
There is a reference concept in the PHP language, which is equivalent to pointers. Such as$a = &$b

      <?php            $a1;            $b = &$a;            $b"2$b";            echo"$a,$b"      ?>

2, Operator precedence
(new) > (+ +, –) > (*,/,%) > (+,-) > (&&) > (| |) > (?:) > (= =) > (and) > (XOR) > (OR)

Web programming

1, _get and _post get form data
2, when processing multi-box, in the HTML to define the form of an array, such as<input name="hobby[]" type="checkbox" value="sport">

Built-in function array built-in functions
   (1) for, foreach Loop output array element for: array of arrays in integer order foreach ($array as $value) foreach ($array as $key = $value) (2) PRI Nt_r () View the array structure, if output <pre&gt before output, the results of the page output will be cleaner (3) count () counts the number of array elements (4) Array_chunk ($array, $size, $preserve) Chunk: block, number   block, block, function function when an array is split, $array, the original array, $size, the array size of the split, greater than 0, $preserve, whether the original index is used.   (5) Array_merge ($array ...) array merge, accept only the parameters of the array type, merge fails, return NULL, if the array index is a numeric index, the index is numbered from ' 0 ', and if the string index, the overwrite occurs. (6) Current () returns an array of elements (7) prev () returns the previous element of the current element, the method moves the pointer inside the array, if there is no previous return False (8) next () returns the last element of the current element, the method can move the pointer inside the array, as If the result does not exist then return False (9) end () returns the last element, the method moves the pointer inside the array (Ten) sort ($array) to sort (one) Rsort ($array) array in reverse order (a) Asort ($ar  Ray) sorts the array elements, keeping the original index relationship of the arrays unchanged (Ksort) ($array) sorts the elements by index name, keeping the original array index relationship unchanged (+) Arsot () (+) Krsort () (+) Shuffle ()   Randomly scrambling the array, deleting the original index (Array_reverse) ($array, $preserve) sorts the original array in reverse order, returns the sorted array, $preserve whether the index is preserved, and false by default. Reset () Resets the array, moves the inner pointer of the array to the first element, and returns the first element. , if the array is empty, returns False () list (), assigns the array element to the variable, strictly speaking, like array, is not a function, only can be used for digital cableand assumes that the index starts at 0. (20) stack operation, Array_push (array,mixed value) pushes one or more elements to the stack, returning the total number of new elements in the array; Array_pop (array), out of the stack, the stack is empty, returns a null (21) queue operation, Array_shift (array) deletes the first element and returns; Array_unshift (Array,val1,val2,...), The parameter is added sequentially in the queue (array_change_key_case) (Array,case_lower/case_upper) converts the string index to case and does not work on the numeric index (23) Set operation: intersection, Array_ Intersect (ARRAY,ARR1 ...) Find the intersection of array elements, ARRAY_INTERSECT_ASSOC () to find the intersection of array key-value, difference set, Array_diff (Array,arr1 ...) to find the difference set of arrays, ARRAY_DIFF_ASSOC (array,arr1 ...) Intersect intersection of key-value difference sets; Intersect diff () Array_flip () swaps the key-value pairs of an array, and if the same value occurs more than once, the last index is converted to the corresponding value (+) array_fill (int start,int num, Mixed val) array_count_values (array) counts the number of elements in the array, returns an array, the value of the original array when the key is present, the number of occurrences (array_key_exists) (Key,array) checks whether the index exists Gets all the indexes in the array (Array_keys) (Array,[,searchvalue][,strict]), returns all the elements, regardless of whether or not it repeats Array_map ( CALLBACKFUNC,ARR1,ARR2,...), the number of arrays is the same as the parameters of the callback function (Array_filter) (Array,callbackfunc);   Filters the elements in the array, and when the callback function returns True, the element is placed in the result set and the key name remains unchanged. (+) In_array (Var,array) var returns the key of the current element in array (+) key (array)
String built-in functions
   (1) Split merge explode (Separator,str[,limit]) uses a string separator to split the string str,limit to limit the number of elements in the returned array, implode (Bds,array), merging arrays into strings , using the BDS link element (2) to compare and replace strcmp (STR1,STR2), Str_replace (SEARCH,REPLACE,STR), replace all search in str with replacement, deform Str_replace ( SEARCHARR,REPLACEARR,STR), the parameter can also be an array; Substr_replace (String,replacement,start[,length]) will string the length after start (   The string replaced by the default to the end is replacement,start and length can be negative, which means the countdown starts at the tail and length is a negative number, but does not include the penultimate length.   (3) Output, print strictly speaking, print is a language structure, not a function, unlike Echo is the print return value is 1, and Echo has no return value; echo can print multiple strings, while print prints only one string. (4) Formatted output sprintf (format,mixed args), similar to the C language of formatted output, similar to the function printf, here if you use $ to complement, you need to use "' $" such a notation (5) to get the substring substr (string,star T,length) intercepts the length of a substring from the start position of string, Strstr (String,needle,flag) finds the first occurrence of the needle in a string and returns all characters after it, including this character, If flag is true, all previous characters (excluding this character) are returned, and all characters after the needle last occurrence in STRCHR () and STRSTR;STRRCHR (String,needle,flag) string are returned  ; Strpos (String,needle[,start]) needle The first occurrence in a string. (6) Delete the first two ends of the extra characters, trim (), LTrim (), RTrim (), the default to remove the white space on both sides of the character, you can also specify. such as trim (STRING,RESTR)  (7) Gets the string length strlen (string), Mb_strlen (String,encode) (8) Case Conversion Strtolower Strtoupper (9) processing the HTML tag string htmlentities (str ing) htmlspecialchars (); Html_entity_decode (string) parses the HTML entity into a string (10) repeatedly generating the string str_repeat (String,num); Str_pad (string , Length,pad,pad_type) The value of Pad_type is constant, Str_pad_left,str_pad_right,str_pad_both.  Use the pad to pad_type the length of a string on the end of string. (11) hash value MD5 () SHA1 () Str_split (String[,split_length]) splits a string into a substring of length and returns an array; split (regex,string) supports regular, Splits a string by a regex
File operations built-in functions
  (1) Opendir (path) opens the directory, returning a value of a resource type (2) closedir (path) close directory (3) Readdir (dir) Read folder, get file name under folder instead of File path (4) Scandir (Strin G dir) returns an array of file names in the folder, failed to return false, parameter dir is not a directory return False (5) GETCWD () Gets the current working directory (6) chdir () changes the current directory (7) Fp=fopen (Filename,mode Opens the file, returns a resource type data, fails returns False (8) fclose (FP) closes the file, the parameter is fopen return value (9) fgets (FP) reads a line, the parameter needs to be a file handle (filename) to read the entire file, Returns an array in which each element is a row (one) of the file get_file_contents (filename) reads the entire file into a string; Fread (Fp,length) reads the entire file and can safely read the binary file.                    If you simply want to read the contents of a file to a string, you should use a better-performing file_get_contens (); () ReadFile () reads the entire file, writes the output buffer, returns the number of bytes read (fwrite (fp,content), returns the number of bytes written, (+) fputs (), as in fwrite () File_put_conten TS (filename,data,mode), which returns the number of bytes written, data can be a one-dimensional array, the value of mode FILE_USE_INCLUDE_PATH,FILE_APPEND,LOCK_EX (Fileowner) ( FileName) returns the file owner ID (+) filesize () Gets the file size (in) filetype () Gets the file type, the return value File,dir,link,block,unknown (+) Is_dir (filename ) is_file (filename) isreadable () iswriteable () (BaseName (Filename[,suffix]) gets the file name portion of the path, and if the suffix exists, the file name returned does notIncludes suffix, which can be used to remove the extension; DirName () Gets the directory name part of the path (in) file_exists (filename) to determine if the file exists (a) mkdir (Pathname,mode) created successfully returns True, Failed to return False (RmDir) The directory must be empty directory (pathname) unlink (filename) Delete file (*) copy (SOURCE,DESC) copies source to DESC, successfully returns True (26) Rename (oldname,newname) FGETSS (handle) can use fopen to open the URL, at which time FGETSS can filter out the HTML tags (*) include () require () () filemtime () Fileatime () Filectime () returns the UNIX timestamp
Time-date built-in functions
  (1) date(format[,timestamp])  (2) mktime(hour,minute,second,month,day,year) 省略的参数将以本地日期和时间代替。  
URL processing built-in functions
  (1) urlencode(str) 返回值字符串中所有的非字母和数字字符变成一个百分号(%) 和一个两位的十六进制数,空格被转换成+,-、_和.不做任何转换  
Mathematical operations
  (1) abs(num)  (2) ceil(num)  (3) floor(num)   (4) sqrt(num)  (5) round(num,precision) 四舍五入成指定precision位数  (6) 进制之间的转换 bin 二进制,dec 十进制, oct 八进制,hex 十六进制,十进制转二进制 decbin(num) ,十进制转八进制 decoct(num) ,其余类似,不存在二进制、八进制、十六进制之间的转换,若要实现这些转换,可以使用base_convert(num,from,to) 如 base_convert("A4",16,2);定义十六进制时不需要加0x34    (7) rand() mt_rand(min,max) 随机数,其中mt_rand()可以生成指定范围内的随机数,默认时0~RAND_MAX
Database operations
  (1) 连接数据库 conn = mysql_connect(host,username,password)  (2) 关闭链接 mysql_close(conn);  (3) 执行语句 mysql_query(sql) 仅对show,select,describe等语句返回一个资源标示,对于其他SQL语句,mysql_query() 在执行成功时返回TRUE,出错时返回FALSE  (4) 处理查询结果集         mysql_affected_rows() 取得前一次mysql操作所影响的记录行数,失败返回-1        mysql_fetch_row($result) result是执行mysql_query()之后返回的资源标识,该函数从查询结果集中返回一行数据。该函数返回值时一个数组,其中每一个元素对应一行结果记录的字段值。依次调用该函数可以返回结果集中的下一行,如果没有更多行,函数返回false。        mysql_fetch_array(result,type) 返回一行关联数组,或普通数组,或二者兼有,type:MYSQL_ASSOC 返回关联数组 MYSQL_NUM 普通数组 MYSQL_BOTH 二者兼有,通常使用该函数获取各字段的值        mysql_fetch_assoc(result) 与mysql_fetch_array()类似,返回的时关联数组  (5) 获取字段信息 mysql_fetch_field(result[,field_offset]) 返回一个object对象,属性包括包括name,table,not_null,primary_key等  (6) 选择数据库,mysql_select_db(databasename) 链接上数据库之后,使用该函数选择databasename数据库  (7) 获取结果集行数 mysql_num_rows(result) ,仅对select语句有效;要取得insert,update,delete执行影响的结果行数,需要使用mysql_affected_rows()
Regular expressions
  (1) int ereg (Pattern,string[,&regs]) pattern regular expression, to match a string, the match result is placed in regs, where Regs[0] holds the entire string matched to the remaining 1, 2... store the string in a regular sequence. Omit Regs, only the regular expression is used as a match, if the pattern pattern match is found in string, then the function returns the length of the matched string, if it is not passed in an optional parameter regs, or if the matched string length is 0, returns 1 if no match or error is found          , which returns false. (2) int eregi (PATTERN,STRING,REGS) ignores case (3) split (Pattern,string[,limit]) uses regular expression to split the string (4) Sql_regcase (string) to return Strin G-matched regular expression, this regular expression is case-insensitive (5) ereg_replace (pattern,replacement,string) replaces the matching string, Eregi_replace (Pattern,replacement, String) is not case sensitive (6) Perl-compatible regular expression functions, Perl regular expressions need to use delimiters (/), for example, "/<\/\w+>/". Function: Array preg_grep (Pattern,input[,flag]) similar to Ereg (), returns a matching element when flag=1, returns an array of mismatched elements; (7) int Preg_match (pattern,subject , Matches[,flag]); Similar to Ereg (), the captured match is placed in Mathes (8) int preg_match_all (Pattern,subject,matches[,flag]); Global search, after the first match is found, the search continues.  The results of the search are stored in matches; (9) Mixed preg_replace (Pattern,replacement,subject[,limit]), where male pattern,replacement can be an array. (Ten) array preg_split (Pattern,subject[,limit[,flag])  

PHP basic built-in functions

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.