PHP array upper Mark type trap

Source: Internet
Author: User
PHP array subscript type trap ???? The project is developed using the PHP language, where MONGODB storage is used; MONGODB data is of a strong type, and PHP data is of a weak type. I couldn't find any data in MONGODB last Friday, the final problem is that the numeric character subscript of the PHP array is automatically converted into an integer subscript. Therefore, although PHP is a weak language, we also need to pay attention to the variable PHP array subscript type trap.

???? The project is developed in PHP, where mongo db storage is used; mongo db has strong data types, and PHP has weak data types, I couldn't find a data query in MONGODB last Friday. the problem was that the numeric character string subscript of the PHP array was automatically converted to an integer subscript. Therefore, although PHP is a weak language, we also need to pay attention to the current type of the variable. we are familiar with PHP's automatic type conversion rules and need to determine the type or force type conversion in some type-sensitive areas.

??? The following sample program briefly explains this phenomenon:

?

$id = "22";$arr1[$id] = "xxx";var_dump($arr1);$id = 22;$arr2[$id] = "xxx";var_dump($arr2);$id = "022";$arr3[$id] = "xxx";var_dump($arr3);$id = "2222222222222";$arr4[$id] = "xxx";var_dump($arr4);

??? The output of this program is:

?

array(1) {  [22]=>  string(3) "xxx"}array(1) {  [22]=>  string(3) "xxx"}array(1) {  ["022"]=>  string(3) "xxx"}array(1) {  ["2222222222222"]=>  string(3) "xxx"}
?

??? So how is the subscript type of the PHP array string determined? Let's take a look at the source code of PHP.

??? First, we search for [in Zend/zend_language_parser.y and find the semantic parsing rules of the array:

?

object_dim_list:        object_dim_list '[' dim_offset ']'  { fetch_array_dim(&$$, &$1, &$3 TSRMLS_CC); }                             |   object_dim_list '{' expr '}'        { fetch_string_offset(&$$, &$1, &$3 TSRMLS_CC); }    |   variable_name { znode tmp_znode;  zend_do_pop_object(&tmp_znode TSRMLS_CC);  zend_do_fetch_property(&$$,  &tmp_znode, &$1 TSRMLS_CC);}   ;

?

?? We use arrays, so we use the first rule fetch_array_dim. in the fetch_array_dim function, we find that the generated opcode is ZEND_FETCH_DIM_W (84 ). In Zend/zend_vm_def.h, ZEND_FETCH_DIM_W's processing function processes the subscript logic.

?

??? Continue tracing, from the zend_fetch_dimension_address function to zend_fetch_dimension_address_inner, and then to zend_symtable_update:

?

static inline int zend_symtable_update(HashTable *ht, char *arKey, uint nKeyLength, void *pData, uint nDataSize,  void **pDest)                 \{     HANDLE_NUMERIC(arKey, nKeyLength, zend_hash_index_update(ht, idx, pData, nDataSize, pDest));    return zend_hash_update(ht, arKey, nKeyLength, pData, nDataSize, pDest);                                      } 

?

?? HANDLE_NUMERIC is a macro that is very interesting. if the arKey of the string subscript can be converted to a long integer idx, zend_hash_index_update is called to insert the data to idx; otherwise, zend_hash_update is called to modify the value of the arKey position. Let's take a look at the specific definition of macros:

?

#define HANDLE_NUMERIC(key, length, func) {                                             \    register char *tmp=key;                                                             \                                                                                        \    if (*tmp=='-') {                                                                    \        tmp++;                                                                          \    }                                                                                   \    if ((*tmp>='0' && *tmp<='9')) do { /* possibly a numeric index */                   \        char *end=key+length-1;                                                         \        long idx;                                                                       \                                                                                        \        if (*tmp++=='0' && length>2) { /* don't accept numbers with leading zeros */    \            break;                                                                      \        }                                                                               \        while (tmp
 
  ='0' && *tmp<='9')) {                                            \                break;                                                                  \            }                                                                           \            tmp++;                                                                      \        }                                                                               \        if (tmp==end && *tmp=='\0') { /* a numeric index */                             \            if (*key=='-') {                                                            \                idx = strtol(key, NULL, 10);                                            \                if (idx!=LONG_MIN) {                                                    \                    return func;                                                        \                }                                                                       \            } else {                                                                    \                idx = strtol(key, NULL, 10);                                            \                if (idx!=LONG_MAX) {                                                    \                    return func;                                                        \                }                                                                       \            }                                                                           \        }                                                                               \    } while (0);                                                                        \}
 

??? From the macro, we know the rules for automatically converting string subscripts to long integers:

??? 1. all are numbers, but cannot have leading 0. for example, arKey = "0123" cannot be converted to 123.

??? 2. it cannot exceed the long representation range (LONG_MIN, LONG_MAX), that is, (-2147483648,214 7483647)

?

?

?

?

?

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.