Elementary Introduction to PHP source code 11: About Array_key_exists,in_array

Source: Internet
Author: User
Tags php source code
This article mainly introduces about the PHP source code 11: About the introduction of Array_key_exists,in_array, has a certain reference value, now share to everyone, the need for friends can refer to

Elementary Introduction to PHP source code 11: About Array_key_exists,in_array

Array_key_exists
(PHP 4 >= 4.1.0, PHP 5)

array_key_exists-checks whether the given key name or index exists in the array
Description
BOOL Array_key_exists (mixed key, array search)

Array_key_exists () returns TRUE when the given key is present in the array. Key can be any value that can be indexed as an array. Array_key_exists () can also be used for objects.

The program is processed separately for three different types,
1. String: Case is_string:
==> static inline int zend_symtable_exists (HashTable *ht, char *arkey, uint nkeylength)
==> #define HANDLE_NUMERIC (key, Length, func)//will handle the key value of the string type and, if it is a number, take the value of the Func value, otherwise go to the next
==> Zend_api int zend_hash_exists (HashTable *ht, char *arkey, uint nkeylength)//Determine if this string exists in the HashTable where the array resides
2, Number: Case Is_long:
==>zend_api int zend_hash_index_exists (HashTable *ht, ulong h)//This function takes a bucket with an index value of H, and if there are multiple values for this index, traverse the bucket with the key value as a numeric type
3, Word value: Case is_null:
==>zend_hash_exists (Hash_of (*array), "", 1)//Determine if there is a null value in the Hashtable of the array
The above functions exist: Return_true; otherwise: return_false;

In_array
(PHP 4, PHP 5)

in_array-checks if a value exists in the array
Description
BOOL In_array (mixed needle, array haystack [, BOOL strict])

Searches for needle in haystack and returns TRUE if found, otherwise FALSE.

If the value of the third parameter strict is TRUE then the In_array () function also checks whether the needle type is the same as in haystack.

Note: If needle is a string, the comparison is case-sensitive.

Note: Needle is not allowed to be an array until PHP version 4.2.0.

The program implementation is very simple, traversing the hash table where the array is located, select the comparison function according to strict, if true then use Is_identical_function, otherwise the default value of Is_equal_function,strict is False
It then uses this function to determine if it is equal and returns true if it exists

But in the program we have seen Target_hash = hash_of (*array);
Macro source code as follows:

#define HASH_OF (P) (Z_type_p (p) ==is_array? Z_arrval_p (P): ((Z_type_p (p) ==is_object?) Z_obj_ht_p (P)->get_properties ((P) tsrmls_cc): NULL)))

The function of this method is to determine whether an array is returned, if it is an array, or if it is an object, if it is an object, all properties of the object are returned, otherwise null is returned.
But in front of the judge whether the parameter is an array, the following code:

if (z_type_pp (array) = Is_array) {  php_error_docref (NULL tsrmls_cc, e_warning, "wrong datatype for second argument") ;  Return_false; }

Is it a legacy of history?
Strange!
Eof

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.