Numbers, floating point, and boolean types are value types, English: int, float, bool, so you know how they use it.
For example, statement: $fa=3.14;
Strings and arrays are reference types, which means they are placed in the stack as addresses. When reassigning values, the address in the stack changes direction, and the original pointing is lost or recycled. English: string, array.
For example: $str=”string”;$arr=array("a"=>"number","b"=>"group");// array() is an array assignment function. There are more than a thousand such functions, and there are generally less than 200 commonly used, I think.
String operation:
Example 2: String merge, add up
<?php
$str = 1;
echo $str .= ""; //The number is converted into a string and merged, the result is: "1".
echo "<br>";
echo $str += "1 Yuan"; //The string is converted into numbers and then added, such as "1XXX" is converted into numbers 1, the result is: 2.
echo "<br>";
?>
Example 3: String changes case
<?php
$str="12345ABc";
echo strtolower($str);//Result: "12345abc".
echo "<br>";
echo strtoupper($str);//Result: "12345ABC".
echo "<br>";
?>
Example 4: string length, intercept substring (Chinese and English)
<?php
$str = "String 2";
echo mb_strlen($str, "UTF-8"); //Returns the function of string length, the second parameter is encoding, because the page is encoded with UTF-8, so this is the case. If omitted, return the number of bytes occupied by the memory (ASCII), that is, 10. Results 4
echo "<br>";
echo mb_substr($str, 1, 2, "UTF-8"); //Return character interception, 1 is interception from the "character" address, 2 is interception 2 "UTF-8" encoded characters, the result is: "String".
echo "<br>";
/**
* Knowledge point: Now we have started to contact functions. Each function has () as a stack call. 0 or more parameters are placed in (), which can be customized and has default values. And keywords such as echo do not have ().
* Many books are encoded in GB2312, and it is very troublesome to get length and substrings. Let me refer to the implementation principle of the mb Chinese string extension library without using the above:
*/
function my_mb_strlen($str, $code = "UTF-8") // Define a new function, $str is a parameter that must be passed in.
{$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 ends in each byte.
for($i = 0;$i < strlen($str);$i++) // This strlen($str) returns the number of bytes occupied by the memory equivalent to mb_strlen($str)
{
if (ord($str[$i]) > 0xa0)$i++; //$str[$i] corresponds to the i-byte of memory. It will be more complicated to judge directly using UTF-8, because the diversity of encoding UTF-8 is commonly used for web pages, and UTF-16 (Unicode) is Windows encoding.
$num++;
}
}
else
{
$num = "Coding not implemented";
} //If you are interested, please check the information yourself
return $num;
}
echo my_mb_strlen($str) . ";" . my_mb_strlen($str, "GB2312") . "<br>"; //The page encoding uses UTF-8, but you say that the string 3 passed in is GB2312, and even if the function is implemented, it cannot be correct.
?>
Example 5: Substring search and replacement
<?php
$str = "String 4";
echo mb_strpos($str, 'string 4', 0, "UTF-8"); //Find the position of the first substring found from 0, result: 2. If the last two parameters are not found, return empty (=""); if the last two parameters are not available, return 6.
echo "<br>";
echo mb_strstr($str, 'string', 0, "UTF-8"); //Seave the first substring found from 0 to the end, and the result is: "string 4". If the last two parameters are not found, return empty (=""); if the last two parameters are not available, return the same =strstr($str,'string').
echo "<br>";
echo str_replace("4", "not 4", $str) ; //Stand substitution, result: "String is not 4".
echo "<br>";
?>
Example 6: Empty substrings, html escape
<?php
$str=" string 5 ";
echo $str=trim($str);//Remove spaces on both sides, the result is: "String 5".
echo "<br>";
echo "color=\"red\"";//\manually escape the ', ",\\ in it to store it in memory, and the result is "color="red""
echo "<br>";
$str="<br>123";
echo htmlentities($str) ; //String escape <>&'" to avoid conflicts with html logos, so that it can be displayed on the html browser side, the result: "<br>123".
echo "<br>";
?>
For example, statement: $fa=3.14;
Strings and arrays are reference types, which means they are placed in the stack as addresses. When reassigning values, the address in the stack changes direction, and the original pointing is lost or recycled. English: string, array.
For example: $str=”string”;$arr=array("a"=>"number","b"=>"group");// array() is an array assignment function. There are more than a thousand such functions, and there are generally less than 200 commonly used, I think.
String operation:
Example 2: String merge, add up
Copy the codeThe code is as follows:
<?php
$str = 1;
echo $str .= ""; //The number is converted into a string and merged, the result is: "1".
echo "<br>";
echo $str += "1 Yuan"; //The string is converted into numbers and then added, such as "1XXX" is converted into numbers 1, the result is: 2.
echo "<br>";
?>
Example 3: String changes case
Copy the codeThe code is as follows:
<?php
$str="12345ABc";
echo strtolower($str);//Result: "12345abc".
echo "<br>";
echo strtoupper($str);//Result: "12345ABC".
echo "<br>";
?>
Example 4: string length, intercept substring (Chinese and English)
Copy the codeThe code is as follows:
<?php
$str = "String 2";
echo mb_strlen($str, "UTF-8"); //Returns the function of string length, the second parameter is encoding, because the page is encoded with UTF-8, so this is the case. If omitted, return the number of bytes occupied by the memory (ASCII), that is, 10. Results 4
echo "<br>";
echo mb_substr($str, 1, 2, "UTF-8"); //Return character interception, 1 is interception from the "character" address, 2 is interception 2 "UTF-8" encoded characters, the result is: "String".
echo "<br>";
/**
* Knowledge point: Now we have started to contact functions. Each function has () as a stack call. 0 or more parameters are placed in (), which can be customized and has default values. And keywords such as echo do not have ().
* Many books are encoded in GB2312, and it is very troublesome to get length and substrings. Let me refer to the implementation principle of the mb Chinese string extension library without using the above:
*/
function my_mb_strlen($str, $code = "UTF-8") // Define a new function, $str is a parameter that must be passed in.
{$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 ends in each byte.
for($i = 0;$i < strlen($str);$i++) // This strlen($str) returns the number of bytes occupied by the memory equivalent to mb_strlen($str)
{
if (ord($str[$i]) > 0xa0)$i++; //$str[$i] corresponds to the i-byte of memory. It will be more complicated to judge directly using UTF-8, because the diversity of encoding UTF-8 is commonly used for web pages, and UTF-16 (Unicode) is Windows encoding.
$num++;
}
}
else
{
$num = "Coding not implemented";
} //If you are interested, please check the information yourself
return $num;
}
echo my_mb_strlen($str) . ";" . my_mb_strlen($str, "GB2312") . "<br>"; //The page encoding uses UTF-8, but you say that the string 3 passed in is GB2312, and even if the function is implemented, it cannot be correct.
?>
Example 5: Substring search and replacement
Copy the codeThe code is as follows:
<?php
$str = "String 4";
echo mb_strpos($str, 'string 4', 0, "UTF-8"); //Find the position of the first substring found from 0, result: 2. If the last two parameters are not found, return empty (=""); if the last two parameters are not available, return 6.
echo "<br>";
echo mb_strstr($str, 'string', 0, "UTF-8"); //Seave the first substring found from 0 to the end, and the result is: "string 4". If the last two parameters are not found, return empty (=""); if the last two parameters are not available, return the same =strstr($str,'string').
echo "<br>";
echo str_replace("4", "not 4", $str) ; //Stand substitution, result: "String is not 4".
echo "<br>";
?>
Example 6: Empty substrings, html escape
Copy the codeThe code is as follows:
<?php
$str=" string 5 ";
echo $str=trim($str);//Remove spaces on both sides, the result is: "String 5".
echo "<br>";
echo "color=\"red\"";//\manually escape the ', ",\\ in it to store it in memory, and the result is "color="red""
echo "<br>";
$str="<br>123";
echo htmlentities($str) ; //String escape <>&'" to avoid conflicts with html logos, so that it can be displayed on the html browser side, the result: "<br>123".
echo "<br>";
?>