Numbers, floating point, Boolean, string and array, floating point, Boolean are value type, English: int, float, bool, so you know how they are used. for example, the statement $ fa3.14; strings and arrays are of reference type, that is, they... numbers, floating point, Boolean, string and array, floating point, Boolean are value type, English: int, float, bool, so you know 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, array.
For example: $ str = "string"; $ arr = array ("a" => "number", "B" => "group"); // array () it is an array assignment function, which has more than one thousand php functions, usually less than 200. I think.
String operation, instance 2: string merging and addition, the code is as follows:
"; Echo $ str + =" 1 RMB "; // string is converted to a number and then added. for example," 1xxx "is converted to a number 1. Result: 2. Echo"
";?>
Instance 3: The string is case-insensitive and the code is as follows:
"; Echo strtoupper ($ str); // small write, result:" 12345abc ". Echo"
";?>
Example 4: string length and substring truncation (Chinese and English). The code is as follows:
"; Echo mb_substr ($ str, 1, 2," UTF-8 "); // The return character is truncated. 1 is truncated starting from the" character "address, 2 is a string of 2 UTF-8 encoded characters ". Echo"
";/*** Knowledge point: now you are new to functions. each function has () as a stack call. () contains 0 or more parameters, you can customize the default value. 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 the Chinese character of each byte 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] indicates the memory I bytes. If UTF-8 is used directly, it will be more complicated, because the diversity of encoding UTF-8 is commonly used for webpage encoding, and UTF-16 (unicode) is windows encoding. $ Num ++;} else {$ num = "encoding not implemented";} // If you are interested, check your information. return $ num;} echo my_mb_strlen ($ str ). ";". my_mb_strlen ($ str, "gb2312 ")."
"; // This page is encoded in UTF-8, but you can say that the input string 3 is gb2312, which cannot be correct even if the function is implemented.?>
Instance 5: Search and replace a substring. the code is as follows:
"; Echo mb_strstr ($ str, 'string', 0," UTF-8 "); // truncates the first substring found from 0 to the end. The result is" 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: Use the following code to escape the null and html characters of a substring:
"; 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 <> & '" to avoid conflicts with html identifiers so that they can be displayed on the html browser. The result is :"
123 ". Echo"
";?>
Tutorial link:
Reprint at will ~ However, please keep the tutorial address★