Home Backend Development PHP Tutorial In-depth understanding of PHP principles (Variables inside PHP)

In-depth understanding of PHP principles (Variables inside PHP)

Oct 15, 2019 pm 01:35 PM
principle variable

In-depth understanding of PHP principles (Variables inside PHP)

Maybe you know, maybe you don’t know, PHP is a weakly typed, dynamic scripting language. The so-called weak type means that PHP does not strictly verify the variable type (Strictly speaking, PHP is a medium-strong type language, this part will be described in a future article), when declaring a variable , there is no need to explicitly indicate the type of data it saves:

<?php
  $var = 1; //int
  $var = "laruence"; //string
  $var = 1.0002; //float
  $var = array(); // array
  $var = new Exception(&#39;error&#39;); //object;
Copy after login

Dynamic language, that is to say, the language structure of PHP can be changed during runtime, for example, we require during runtime A function definition file, which causes dynamic changes to the language's function table.

The so-called scripting language means that PHP does not run independently. To run PHP we need a PHP parser:

/usr/bin/php -f example.php
Copy after login

I have already mentioned in the previous article that the execution of PHP is through Zend engine (ZE, Zend engine), ZE is written in C. Everyone knows that C is a strongly typed language, that is to say, all variables in C can only save one type from the time they are declared to the final destruction. type of data. So how does PHP implement weak types based on ZE?

In PHP, all variables are saved using a structure -zval. In Zend/zend.h we can see the definition of zval:

typedef struct _zval_struct {
    zvalue_value value;
    zend_uint refcount;
    zend_uchar type;
    zend_uchar is_ref;
  } zval;
Copy after login

where zvalue_value is true Now it’s time to reveal the key to saving data. How does PHP implement weak types based on ZE? Because zvalue_value is a union,

typedef union _zvalue_value {
    long lval;
    double dval;
    struct {
        char *val;
        int len;
    } str;
    HashTable *ht;
    zend_object_value obj;
} zvalue_value;
Copy after login

So how does this structure store multiple types in PHP?

Common variable types in PHP are:

  • 1. Integer/floating point/long integer/bool value, etc.

  • 2. String

  • 3. Array/associative array

  • 4. Object

  • 5. Resources

PHP stores the real type of a variable based on the type field in zval, and then chooses how to obtain the value of zvalue_value based on type. For example, for integers and bool values:

   zval.type = IS_LONG;//整形
   zval.type = IS_BOOL;//布尔值
Copy after login

Get zval.value.lval, for bool values ​​lval∈(0|1);

If it is double precision, or float Then it will get the dval of zval.value.

And if it is a string, then:

   zval.type = IS_STRING
Copy after login

At this time, it will be: zval.value.str

And this is also a structure , storing the string in C format and the length of the string.

For arrays and objects, type corresponds to IS_ARRAY, IS_OBJECT respectively, and the corresponding ones are zval.value.ht and obj

## respectively. #What is special is the resource. In PHP, the resource is a very special variable. Any variable that does not belong to the built-in variable type of PHP will be regarded as a resource for saving, such as database handle, open file handle, etc. . For resources:

   type = IS_RESOURCE
Copy after login

At this time,

zval.value.lval will be fetched. At this time, lval is an integer indicator, and then PHP will use this indicator in PHP Query the corresponding resources in a resource list you created (I will introduce this part in a separate article in the future). For now, you only need to know that the lval at this time seems to be the offset value corresponding to the resource list.

 ZEND_FETCH_RESOURCE(con, type, zval *, default, resource_name, resource_type);
Copy after login
By borrowing this mechanism, PHP implements weak types, because for ZE, it always faces the same type, that is zval.

For more PHP related knowledge, please visit

PHP Chinese website!

The above is the detailed content of In-depth understanding of PHP principles (Variables inside PHP). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Analysis of the function and principle of nohup Analysis of the function and principle of nohup Mar 25, 2024 pm 03:24 PM

Analysis of the role and principle of nohup In Unix and Unix-like operating systems, nohup is a commonly used command that is used to run commands in the background. Even if the user exits the current session or closes the terminal window, the command can still continue to be executed. In this article, we will analyze the function and principle of the nohup command in detail. 1. The role of nohup: Running commands in the background: Through the nohup command, we can let long-running commands continue to execute in the background without being affected by the user exiting the terminal session. This needs to be run

In-depth discussion of the principles and practices of the Struts framework In-depth discussion of the principles and practices of the Struts framework Feb 18, 2024 pm 06:10 PM

Principle analysis and practical exploration of the Struts framework. As a commonly used MVC framework in JavaWeb development, the Struts framework has good design patterns and scalability and is widely used in enterprise-level application development. This article will analyze the principles of the Struts framework and explore it with actual code examples to help readers better understand and apply the framework. 1. Analysis of the principles of the Struts framework 1. MVC architecture The Struts framework is based on MVC (Model-View-Con

In-depth understanding of the batch Insert implementation principle in MyBatis In-depth understanding of the batch Insert implementation principle in MyBatis Feb 21, 2024 pm 04:42 PM

MyBatis is a popular Java persistence layer framework that is widely used in various Java projects. Among them, batch insertion is a common operation that can effectively improve the performance of database operations. This article will deeply explore the implementation principle of batch Insert in MyBatis, and analyze it in detail with specific code examples. Batch Insert in MyBatis In MyBatis, batch Insert operations are usually implemented using dynamic SQL. By constructing a line S containing multiple inserted values

What are instance variables in Java What are instance variables in Java Feb 19, 2024 pm 07:55 PM

Instance variables in Java refer to variables defined in the class, not in the method or constructor. Instance variables are also called member variables. Each instance of a class has its own copy of the instance variable. Instance variables are initialized during object creation, and their state is saved and maintained throughout the object's lifetime. Instance variable definitions are usually placed at the top of the class and can be declared with any access modifier, which can be public, private, protected, or the default access modifier. It depends on what we want this to be

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

Mind map of Python syntax: in-depth understanding of code structure Mind map of Python syntax: in-depth understanding of code structure Feb 21, 2024 am 09:00 AM

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

An in-depth discussion of the functions and principles of Linux RPM tools An in-depth discussion of the functions and principles of Linux RPM tools Feb 23, 2024 pm 03:00 PM

The RPM (RedHatPackageManager) tool in Linux systems is a powerful tool for installing, upgrading, uninstalling and managing system software packages. It is a commonly used software package management tool in RedHatLinux systems and is also used by many other Linux distributions. The role of the RPM tool is very important. It allows system administrators and users to easily manage software packages on the system. Through RPM, users can easily install new software packages and upgrade existing software

Detailed explanation of the principle of MyBatis paging plug-in Detailed explanation of the principle of MyBatis paging plug-in Feb 22, 2024 pm 03:42 PM

MyBatis is an excellent persistence layer framework. It supports database operations based on XML and annotations. It is simple and easy to use. It also provides a rich plug-in mechanism. Among them, the paging plug-in is one of the more frequently used plug-ins. This article will delve into the principles of the MyBatis paging plug-in and illustrate it with specific code examples. 1. Paging plug-in principle MyBatis itself does not provide native paging function, but you can use plug-ins to implement paging queries. The principle of paging plug-in is mainly to intercept MyBatis

See all articles