Face test and answer analysis of PHP type conversion

Source: Internet
Author: User
Tags memory usage zend


One of the most frequently asked questions about new hires for a company is the value of PHP type conversions, such as:

Var_dump ((int) true);
Var_dump ((string) true);
Var_dump ((string) false);
Var_dump ((BOOL) "1");
Var_dump ((BOOL) "0");
Var_dump ((BOOL) "");
Var_dump ((BOOL) "false");

I remember the first to see this topic is in the British-pole PHP senior Development engineer post of the pen test, seemingly very basic, but still can be baffled a lot of phper. Let's take a look at the results of the operation:

Int (1)
String (1) "1"
String (0) ""
BOOL (TRUE)
BOOL (FALSE)
BOOL (FALSE)
BOOL (TRUE)

For most people, lines 1th, 2, and 4 are usually no problem. But why is false converted to a string that is an empty string? When processing a request value, it is usually false to pass a string type, but "false" (string) is not False (Boolean), which is somewhat confusing.

Why is that?

On this issue, let's start with the PHP kernel to see what's going on inside the system at the time of type conversion.

First, add some background on how the PHP weak type implementation works. PHP interpreter is written in C language, of course, the final processing of variables, will also use the C language to construct data structure to achieve. In the Zend engine, a PHP variable corresponds to a type that is zval.

Open Zend/zend_types.h file, we can see the definition of zval type, php-5.5.23 version around the 55th line:

typedef struct _ZVAL_STRUCT Zval;


So we find that zval is actually a type of struct called _zval_struct, and we found the definition of this structure in Zend/zend.h file, starting around 320 lines:



typedef Union _ZVALUE_VALUE {
Long lval; /* Long Value * *
Double Dval; /* Double Value * *
struct {
Char *val;
int Len;
} str;
HashTable *ht; /* Hash Table value * *
Zend_object_value obj;
} Zvalue_value;

struct _zval_struct {
/* Variable Information * *
Zvalue_value value; /* Value * *
Zend_uint refcount__gc;
Zend_uchar type; /* Active type */
Zend_uchar is_ref__gc;
};

As you can see, the _zval_struct contains two important members, one is the value of the Zvalue_value type, and the other is the type of Zend_uchar. Note The Zvalue_value type is a consortium that stores information about the value of a PHP variable. (If you forget what a consortium is, let me explain.) A consortium resembles a struct, but a member of a consortium that has and can only have one at a time, and the consortium occupies a space that is the longest member of the consortium, in order to conserve memory usage. In Zvalue_value, members of the five types are included long, double, struct, HashTable, and Zend_object_value. They are used to store PHP variables of different types of values, respectively:
Type C PHP Type
long bool
Int
Resource
Double float
struct string
HashTable Array
Zend_object_value Object

When you see this structure, you must also understand the frequently asked questions such as the scope of the int type in PHP, and the strlen of time complexity in PHP.

This shows that PHP variable type conversion, or weak type implementation, is essentially the implementation of the Zval type between the different types of conversion. In addition to completing the Zvalue_value numeric conversion, you also need to set the type in _zval_struct to the type of the current variable. The Convert_to_* series functions are implemented in the Zend engine, and we can see these transformation functions in ZEND/ZEND_OPERATORS.C, which can be found in about 511 rows to find functions converted to Boolean types:



Zend_api void Convert_to_boolean (Zval *op)/* {{* * *
{
int tmp;

Switch (z_type_p (OP)) {
Case Is_bool:
Break
Case Is_null:
Z_lval_p (OP) = 0;
Break
Case Is_resource: {
Tsrmls_fetch ();

Zend_list_delete (z_lval_p (OP));
}
/* Break missing intentionally * *
Case Is_long:
Z_lval_p (OP) = (z_lval_p (OP) 1:0);
Break
Case Is_double:
Z_lval_p (OP) = (z_dval_p (OP) 1:0);
Break
Case is_string:
{
Char *strval = z_strval_p (OP);

if (z_strlen_p (OP) = 0
|| (Z_strlen_p (OP) ==1 && z_strval_p (OP) [0]== ' 0]) {
Z_lval_p (OP) = 0;
} else {
Z_lval_p (OP) = 1;
}
Str_free (Strval);
}
Break
Case Is_array:
TMP = (zend_hash_num_elements (z_arrval_p (OP))? 1:0);
Zval_dtor (OP);
Z_lval_p (OP) = tmp;
Break
Case Is_object:
{
Zend_bool retval = 1;
Tsrmls_fetch ();

Convert_object_to_type (OP, Is_bool, Convert_to_boolean);

if (z_type_p (OP) = = Is_bool) {
Return
}

Zval_dtor (OP);
Zval_bool (OP, retval);
Break
}
Default
Zval_dtor (OP);
Z_lval_p (OP) = 0;
Break
}
Z_type_p (OP) = Is_bool;
}
/* }}} */

Case Is_string This code is an operation that converts a string-type variable to a Boolean. You can see that there is only an empty string, or the string length is 1, and this character is 0 o'clock, and the string has a Boolean value of 1, true, and 0, or false.

In the same way, we understand how Boolean values are converted to strings and can be learned from the implementation of the _convert_to_string function.

Seemingly simple and basic PHP problems, the root of which is the realization of the PHP implementation mechanism of the grasp. Personally, this problem is also a good topic to identify the boundaries of phper knowledge.

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.