PHP function operations
<? Php
// Simple functions
Function show (){
Echo "hello ";
}
Show ();
// Functions with Parameters
Function show ($ ){
Echo "$ ";
}
Show ("world ");
// Functions with returned values
Function show (){
Return "Small V, hello! ";
}
Echo show ();
Function show ($ a, $ B ){
Return $ a + $ B;
}
Echo show (10, 5 );
// Variable parameter functions (special usage)
Function sum (){
$ Arr = func_get_args ();
$ Sum = 0;
For ($ I = 0; $ I <count ($ arr); $ I ++ ){
$ Sum + = $ arr [$ I];
}
Echo $ sum;
}
Sum (1, 2, 5 );
// Common functions
Echo rand (); // obtain a random number
Echo time (); // obtain the current Timestamp
Echo date ("Y-m-d H: I: s", time (); // format the display time
Echo strtotime ("10:00:00") // convert the time to the timestamp
// String functions
$ S = "Small V, hello! ";
$ S1 = "hello world ";
Echo strlen ($ s1 );
Echo strtolower ($ s1 );
Echo strtoupper ($ s1 );
// Split the string
$ Str = "hello | mike | nice | 159 ";
Var_dump (explode ("|", $ str ));
// Merge strings
$ Arr = [2, 5, 9, 6, 3];
Echo implode ($ arr );
// Replace the string
Echo substr_replace ($ str, "###", 16,3 );
Echo str_replace ("|", "@", $ str );
// Truncate the string
Var_dump (substr ($ str, 10, 4 ));
// Regular Expression
Separator:/start/end
^ Match start with $ match end
* The previous expression can appear n times + the previous expression appears at least once.
? The preceding expression can appear for times.
{N} matches n times {n,} At least appears n times at most {n, m} At Least n times at most m times
X | y or [abc] matches any one of them [a-z] matches any one between a and z.
\ D any number \ S any visible character \ w any word character including underline
$ S = a1b2c3d4e5;
Echo preg_replace ("/\ d/", "*", $ s); // replace
Var_dump (preg_split ("/\ d/", $ s); // split
// Match the first regular type
$ S = file_get_contets (); // get the content
$ Arr = array ();
Preg_match ("", $ s, $ arr );
?>