Principles of PHP string traps

Source: Internet
Author: User

A string is series of characters.
String access and modification by character
Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array brackets, as in $ STR [42]. think of a string as an array of characters for this purpose.
Note: strings may also be accessed using braces, as in $ STR {42}, 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 an 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 first character of an assigned string is used. assigning empty string assigns NUL byte.

The above are all the original words in PHP manual.
Note that brackets [] are used to access the array. Strings can also be accessed using the operator. However, note that when accessing a string, the content in the operator "[]" will be converted to the int type.
Eg: $ STR = '000000 ';
Echo $ STR ['php']; // The result is 1, because offset 'php' is converted to integer 0, which is the first character of the string.
Var_dump (isset ($ STR ['php']); // The result is the same as the bool (true) principle.
Therefore, when we use isset to determine whether a key exists in a setting, we should first judge whether the passed variable is an array and then determine whether a specified key exists.
Eg: // if you need to determine whether the passed array has the 'php' key, the safer method is:CopyCodeThe Code is as follows: function is_set ($ arr, $ key ){
If (is_array ($ ARR) & isset ($ arr [$ key]) {
// Logic with this value exists
} Else {
// $ Arr is not an array or an array $ arr does not have the key $ key logic
}
}

If no is_array is added to the above function, when a string is passed, the result is not as expected.

This is the only note to avoid similar problems in the future.

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.