Questions about PHP characters and arrays

Source: Internet
Author: User
Questions about PHP characters and arrays $ str = "123456"; var_dump ($ str ["abc"]); why is the result the first character instead of null & nbsp; in addition, when I unset ($ str ["abc"]), the system prompts that this subscript does not exist ., The character string is not an array and has only a position. no subscript or associated key array functions cannot be used as strings. Therefore, & nb's questions about PHP characters and arrays
$ Str = "123456 ";
Var_dump ($ str ["abc"]);

Why is the result the first character instead of null and I am unset ($ str ["abc"]) again prompting that this subscript does not exist.
------ Solution --------------------
A string is not an array and only has a position. it has no subscript or associated key.
Array functions cannot be used as strings.

Therefore, "abc" in $ str ["abc"] is converted to a value of 0.
For $ str = "123456"; $ str ["abc"] is $ str [0], so 1 is returned.

Although you can write $ str [$ I] at a location of the access string
To prevent ambiguity, we recommend that you write $ str {$ I} like this in php}
------ Solution --------------------
Manual comparison operators
------------------------------------------
Var_dump (0 = "a"); // 0 = 0-> true
Var_dump ("1" = "01"); // 1 = 1-> true
Var_dump ("1" = "1e0"); // 1 = 1-> true
Switch (""){
Case 0:
Echo "0 ";
Break;
Case "a": // never reached because "a" is already matched with 0
Echo "";
Break;
}
?>
------------------------
The switch example is obvious.

The string index cannot use a "string", but php intelligently converts it to 0.

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.