Writing a program is the form of html code displayed by the browser. Therefore, we should first study these five types. Numbers, Floating Points, and Boolean types are value types. in English: int, float, and bool, you can see how they are used.
Example statement: $ fa = 3.14;
Strings and arrays are reference types. that is to say, they are placed in the stack as addresses. when a value is assigned again, the address in the stack changes direction. the original direction is lost or recycled: string and array.
For example: $ str = "string"; $ arr = array ("a" => "number", "B" => "group"); // array () it is an array assignment function. there are more than one thousand such functions in PHP, and less than 200 are commonly used, I think.
String operation:
Instance 2: string merging and addition
The code is as follows:
$ Str = 1;
Echo $ str. = ""; // The number is converted to a string and then merged. result: "1 ".
Echo"
";
Echo $ str + = "1 RMB"; // The string is converted to a number and then added. for example, if "1XXX" is converted to a number 1, the result is 2.
Echo"
";
?>
Instance 3: The string is case-insensitive.
The code is as follows:
$ Str = "12345ABc ";
Echo strtolower ($ str); // The result is "12345abc ".
Echo"
";
Echo strtoupper ($ str); // small write, result: "12345ABC ".
Echo"
";
?>
Example 4: string length, substring truncation (Chinese and English)
The code is as follows:
$ Str = "string 2 ";
Echo mb_strlen ($ str, "UTF-8"); // returns the string length function, the second parameter is encoding, as the page is encoded in UTF-8. If this parameter is omitted, the number of bytes in memory (ASCII) is returned, that is, 10. Result 4:
Echo"
";
Echo mb_substr ($ str, 1, 2, "UTF-8"); // return the truncation of characters, 1 starting from the "character" address, 2 is the truncation of 2 "UTF-8" encoded characters, the result: "string ".
Echo"
";
/**
* Knowledge Point: now you are new to functions. each function has () as a stack call. () contains 0 or more parameters, which can be customized with default values. For example, echo does not have a keyword.
* Many books are encoded in GB2312 format. it is difficult to get the length and substring. The following is a reference to the implementation principle of the above mb Chinese character string extension Library:
*/
Function my_mb_strlen ($ str, $ code = "UTF-8") // defines a new function, $ str is a required parameter.
{$ Num = 0;
If ($ code = "UTF-8 ")
{
$ Str = iconv ("UTF-8", "GB2312", $ str); // Convert to GB2312 encoding, the ord function returns the corresponding ASCII value to determine whether each byte of the Chinese character ends.
For ($ I = 0; $ I <strlen ($ str); $ I ++) // in this strlen ($ str) the number of bytes occupied by the returned memory is equivalent to mb_strlen ($ str)
{
If (ord ($ str [$ I])> 0xa0) $ I ++; // $ str [$ I] corresponds to the memory I bytes. It would be more complicated to identify with UTF-8 directly, because the diversity of encoding UTF-8 is commonly used for web pages, and UTF-16 (Unicode) is windows encoding.
$ Num ++;
}
}
Else
{
$ Num = "encoding not implemented ";
} // Check your information if you are interested.
Return $ num;
}
Echo my_mb_strlen ($ str). ";". my_mb_strlen ($ str, "GB2312 ")."
"; // This page is encoded with a UTF-8, you say the passed string 3 is GB2312, even if the function is implemented, it cannot be correct.
?>
Instance 5: Search and replace substrings
The code is as follows:
$ Str = "string 4 ";
Echo mb_strpos ($ str, 'string 4', 0, "UTF-8"); // find the first substring position found from 0, result: 2. If no value is found, null (= "") is returned. if the last two parameters are not found, 6 is returned.
Echo"
";
Echo mb_strstr ($ str, 'string', 0, "UTF-8"); // captures the first substring found from 0 to the end. result: "string 4 ". If no value is found, null (= "") is returned. if the last two parameters are not found, the same = strstr ($ str, 'string') is returned ').
Echo"
";
Echo str_replace ("4", "not 4", $ str); // string replacement. result: "The string is not 4 ".
Echo"
";
?>
Example 6: empty and html escape of the substring
The code is as follows:
$ Str = "string 5 ";
Echo $ str = trim ($ str); // remove spaces on both sides. result: "string 5 ".
Echo"
";
Echo "color = \" red \ ""; // \ manually convert the preceding ', "to store it in the memory. The result is" color = "red ""
Echo"
";
$ Str ="
123 ";
Echo htmlentities ($ str); // string escape <> & '"avoids conflicts with html identifiers so that they can be displayed on the html browser. The result is:" & ltbr & gt123 ".
Echo"
";
?>