1. Define: $ symbol to define the variable
2. Description:
(4) PHP also provides another way to assign a value----a reference assignment, when you pass an address
(5) Globle $a; Used to define global variables, used in function body (the function body does not call global variables by default)
1. Definition: Double quotation marks ("") and single quotation marks (') to define a string
2. Common methods
(3) Convert case: Strtolower (String),----to lowercase, strtoupper (string),----to uppercase;
1 $str = "I am a string", 2 $str _1= "I am a string", 3 echo strtolower ($STR), 4 echo "<br/>", 5 echo strtoupper ($str _1);
Splitting and merging strings explode (delimiter,string[,limit]); implode ([Glue,]array);
1 $str = "I am A String", 2 $arr =explode ("", $str), 3 Print_r ($arr), 4 echo "<br/>", 5 echo Implode ("", $arr);
Operation Result:
Array ([0] = I [1] = AM [2] = a [3] = = string)
I am a String
(4) Interception of replacement Substr_replace (String,replacement,start_index[,length]);
If replacement is an empty string, there is a substitution meaning. Precise control can be achieved by starting position and length; If replacement is not an empty string, it replaces the
1 $str = "I am A String", 2 echo $str, 3 echo "<br/>", 4 echo substr_replace ($str, "#", 3),//replaces the 3 to the last character 5 echo "<br/& gt; "; 6 echo substr_replace ($str, "#", 3,5);//Replace the string 7 echo "<br/>" from 3 (including 3) 5 lengths, 8 Echo substr_replace ($str, "", 0,7);// Truncated string string
Operation Result:
I am a String
I a#
I a#tring
String
(5) Find alternate array and string Str_replace (search,replacement,string[, $count]); $count returns the number of replacements
1//Assignment: Body text= ' black ' 2 $bodytag = str_replace ("%body%", "Black", "body1 text= '%body% '"); 3 echo $bodytag; 4 echo "<br/>"; 5 6//assignment: Hll wrld F PHP 7 $vowels = Array ("A", "E", "I", "O", "U", "a", "E",
"I", "O", "U"), 8 $onlyconsonants = str_replace ($vowels, " ", "Hello World of PHP" ); 9 echo $onlyconsonants, echo "<br/>", 11 12//assignment: You should eat pizza, beer, and ice cream every day13 $phrase
= "You should eat fruits, vegetables, and fiber every day." $healthy = Array ("Fruits", "vegetables ", " fiber "); $yummy = Array (" Pizza ", " beer ", " ice Cream "), $newphrase = str_re Place ($healthy, $yummy, $phrase); echo $newphrase; echo "<br/>"; 20 21//Assignment: 222 $str =
str_replace ("ll", " ", " Good Golly Miss molly!", $count); Echo $count;
(6) Intercept substr (String,start_index[,length]); Strat_index and length can be negative, search in the opposite direction
1 echo substr ("ABCdef",-1);//Includes Start_index's position 2 echo "<br/>"; 3 echo substr ("ABCdef", 5); 4 echo "<br/>"; 5 echo substr ("ABCdef", 2); 6 echo "<br/>"; 7 echo substr ("ABCDE", 1,4),//length length, when positive 8 echo "<br/>"; 9 echo substr ("ABCDE", 0,-1);//abcde, ending at 1 from the end of the string echo "<br/>"; Echo substr ("ABCDE", 1,-2);//BC
Regular Expressions in PHP:
1. Definition://
2. Method:
int preg_match ( string $pattern
, string $subject
[, array & $matches
[, int $flags
= 0 [, int $ Offset
= 0]])----Replace qualified, default is global, limit number of times can be used
preg_match () returns pattern
the number of matches. Its value will be 0 times (mismatched) or 1 times, because preg_match () will stop the search after the first match. Preg_match_all () differs from this, and it searches subject
until the end is reached.
1 echo preg_replace ($pattern, "#", $str), 2 echo "<br/>", 3 echo preg_replace ($pattern, "#", $str, 1, $c); 4 echo "<br /> ";
Results:
# ## # ######!
# AM a string!
Preg_split (preg,string);----Split by Preg, returns an array
1 $str = "I am a string!"; 2 $pattern = "//"; 3 Print_r (Preg_split ($pattern, $str));
Results:
Array ([0] = [1] = I [2] = = [3] = a [4] = = m [5] = [6] = a [7] = = [8] = S [9] = [1] 0] = = r [One] = I [n] ~ ~ [+] = g [+] =! [+] =)
Mixed preg_replace ( mixed $pattern
, mixed $replacement
, mixed $subject
[, int $limit
=-1 [, int &$count
]])
Array preg_grep ( string $pattern
, array $input
[, int $flags
= 0])
Returns an array of elements in the given array that input
pattern
match the pattern.
Functions in PHP:
Definition: Function name () {};
Note: Return values and Parameters
Parameters: Can be many, not less
Func_get_args (): Returns an array that contains all the arguments
Func_num_args (): Returns the number of parameters