Common PHP functions: php. Common functions of PHP: variables declared by php1.PHP string or (single quotation marks are generally used because it is easier to write) $ strHelloPHP; echo $ str; common functions of strpos PHP, php
1. PHP string
- String declaration variable = ''or" "(single quotation marks are generally used, because it is easier to write)
$str = 'Hello PHP';echo $str;
- Strpos calculates the position of a character in a string (starting from 0)
$ Str = 'Hello php'; echo strpos ($ str, 'o'); // calculate the position of the character in the string echo'
'; Echo strpos ($ str, 'Ph ');
$ Str = 'Hello php'; // capture string $ str1 = substr ($ str, 2, 3); // start from 2, truncate a string of 3 echo $ str1;
If the length parameter is not input, it is intercepted from the specified position until the end of the string.
- Str_split: splits a string with a fixed length (the default length is 1)
$ Str = 'Hello php'; // The split string $ result = str_split ($ str); // Save the result to an array print_r ($ result ); // input an array echo using print_r'
'; $ Result1 = str_split ($ str, 2); print_r ($ result1 );
- Explode (delimiter, string to be split) is separated by space
$str = 'Hello PHP Java C# C++';$result = explode(' ',$str);print_r($result);
$ Str = 'Hello PHP Java C # C ++ '; // string join $ num = 100; $ str1 = $ str .'
Objective-C '. $ num; echo $ str1; echo'
'; $ Str2 = "$ str
Objective-C $ num "; // A simple echo $ str2 statement;
2. PHP array
- Array declaration PHP array is very dynamic and can store all types of data
// Array declaration method $ 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 the last element
$arr = array();for($i=0;$i<100;$i++){ array_push($arr,'Item'.$i);}print_r($arr);
- Store data using Key-Value pairs
$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 PHP Array be used as an Array, but also as a Map
- Array initialization
$ Arr = array ('H' => 'hello', 'W' => 'world'); // initialize print_r ($ arr) for the PHP array );
- Array icons and key-value pairs can exist at the same time.
$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) Make World
Lifecycle 1. PHP string declaration variable = ''or" "(single quotation marks are generally used because it is easier to write) $ str = 'Hello php'; echo $ str; strpos...