Numbers, floating-point, Boolean are value types, English: int, float, bool, so you know how they're used.
such as statements: $FA = 3.14;
String and array are reference types, that is, they are placed in the stack as an address, when re-assignment, the address of the stack change direction, the original point has not been or to recycle, English: string, array.
For example: $str = "string"; $arr =array ("a" = "number", "B" = "group");//Array () is a function of the assignment of arrays, PHP has more than 1000, generally commonly used less than 200, I think.
String manipulation:
Example 2: string merging, adding
Copy CodeThe code is as follows:
$STR = 1;
echo $str. = ""; The number is converted to a string and then merged, the result: "1".
echo "
";
echo $str + = "1 Yuan"; The string is converted to a number and then added, such as "1XXX" into the number 1, the result: 2.
echo "
";
?>
Example 3: string Change case
Copy CodeThe code is as follows:
$str = "12345ABc";
echo Strtolower ($STR);//lowercase, Result: "12345abc".
echo "
";
echo Strtoupper ($STR);//lowercase, Result: "12345ABC".
echo "
";
?>
Example 4: string length, intercept substring (Chinese and English)
Copy CodeThe code is as follows:
$str = "String 2";
Echo Mb_strlen ($str, "UTF-8"); A function that returns the length of a string, the second parameter is encoded, because the page is encoded with UTF-8, so for this. If omitted, returns the number of bytes consumed by memory (ASCII), which is 10. Results 4
echo "
";
Echo Mb_substr ($STR, 1, 2, "UTF-8"); The return character is intercepted, 1 is intercepted from the "character" address, 2 is a 2 "UTF-8" encoded character, the result: "string".
echo "
";
/**
* Knowledge Point: Now begins to touch the function, each function has () as a stack call, () put 0 or more parameters, you can customize can have default values. The keyword, like echo, is not ().
* Many books are encoded with GB2312 and are cumbersome to take lengths and substrings. Here is a reference for you do not use the above MB Chinese string extension Library implementation principle:
*/
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); Converted to GB2312 encoding, the ORD function returns the corresponding ASCII value to determine whether the Chinese character is ending for each byte.
for ($i = 0; $i < strlen ($STR); $i + +)//strlen ($STR) returns memory consumption in bytes equivalent to Mb_strlen ($STR)
{
if (Ord ($str [$i]) > 0xa0) $i + +; $STR [$i] corresponds to the I byte of memory. It would be more complicated to judge directly with UTF-8, because the multiplicity of encodings UTF-8 is commonly used for Web pages, UTF-16 (Unicode) is the Windows encoding.
$num + +;
}
}
Else
{
$num = "code not implemented";
}//Be interested in checking the information yourself
return $num;
}
echo My_mb_strlen ($STR). ";" . My_mb_strlen ($str, "GB2312"). "
"; The page is encoded with UTF-8, but you say that the incoming string 3 is GB2312, even if the function is implemented is not correct.
?>
Example 5: Substring lookup, substitution
Copy CodeThe code is as follows:
$str = "String 4";
Echo Mb_strpos ($str, ' string 4 ', 0, ' UTF-8 '); Finds the position of the first substring found starting with 0, results: 2. If not found, returns null (= ""), or 6 if the last two arguments do not.
echo "
";
Echo mb_strstr ($str, ' string ', 0, ' UTF-8 '); Intercepts the first substring found starting at 0 to the end, the result: "String 4". Returns empty (= "") If not found, returns the same =strstr ($str, ' string ') if the last two arguments do not.
echo "
";
Echo Str_replace ("4", "Not 4", $str); string substitution, Result: "String is not 4".
echo "
";
?>
Example 6: Substring de-space, HTML escape
Copy CodeThe code is as follows:
$str = "String 5";
echo $str =trim ($STR);//Remove both spaces, Result: "String 5".
echo "
";
echo "color=\" red\ "",//\ manually escaping inside ', ', \, so that it is stored in memory, the result "color=" Red ""
echo "
";
$str = "
123 ";
echo htmlentities ($STR); String escape <>& ' "to avoid conflicts with HTML identifiers so that they can be displayed in the HTML browser, the result:" <br>123 ".
echo "
";
?>
http://www.bkjia.com/PHPjc/322481.html www.bkjia.com true http://www.bkjia.com/PHPjc/322481.html techarticle numbers, floating-point, Boolean are value types, English: int, float, bool, so you know how they're used. such as statements: $FA = 3.14; strings and arrays are reference types, which means that he ...