PHP STRING Trap principle Description _php Tips

Source: Internet
Author: User
A string is series of characters.
String access and modification by character
Characters within strings may accessed and modified by specifying the zero-based offset of the desired character The string using square array brackets, as in $str [42]. The "I" of a string as an array of the characters for this purpose.
Note:strings may also is accessed using braces, as in $str {n}, for the same purpose. However, this syntax is deprecated as of PHP 5.3.0. Use square brackets instead, such as $STR [42].
Warning
Writing to a out of range offset pads the string with spaces. Non-integer types are converted to integer. Illegal offset type emits e_notice. Negative offset emits e_notice in write but reads empty string. Only the "a assigned string is used." Assigning empty string assigns NUL byte.

All of these are the exact words in PHP manual.
Note that when we access the array, we use the square brackets "[]", and string as one can also be accessed using the operator "[]". However, it is important to note that when the string is accessed, the contents of the operator "[]" are converted to the int type.
eg: $str = ' 123456 ';
echo $str [' php '];//result is 1, because offset ' php ' translates to an integer of 0, which is both the first character of the string.
Var_dump (Isset ($str [' php '])]//The result is bool (true) the same principle.
So, when we use isset to judge whether a setting exists a key, we should first judge whether the variable passed over is an array, and then judge whether the specified key is present.
eg://If you need to determine if the array passed over has a ' php ' key, it's safer to:
Copy Code code as follows:

function Is_set ($arr, $key) {
if (Is_array ($arr) && isset ($arr [$key])) {
The logic that exists for this value
} else{
$arr is not an array or an array $arr does not exist the logic of the key $key
}
}

If the above function does not add is_array judgment, when passing a string over, the result is not what we expected.

This is the only thing to remember, so that similar problems will arise later.

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.