The difference between php references and pointers
Essence:
The reference is an alias and the pointer is the address
Specific:
1. Phenomenonally speaking, a pointer can change the value it points to at runtime, but a reference will not change once it is bound to an object. This sentence can be understood as: a pointer can be reassigned to point to a different object. However, the reference always points to the object specified during initialization and cannot be changed later, but the content of the specified object can be changed.
2. From the perspective of memory allocation, the program allocates a memory area for pointer variables but not for references, because the reference must be initialized when it is declared to point to an existing object. References cannot point to null values.
3. From a compilation perspective, the program adds pointers and references to the symbol table respectively during compilation. The symbol table records the variable name and the address corresponding to the variable. The address value corresponding to the pointer variable in the symbol table is the address value of the pointer variable, and the address value corresponding to the reference in the symbol table is the address value of the referenced object.
The symbol table will not be changed after it is generated, so the pointer can change the object pointed to (the value in the pointer variable can be changed), but the reference object cannot be changed. This is the main reason why using pointers is unsafe and using references is safe. In a sense a reference can be thought of as a pointer that cannot be changed.
4. The fact that there is no reference to a null value means that code using references is more efficient than using pointers. Because there is no need to test the validity of a reference before using it. In contrast, pointers should always be tested to prevent them from being null.
5. In theory, there is no limit to the number of pointer levels, but the reference can only be one level. As follows:
int** p1; // 合法。指向指针的指针 int*& p2; // 合法。指向指针的引用 int&* p3; // 非法。指向引用的指针是非法的 int&& p4; // 非法。指向引用的引用是非法的
Attention! The above reading is from left to right.
Recommended tutorial: PHP video tutorial
The above is the detailed content of The difference between php references and pointers. 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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6
Visual web development tools

SublimeText3 Chinese version
Chinese version, very easy to use

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
