PHP Common features, PHP
1.PHP string
- The string declares the variable = ' ' or ' (the general case uses single quotes because it is easier to write)
$str = ' Hello PHP '; Echo $str;
- Strpos to calculate the position of a character in a string (starting at 0)
$str = ' Hello PHP '; Echo Strpos ($str, ' o '); // calculates the position of a character in a string Echo '
'; Echo Strpos ($str, ' PH ');
$str = ' Hello PHP '; // Intercept String $str 1 substr ($str// intercept from 2 position, intercept string with length 3 echo$str 1;
If the length parameter is not passed, it will be truncated from the specified position to the end of the string.
- Str_split split string fixed-length split (default length is 1)
$str = ' Hello PHP '; // Split String $result Str_split ($str); // Save the result in an array Print_r ($result); // enter an array using Print_r Echo '
'; $result 1 Str_split ($str, 2); Print_r ($result 1);
- Explode (split character, string to be split) separated by space
$str = ' Hello PHP Java C # C # '; $result Explode (',$str); Print_r ($result);
$str = ' Hello PHP Java C # C # '; // Connection of Strings $num = +; $str 1 $str. '
Objective-c '. $num ; Echo $str 1 ; Echo '
'; $str 2 = "$str
$num"; // another easy way to do it Echo $str 2;
2.PHP arrays
- Array declaration PHP arrays are very dynamic and can store all types of data
// how arrays are declared $arr Array (); $arr [0] = ' Hello '; $arr [1] = ' world '; $arr [2] = 2; $arr [3] = 3.14; Print_r ($arr);
- Array_push ()//Add an element
- Array_pop ()//delete last element
$arr Array (); for ($i= 0; $i<100; $i+ +) {array_push($arr, ' Item '). $i);} Print_r ($arr);
- Storing data by key-value pairs (key-value)
$arr Array (); $arr [' H '] = ' Hello '; $arr [' W '] = ' world '; Print_r ($arr); Echo '
'; Echo $arr [' H '];
Output: Array ([H] = = Hello [W] = world) Hello
- Not only can the PHP array be used as an array, but also can be used as a map
- Array initialization
$arr Array (' H ' = ' Hello ', ' W ' = ' world '); // Initialization of PHP arrays Print_r ($arr);
- You can have array markers at the same time, and key-value pairs
$arr Array (0=> ' make ',1=> ' Tom ', ' H ' = ' Hello ', ' W ' = ' ' World '); Print_r ($arr); Echo '
'; Echo $arr [0]; Echo '
'; Echo $arr [' W '];
Output: Array ([0] = make [1] = Tom [H] = = Hello [W] = World
http://www.bkjia.com/PHPjc/1069668.html www.bkjia.com true http://www.bkjia.com/PHPjc/1069668.html techarticle php common Functions, PHP 1.PHP string string declaration variable = "" or "" (generally use single quotation marks, because it is easier to write) $str = ' Hello php '; echo $str; Strpos meter ...