PHP uses subscript notation [] to read the logic of a string

Source: Internet
Author: User

The [(subscript)] symbol in PHP can be applied not only to arrays and objects, but also to strings, assuming you are not aware of very easy errors.

For example, to get a network interface, normally returns a JSON of the array structure, after parsing the result is:

Array (' content ' = ' This is returned by interface ')


After we have obtained the interface data, it is problematic to infer whether the content has a value by using the following statement:

if (!empty ($result [' content ')])    echo $result [' content '];

Because the server exception is assumed. The following HTML string may be returned:


Such a case. After Json_decode parsing we assume that using the above statement, we get a < character, which is why?

Let's take a look at the logic in PHP5.2.5 [] that acts on strings:

... case is_string: {zval tmp;                if (Dim = = NULL) {Zend_error_noreturn (E_error, "[] operator not supported for strings");                        } if (z_type_p (Dim) = Is_long) {switch (z_type_p (Dim)) { /* Case Is_long: */Case Is_string:case IS_DOUBLE:C ASE is_null:case Is_bool:/* do nothing */b                        Reak;                            Default:zend_error (e_warning, "illegal offset type");                    Break                    } tmp = *dim;                    Zval_copy_ctor (&AMP;TMP);                    Convert_to_long (&AMP;TMP);                Dim = &tmp; } switch (type) {case Bp_var_r:case BP_var_is:case bp_var_unset:/* do nothing ... */break;                        Default:separate_zval_if_not_ref (CONTAINER_PTR);                Break                    } if (result) {container = *container_ptr;                    Result->str_offset.str = container;                    Pzval_lock (container);                    Result->str_offset.offset = z_lval_p (Dim);                    Result->var.ptr_ptr = NULL;                    if (type = = Bp_var_r | | type = = bp_var_is) {ai_use_ptr (Result->var);            }} return; } Break, ...

The above source code: Dim denotes subscript;

First, infer that there is no subscript in the [] operator. Let's say it's an error.

Because there is no subscript, it is necessary to add a word group, which is definitely an error for a string.

Then infer that the subscript is not a digital type, please note 1234 and "1234", PHP will feel that the digital type. Assume that it is not a numeric type. The subscript is converted to a digital type. Under such a conversion rule, the string will be converted to 0.

So, in the example above. When you visit the data labeled content. It returns the data labeled 0, which is the < character.

So. Strictly speaking, when visiting an array subscript, it is important to infer that this type is not an array type, and that the ability is guaranteed to be foolproof.

PHP uses subscript notation [] to read the logic of a string

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.