PHP Bottom Principle ____php

Source: Internet
Author: User
Tags php script zend

First , the basic principle of PHP-know its why, look at a picture

1, we write the PHP code is not directly run, it first through the lexical analyzer-parser and compiler

Here you may find that PHP is a compiler language. PHP is generally considered a scripting language, yes. But strictly speaking, PHP is also a compiler language, it will compile PHP as a opcode intermediate language, a bit like Java class file.

2, after the generation of Cpcode by the Zend company developed by the executive engine execution


Let's make a comparison.

C, C + + language
Java
Compiled into machine code (binary) to run First compile the Java file into. Class, called bytecode, and then run with the JVM
So Java is not cross-platform, but the JVM is Cross-platform
Interpreting language
Php
Interpreter interpretation execution, most classic such as: Linux shell
Interpreter to execute the command
PHP has special, although it is a scripting language, but not interpreted by interpreters.
Instead, the Zend virtual machine masks the operating system.
PHP code is compiled into opcode. Run by a virtual machine opcode
But as soon as the--opcode,php script finishes, opcode clears (Java can package the. class to publish)

3, then opcode can be cached.

PHP does not support itself, but accelerators such as Apc,xcache (if it's too wasteful to throw away a lifetime of success) Let's take a look at a summary

Zend Compiler (Php->opcode)
Zend Virtual machine (perform opcode)
Operating system Level Win/linux/mac

Second, the PHP variable bottom implementation

PHP is the bottom of the C language to achieve, C language is strong type, and PHP is a weak type language (such as the need to add variable type). So how is PHP implemented?

We unzip the source code package for PHP and see the following directory: as shown in the picture


Let's analyze it.

We open the Zend file and find the Zend.h file.

Found it

The structural body of a typedef struct _ZVAL_STRUCT zval

And look at its implementation.

struct _zval_struct {

/* Variable Information * *

Zvalue_value value; /* Value * *

Zend_uint refcount__gc;

Zend_uchar type; /* Active type */

Zend_uchar is_ref__gc;

}

The core of the PHP variable implementation is in a few lines of code above

The value of the PHP variable is described by the structure above.

It's made up of four fields

Analysis of the first, zvalue_value value;

typedef Union _ZVALUE_VALUE {//Union (enum)

Long lval;

Double Dval;

struct {

Char *val;

int Len;

};

HashTable *ht;

Zend_object_value obj;

} Zvalue_value;


Analysis of the second, Zend_uchar type;

It could have been is_null, Is_bool.


So to determine what the type of a variable is, it's based on the Zend_uchar type.

How much of its value is determined by the TypeDef union _ZVALUE_VALUE Its consortium


Let's analyze a piece of code to see how the variables in PHP are implemented.

$a = 3;

/***
A struct produces
{ 
Union_value {long 3}  //Description value type
Is_long  //description type
refcount_gc:1
is_ ref_gc:0
}
***/

The value of the Type field exists in a constant form

Let's see what it's exactly saved

Is_null, Is_bool, Is_long, is_double, is_string, Is_arrary. Is_object, Is_resource

It actually corresponds to eight types of data in the PHP array

This creates a problem where only the V data types are available in Zvalue_value, and there are three types that do not correspond

1:null directly corresponds to Zval->type=is_null, you can indicate that you do not have to set the value
2:bool type, Zval->type=is_bool, then set zval_value.lval = 1/0
3:resource type, resource type is often an interface opened on the server, if the file read interface
Zval->type=is_resource, zval->type.lval= the number of interfaces opened by the server
Here we need to pay special attention to:

PHP, String type, length is already cached, call strlen not calculated, very fast, the length is directly into the structure of the body

$b = ' Hello ';
/**
{
        char: ' Hello ';
        Len:5;
    }
    type:is_string;
    Refcount__gc:1
    is_ref__gc:0
}
**/

Let me ask you a question, where is the name of the variable?

In PHP, the name of the variable is placed in the symbol table symbol_table

Here is the headline, what is the symbol table.

The symbol table is a hashtable that stores the address of the ZVAL structure of the variable name-> variable.

Zend/zend_globals.h find Hashtable synbol_table; /* Main Synbol Table * *

Hash table We can take it as an associative array


Look at a piece of code, see declaring a variable, you have to think of things

$a = 5;
$b = 5.54321;
$c = ' Hello ';
/**
generates 3 structures
at the same time, in the global rich table, more than 3 records
a--> 0x123--> structural body {5}
b--> 0x213--> structure {5.54321}
c-- > 0x339--> structural body {Hello}
**/

Above is the global symbol table, generally by the global must have local symbol table

HashTable *active_synbol_table

The principle is the same


Let's take a look at the assignment and reference of the variable, first look at the following code:

$a = 3;

$b = $a

Whether 2 structures were generated at the bottom.

Yes, a new structure was not generated during the implementation of PHP

$a = 3
/**
zvalue:3
type:is_long
refount__gc=1
is_ref_gc:0
**/
$b = $a
/**
zvalue:3
type:is_long
refcount__gc=2 changed here
is_ref_gc:0 <pre name= "
code" class= "PHP" > They point to the same structure on the address, save space, and no replication of the structure
**/




123


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.