Home  >  Article  >  Backend Development  >  PHP variable and date processing cases

PHP variable and date processing cases

墨辰丷
墨辰丷Original
2018-06-07 16:35:203179browse

This article mainly introduces PHP variables and date processing cases. Interested friends can refer to it. I hope it will be helpful to everyone.

Variable related
Internal implementation of PHP variables
The system types of programming languages ​​are divided into two types: strong type and weak type:

  1. Strong Type language means that once a variable is declared as a variable of a certain type, a value other than the type of the variable cannot be assigned to it during the running of the program. Languages ​​such as c/c/java fall into this category

  2. Scripting languages ​​such as php, ruby, and javascript are weakly typed languages: a variable can represent any data type


php Variable types and storage structuresWhen php declares or uses variables, it does not need to explicitly indicate its data type

php is a weakly typed language. This does not mean that php has no type. In php, There are 8 types of variables, which 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 structureThe value of the variable is stored in In the zval structure shown. Its structure is as follows:

  typedef struct _zval_struct zval; 
   
  struct _zval_struct { 
    zvalue_value value; // 存储变量的值 
    zend_uint refcount__gc; // 表示引用计数 
    zend_uchar type;  // 变量具体的类型 
    zend_uchar is_ref_gc;  // 表示是否为引用 
  };

The value of the variable is stored in another structure zvalue_value

Variable typezval structure The type field is the most critical field to implement weak types. The value of type can be one of: IS_NULL, IS_BOOL, IS_LONG, IS_DOUBLE, IS_STRING, IS_ARRAY, IS_OBJECT, IS_RESOURCE. It is easy to understand literally, they are only the only types of Mark, store different values ​​in the value field according to different types

Storage of variable valuesAs 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 
   
  /** 
   * 求两个日期之间相差的天数(针对1970年1月1日之后,求之前可以采用泰勒公式) 
   * @param string $day1 
   * @param string $day2 
   * @return number 
   */ 
  function diffBetweenTwoDays ($day1, $day2) 
  { 
    $second1 = strtotime($day1); 
    $second2 = strtotime($day2); 
     
    if ($second1 < $second2) { 
      $tmp = $second2; 
      $second2 = $second1; 
      $second1 = $tmp; 
    } 
     
    return ($second1 - $second2) / 86400; 
  } 
   
  $day1 = "2013-07-27"; 
  $day2 = "2013-08-04"; 
   
  $diff = diffBetweenTwoDays($day1, $day2); 
  echo $diff."\n";

Summary:That’s it The entire content of this article is hoped to be helpful to everyone's study.

Related recommendations:

Detailed explanation of DS extension of data structure in PHP

PHP implementation of extracting strings Mobile phone number regular expression method

PHP implementation method of generating and parsing xml based on SimpleXML

The above is the detailed content of PHP variable and date processing cases. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn