Some interview topics on variables and date processing in PHP organize _php examples

Source: Internet
Author: User
Tags diff php and

Variable related
Internal implementation of PHP variables
the system type of the programming language is divided into strong type and weak type two kinds:

    1. A strongly typed language is one in which a variable is declared as a variable of a type, and a value other than the type of the variable cannot be assigned to it while the program is running, and C/c++/java languages fall into this category
    2. Scripting languages such as PHP and Ruby,javascript are weakly typed languages: A variable can represent any type of data


PHP variable type and storage structure
when you declare or use a variable, PHP does not need to explicitly indicate its data type

PHP is a weak type language, which does not mean that PHP has no type, in PHP, there are 8 types of variables, can be divided into three categories:

    1. Scalar type: boolean,integer,float,string
    2. Composite type: Array,object
    3. 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 values 
    zend_uint refcount__gc;//representing reference count 
    Zend_uchar type;  Variable-specific type 
    Zend_uchar is_ref_gc;  Indicates whether it is a reference 
  }; 


The value of a variable is stored in another struct zvalue_value

Variable type
the Type field of the ZVAL structure is the most critical field for implementing a 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 understood that they were just types of unique labels, depending on the type of different values stored in the Value field

Storage of variable values
as mentioned earlier, 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; 
  } _zvalue_value; 


Date Related

Calculate the number of days between two dates

 <?php 
   
  /** 
   * Find the number of days between two dates (for January 1, 1970, before you can use Taylor Formula) 
   * @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); 
  echo $diff. " \ n "; 

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.