"The fastest in PHP programming" the second lecture on numbers, floating-point, Boolean, string and array _php Foundation

Source: Internet
Author: User
Tags lowercase ord php programming strlen
Numbers, floating-point, Boolean are value types, English: int, float, bool, so you know how they use it.

such as sentence: $fa = 3.14;

strings and arrays are reference types, that is, they are placed on the stack as addresses, and when the value is assigned, the address in the stack changes direction, the original point is not or is recycled, English: string, array.

For example: $str = "string", $arr =array ("a" => "number", "B" => "group"),//Array () is an array assignment function, such a function PHP has more than 1000, commonly used less than 200, I think.

String manipulation:

Instance 2: string merging, adding
Copy Code code as follows:

<?php
$STR = 1;
echo $str. = ""; Numbers are converted into strings and then merged, resulting in "1".
echo "<br>";
echo $str + = "1 Yuan"; The string is converted to a number and then added, such as "1XXX" into the number 1, resulting in 2.
echo "<br>";
?>

Instance 3: String Change case
Copy Code code as follows:

<?php
$str = "12345ABc";
echo Strtolower ($STR);//lowercase, Result: "12345abc".
echo "<br>";
echo Strtoupper ($STR);//lowercase, Result: "12345ABC".
echo "<br>";
?>

Instance 4: String length, intercepting substring (in Chinese and English)
Copy Code code as follows:

<?php
$str = "String 2";
Echo Mb_strlen ($str, "UTF-8"); Returns the string length of the function, the second parameter is encoded, because the page is encoded with UTF-8, so for this. If omitted, returns the number of bytes (ASCII) used in memory, that is, 10. Result 4
echo "<br>";
Echo Mb_substr ($STR, 1, 2, "UTF-8"); The return character interception, 1 is intercepted from "the character" address, 2 is intercepts 2 "UTF-8" the encoded character, the result: "The string".
echo "<br>";
/**
* Knowledge Point: Start Contact function Now, each function has () as a stack call, () inside put 0 or more parameters, you can customize can have a default value. And the keyword such as echo is not ().
* Many books are encoded with GB2312, which is troublesome when taking lengths and substrings. Here is a reference to the use of the above MB Chinese string expansion Library implementation principle:
*/
function My_mb_strlen ($str, $code = "UTF-8")//define a new functions, $str are parameters 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 ends in each byte.
for ($i = 0; $i < strlen ($STR); $i + +)//The number of bytes returned in memory in this strlen ($STR) is equivalent to Mb_strlen ($STR)
{
if (Ord ($str [$i]) > 0xa0) $i + +; $STR [$i] corresponds to the I byte of memory. It is more complicated to judge directly using UTF-8, because the diversity of encodings UTF-8 the common encoding of web pages, and UTF-16 (Unicode) is the Windows encoding.
$num + +;
}
}
Else
{
$num = "code not implemented";
Find out what you are interested in.
return $num;
}
echo My_mb_strlen ($STR). ";" . My_mb_strlen ($str, "GB2312"). "<br>"; The page is encoded with UTF-8, and you say that the Passed-in string 3 is GB2312, even if the function is not true.
?>

Instance 5: Substring lookup, replace

Copy Code code as follows:

<?php
$str = "String 4";
Echo Mb_strpos ($str, ' string 4 ', 0, ' UTF-8 '); Finds the position of the first substring found starting from 0, and results: 2. If it is not found, return null (= ""), or 6 if the last two arguments do not.
echo "<br>";
Echo mb_strstr ($str, ' string ', 0, "UTF-8"); Intercepts the first substring found from 0 to the end, and the result: "String 4". If not found, returns null (= "") and returns the same =strstr ($str, ' string ') if the last two arguments do not.
echo "<br>";
Echo Str_replace ("4", "Not 4", $str); string substitution, Result: "String is not 4".
echo "<br>";
?>

Instance 6: Substring null, HTML escape
Copy Code code as follows:

<?php
$str = "String 5";
Echo $str =trim ($STR);//Remove both spaces and result: "String 5".
echo "<br>";
echo "color=\" red\ "",//\ manually escaping ', ', \, storing it in memory, the result color= "Red" "
Echo" <br> ";
$str = "<br>123";
Echo htmlentities ($STR);//String Escape <>& "avoids and HTML identity conflicts so that it can be displayed on the HTML browser side, resulting in" &ltbr&gt123 ".
echo "<br>";
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.