PHP Common Function Classification summary "Daquan"

Source: Internet
Author: User
Tags arithmetic operators logical operators php basics reserved types of functions
Learning for so long PHP, basic knowledge always feel insecure, especially the array, string function application, all hands knocked over the hand, make summary is the basis, in retrospect.

one. PHP Basics Syntax
variables, constants         are strictly case-sensitive, but the built-in structure or keywords do not matter (ECHO)     named: Can not be a number, space,. To start, but can have Chinese characters, eg:$ variable = "AA";     variable: $a = ' AA '; $ $a = "BB"; $aa = "BB";     Reference assignment: $a = "AA"; $b =& $a; Change the value of the $a, $b also change. Different: storage structure is separate, even if unset ($a), $b still (differentiate C)     variable type: int str array BOOL object float resource NULL     global variable: full The local variable itself is the static storage mode, all the global variables are static variables             $_session,  $_cookie,  $_post,  $_ Get,  $_request, $_FILES,$_EVN     static variables:             Static  [detailed See figure]&nbs P     Static methods: Static methods do not require that the class being instantiated can be used directly. Math::max ($a, $b); (Not instantiate the math class directly invoke static method Max)     constants:         define ("NAME", $value, [TRUE]); If the third argument is true, case-insensitive, the default is case sensitive         predefined constants:         php_os=       "WINDOWS";  php_version= "Version";                E_error=1, error, resulting in script termination;  e_warning=2, warning, script not terminated ; e_notice=8, non-critical error         Magic constant: "All is returned to the physical path, even if it contains output, output is also source code information, not the current containing file information, and $_server distinguish"               __file__     Current file name               __ class__     Current class name
__function__ the current function name
__method__ Current Method Name
__LINE__ the current line number name
Summary: (Global) constants: (Default constants are global) stored in (static) data segments
Variable global variables: stored in static data segment local variables: stored in the stack static variables: (regardless of global/local) stored in static data segments
Type convert 1.setType ($a);//Get the type of the variable 2. $b = (int) $a;//convert $a to plastic $b =intval ($a); 3.is_int ($b);//judge whether $b is plastic, return bool value
    Type conversions: (int), (bool), (float), (), (String), (), (array), (), (object);            & nbsp           intval (), Floatval () Strval ()     type:           &NB Sp         is_bool/int/float/string/array/object/resource/null                     is_numberic (); Determine if any type of number or array string                     is_callable ();/To determine whether a valid function name operation Characters       arithmetic operators: +-*/% + +--      connection operator:         assignment operator: =, +=,-=,*=,/=,%=,.=       comparison operator: >,<,==,===,!= <>,!==       Logical operators: and,&&  ;   Or,| |  ;   not,!  ;  xor (logical XOR, different sides return true, same return false)       bitwise operator   &NBSP;: &; |;^ (XOR, different return 1); ~ (non operator, 1.0 reverse);< < left, 0;>> to right, left blank complement 0       Other operators:  &nbsp                       &NBSP?:   Three mesh operator     Example: $a = $bool ? $b: $c; If $bool is established, $a = $b; $a= $c                         @ Ignore error                         => array subscript with         &NBSP ;              -> Call object value with                 &NBSP ;       The inverted quotation mark is the execution operator ...                           instanceof type operator  class Cl assone{} $a =new classone (); Var_dump (  $a instanceof classone  );//return True Process Control
        1.if () {}else{}         2.while () {};         3.do{}while () $         4.for ($a =1 $a <10; $a + +) {}       & nbsp 5.switch ($a) {            case 1:echo 1; break;             CA Se 2:echo 2;break;             Default:echo "This is Defaut value";             {     continue,break;exit   difference:         Continue skips the current loop, the loop continues         break jumps out of the current loop, the cycle terminates    ,     exit;   Terminate the current script, the code behind this line does not execute on function       naming: Follow variable naming rules, functions can not be overloaded,     function: functions to achieve structured programming, improve the maintainability of the Code         Global variables: All scripts can be used in the     local variables: only in the function body, the completion of the function automatically release        |---> is divided into static State storage type and dynamic storage type;  static $a is a static variable, the function is finished, will not be released     function local variable programming global variable: 1.global; 2. $GLOBAL ['] Use global array   
several types of functions1. Functions Referencing Parameters: function (& $a) {The operation of a formal parameter $a within the}//function can also have an effect on the argument Eg:sort () 2. Function of default parameter: functions ($a =0) {}//No arguments passed in, the default $a=0 3. Variable parameters Number of functions: function ($a, $b $c,...) {}//principle: the Fun_get_args () function accepts all parameters and returns an array to use, so you can have multiple parameters Eg:echo (), Array_merge () 4. Callback function: $fun = "one"; function one () {} when the $fun () is invoked, the one () function is called again, Call_user_func_array () 5. Recursive function: function test () {test ()}//to call functions again in functions, But pay attention to the problem of dead loops, to have execution end jump out


two. PHP Common Functions
Common functions: Echo () "Language structure" print ()//"Language Structure" "has a return value", if the transmission failed to cause no output, it returns false
Var_dump () Var_export () "has a return value, translates a valid PHP code" printf () or similar to the C language form printf ("My name is%s, age%d", $name, $age); to sprintf ()//is similar to printf, but does not print, but returns the formatted text, others are the same as printf

Array functions: 1. Sort class: "1. No return value, pass value reference, the original array was modified directly" press V:sort,rsort,asort,arsort, press K:ksort,krsort by letter: Natsort ();//case-sensitive sort Natcasesort ()//case-insensitive sort, when the character is encountered exactly the same, according to the number row eg:file1,file2, the two characters are the same, followed by the number 1<2 row, so the result File1,file2Callback: Usort ($arr, "strnatcmp")/Callback class sort, throw each element in $arr array to strnatcmp () [Unnatural number sort] process "return a new sorted array" rule: no "k", sorted by "value" row Order, the order has "a" means to retain the KEY, there is "R" in the reverse order of "K", sorted according to "KEY" sort, there is "R" in the Order of "U", which means to throw to the callback function to handle the
2. Key value Operation class: "All have return value, did not modify on original parameter" 1 array_values ($arr);         Gets the value rearrangement in the $arr, removing the subscript "return value new indexed array" 2.array_keys ($arr [, "Str", true])//Get the subscript for all characters in $arr to be "str", an indexed array, and true to "return the new indexed array" 3.array_search ("is", $arr [, True])//Returns the key of the value "is" in $arr, the return fales,true is not found to indicate that the return of the first matching value is strictly by type (8, "8") 4.in_array ( "Str", $arr);//To determine whether "str" exists in $arr, "return bool" 5.is_array ($arr);//To determine if the array "returns BOOL" 6. Array_key_exists ($key, $arr); Query $arr Whether there are $key, "return bool" 7.array_flip ($arr),//Exchange key values, if there is duplication, come from behind, "return to the new array" 8.array_reverse ($arr, [True|false]); Array order is reversed, Param2 retains the original key value "returns the new association/Index Array" 9. Array_column (Array (), ' name ' [, Name_two])-Returns the name column specified in the array [optional argument, if there is a return name=>name_two form] "returns a one-dimensional array"
3. Number and uniqueness of elements 1.array_unique ($arr);//Remove duplicate values from $arr, repeat the first value, "Return array, key value Keep 2.array_count_values ($arr)/count array value occurrence , "returns the array, the key is the original array value, value is the number of statistics" 3.count ($arr [, 1])/sizeof ();//Statistics $arr the number of elements, the parameter "1" means the statistical multidimensional array open, the default 0 is off "return statistics"
4. Callback function1.array_filter ($arr, "function");//$arr in function functions, "returns data that evaluates to true to form a new array with key values reserved" 2.array_walk ($arr, "function" [, " Data "]//Put $arr into function (& $v, $k, $data) to process" return value is bool "3.array_map (" function ", $arr, $arr 2, $arr 3,....); /return all arrays to the callback function uniform processing, "return array" 4.array_reduce ($arr, myfunction[,initial]): The values in one-dimensional array $arr are passed to the V2 of the custom function MyFunction ($v 1, $v 2) , the v1 for the cumulative value is similar to (. =), [if there is initial, first pass it into the V1] "return string"
5. Splits, merges, decomposes, joins arrays1.array_slice ($arr, 1[,2]);//In $arr, take [, return two values] from the second start to return the new array (no effect on the original array), the key value reserved "2.array_splice ($arr, 1[,2," AAA "," BB "]); /delete or replace, take the second start from $arr, delete or replace 2 values "The return value is a new array, remove the original array" 3.array_combine ($arr 1, $arr 2);//array $arr1 for key, $arr 2 for values to form a new indexed array "return cable Quoted array "4.array_merge ($arr 1, $arr 2, $arr 3 ...); /array to merge, retain the key value, there are duplicates, then the "Return of the new array" Array_merge found that the key value of the same, take the latter; $arr 1+ $arr 2 found the same key value, take the former, the second duplicate value is lost         Discard 5.array_intersect ($arr 1, $arr 2)//returns the intersection of two arrays, the key value 6.array_diff ($arr 1, $arr 2)//Returns the difference set of the two array, the value returned is the value of the first array, the key value is unchanged 7.array_chunk ($arr, 2)//split array, $arr the "2 for a group of" equal division "returns a two-dimensional array"
6. Array of data Structure "2. No return value, pass value reference, directly to the original array of changes."

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.