


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('error'); //object;
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
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;
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;
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;//布尔值
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
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
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);
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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

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

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

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:

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

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

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
