PHP Collation (i): Variables and strings

Source: Internet
Author: User
Tags explode preg strcmp

Variables in PHP:

1. Define: $ symbol to define the variable

2. Description:

(1) PHP weak language, when defining variables without declaring the type, but does not mean that PHP does not have a data type

(2) Variable names are case-sensitive and can only be numbers, letters, or underscores

(3) The variable defaults to the value assignment, which means that two variables are completely independent after the assignment

$a = "string"; $b = $a; Pass the value over, there is no effect between $a and $b

(4) PHP also provides another way to assign a value----a reference assignment, when you pass an address

<?php$foo  =  ' Bob ';               Assign ' Bob ' to $foo $bar  = & $foo;               by $bar reference $foo $bar  =  "My name is  $bar";   Modify $bar variable echo  $bar; echo  $foo;                  The value of the $foo is also modified?>

(5) Globle $a; Used to define global variables, used in function body (the function body does not call global variables by default)

String in PHP:

1. Definition: Double quotation marks ("") and single quotation marks (') to define a string

"String" (double quotation marks):

(1) PHP will parse some special characters (escape characters)

(2) Variables in string form will also be parsed

$str = 1;echo "This is a {$str} string";

Results:

This is a string of 1

' String ' (single quote):

Just a simple string with no corresponding parsing capability

2. Common methods

(1) String length: strlen (String);

$str = "I am a string"; Echo strlen ($STR);

Operation Result:

13

(2) Comparison string: strcmp (string1,string2)----case-sensitive, return 0 (same) and 1 (not the same); STRCASECMP (STRING1,STRING2);----case insensitive, returns 0 (same) and 1 (not the same)

$str = "I am a string"; $str _1= "I am a string"; Echo strcmp ($str, $str _1);//Case-sensitive echo "<br/>"; Echo strcasecmp ($str, $st r_1);//Case insensitive

Operation Result:
1 (different)
0 (same)

(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

PHP Collation (i): Variables and strings

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.