Variable correlation
Internal implementation of PHP variables
The system type of a programming language is divided into two types: strong and weak.
- A strongly typed language is one in which a variable is declared as a variable of a certain type, and it cannot be assigned to a value other than the type of the variable in the course of the program's operation, such as C/c++/java.
- Scripting languages such as PHP and Ruby,javascript are weak-type languages: A variable can represent any data type
PHP variable type and storage structure
PHP does not need to explicitly indicate its data type when declaring or using a variable
PHP is a weakly typed language, which does not mean that PHP has no type, in PHP, there are 8 types of variables, can be divided into three categories:
- Scalar type: boolean,integer,float,string
- Compound Type: array,object
- Special type: Resource,null
Variable storage structure
The value of the variable is stored in the ZVAL structure shown below. Its structure is as follows:
typedef struct _ZVAL_STRUCT zval; struct _zval_struct { zvalue_value value;//storage variable zend_uint refcount__gc;//= reference count zend_uchar type; Variable-specific type Zend_uchar is_ref_gc; Indicates whether it is a reference
The value of the variable is stored in another struct zvalue_value
Variable type
The Type field of the Zval struct is the most critical field for implementing the weak type, and the value of type can be: Is_null, Is_bool, Is_long, is_double, is_string, Is_array, Is_object, Is_ One of the resource. Literally well understood, they're just types of unique markers that store different values in the Value field depending on the type
Storage of variable values
The value of the variable is stored in the zvalue_value structure, and the structure is defined as follows:
typedef Union _ZVALUE_VALUE { long lval; Double dval; struct { char *val; int len; } STR; HashTable *ht; Zend_object_value obj;
Date related
Calculate the number of days between two dates
<?php /** * For days between two dates (Taylor formula can be used after January 1, 1970) * @param string $day 1 * @param string $day 2 * @return Number * /function Diffbetweentwodays ($day 1, $day 2) { $second 1 = strtotime ($day 1); $second 2 = strtotime ($day 2); if ($second 1 < $second 2) { $tmp = $second 2; $second 2 = $second 1; $second 1 = $tmp; } Return ($second 1-$second 2)/86400; } $day 1 = "2013-07-27"; $day 2 = "2013-08-04"; $diff = Diffbetweentwodays ($day 1, $day 2);