PHP programming the fastest to understand the second lecture Numbers floating point Boolean strings and arrays
- 2020-03-31 21:20:26
- OfStack
Numbers, floats, and booleans are value types; int, float, bool, so you know how to use them.
For example: $fa=3.14;
Strings and arrays are reference types, that is, they are placed on the stack as addresses. When re - assigned, the address in the stack changes direction and the original direction is removed or recycled.
For example: $STR = "string"; $arr = array (" a "= >" Number ", "b" = >" Group "); // array() is an array assignment function. There are more than a thousand such functions in PHP.
String operation:
Example 2: string merging, adding
Example 3: string changes case
Example 4: string length, intercepting substrings (in English and Chinese)
Example 5: find and replace substrings
Instance 6: empty substring, HTML escape
For example: $fa=3.14;
Strings and arrays are reference types, that is, they are placed on the stack as addresses. When re - assigned, the address in the stack changes direction and the original direction is removed or recycled.
For example: $STR = "string"; $arr = array (" a "= >" Number ", "b" = >" Group "); // array() is an array assignment function. There are more than a thousand such functions in PHP.
String operation:
Example 2: string merging, adding
<?php
$str = 1;
echo $str .= ""; //The number is converted to a string and then merged. Result: "1".
echo "<br>";
echo $str += "1 yuan "; //The string is converted to a number and then added. For example, "1XXX" is converted to a number 1, resulting in: 2.
echo "<br>";
?>
Example 3: string changes case
<?php
$str="12345ABc";
echo strtolower($str);//Write small, result: "12345abc".
echo "<br>";
echo strtoupper($str);//Write small, result: "12345ABC".
echo "<br>";
?>
Example 4: string length, intercepting substrings (in English and Chinese)
<?php
$str = " string 2";
echo mb_strlen($str, "UTF-8"); //Returns a function of the length of the string. The second argument is the encoding, which is the case since the page is encoded in utf-8. If omitted, the number of bytes (ASCII) consumed is returned, i.e. 10. Results 4
echo "<br>";
echo mb_substr($str, 1, 2, "UTF-8"); //Return character interception, 1 from the "character" address interception, 2 to intercept two "utf-8" encoding characters, the result: "string".
echo "<br>";
function my_mb_strlen($str, $code = "UTF-8") //Define a new function,$STR is an argument that must be passed in.
{$num= 0;
if ($code == "UTF-8")
{
$str = iconv("UTF-8", "GB2312", $str); //Converted to GB2312 encoding,ord function returns the corresponding ASCII value to determine whether the Chinese character ends for each byte.
for($i = 0;$i < strlen($str);$i++) //Here strlen($STR) returns the number of bytes in memory equivalent to mb_strlen($STR)
{
if (ord($str[$i]) > 0xa0)$i++; //$STR [$I] corresponds to I bytes of memory. This is more complicated if you use utf-8 directly, because the encoding diversity utf-8 is a common encoding for web pages, and utf-16 (Unicode) is a Windows encoding.
$num++;
}
}
else
{
$num = " Code not implemented ";
} //Look it up if you're interested
return $num;
}
echo my_mb_strlen($str) . ";" . my_mb_strlen($str, "GB2312") . "<br>"; //The page code is utf-8, and you say the incoming string 3 is GB2312, even if the function is implemented correctly.
?>
Example 5: find and replace substrings
<?php
$str = " string 4";
echo mb_strpos($str, ' string 4', 0, "UTF-8"); //Find the first substring position found from 0. Result: 2. If not, return null (=""); If the last two arguments do not, return 6.
echo "<br>";
echo mb_strstr($str, ' string ', 0, "UTF-8"); //Intercepts the first substring found from 0 to the end, resulting in "string 4". If not, return null (=""); If the last two arguments do not, return the same = STRSTR ($STR,' string ').
echo "<br>";
echo str_replace("4", " not 4", $str) ; //String substitution, result: "string is not 4".
echo "<br>";
?>
Instance 6: empty substring, HTML escape
<?php
$str=" string 5 ";
echo $str=trim($str);//Remove the Spaces on both sides and the result is "string 5".
echo "<br>";
echo "color="red"";// manually escape the ', ", inside, cause it to be stored in memory, result "color="red""
echo "<br>";
$str="<br>123";
echo htmlentities($str) ; //String escape <> &'" to avoid conflicts with the HTML identity, enable it to be displayed in the HTML browser side, the result: "<br>123".
echo "<br>";
?>