


Detailed introduction to PHP variable reference and object reference_PHP tutorial
The article summarizes how to make variable references in PHP and what are variable references? How to do it? Let’s introduce the usage of PHP variable references one by one.
What does a quote do
PHP's references allow two variables to point to the same content. Meaning, when doing this:
The code is as follows | Copy code |
代码如下 | 复制代码 |
$a =& $b; ?> |
?>
This means $a and $b point to the same variable.
$a and $b are exactly the same here. It’s not that $a points to $b or vice versa, but that $a and $b point to the same place.
Note:
If an array with a reference is copied, its value will not be dereferenced. The same is true for passing array values to functions.
Note:
If an undefined variable is assigned by reference, passed by reference, or returned by reference, the variable will be automatically created.
Example #1 Using references to undefined variables
代码如下 | 复制代码 |
function foo(&$var) { } foo($a); // $a is "created" and assigned to null $b = array(); $c = new StdClass;
|
The code is as follows | Copy code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
function foo(&$var) { }
foo($a); // $a is "created" and assigned to null $b = array(); foo($b['b']);var_dump(array_key_exists('b', $b)); // bool(true) $c = new StdClass; foo($c->d);
var_dump(property_exists($c, 'd')); // bool(true)
It should be noted that if test(1); is used here, an error will occur. You have to think about the reason yourself Function reference returns
The above code is the effect of running in PHP5 The role of quotation
When you declare a variable with global $var you actually create a reference to the global variable. That is the same as doing this:
This means that, for example, unset $var will not unset a global variable. $this
In an object method, $this is always a reference to the object that calls it.
//Another little episode below In layman terms 1: If there is the following code$a="ABC"; $b=$a; In fact, at this time, $a and $b both point to the same memory address, rather than $a and $b occupying different memories 2: If you add the following code to the above code Since the data in the memory pointed to by $a and $b needs to be rewritten, the Zend core will automatically determine at this time and automatically generate a data copy of $a for $b and re-apply for a piece of memory for storage
Think of global $var; as shorthand for $var =& $GLOBALS['var'];. Thus assigning another reference to $var only changes the reference to the local variable.
The answers to php interview questions are as follows: truehttp: //www.bkjia.com/PHPjc/631279.html TechArticleThe article summarizes how to make variable references in php and what are variable references? How to do it? Let’s introduce the usage of PHP variable references one by one. What to do with PHP quotes...
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 UndressAI-powered app for creating realistic nude photos ![]() AI Clothes RemoverOnline AI tool for removing clothes from photos. ![]() Undress AI ToolUndress images for free ![]() Clothoff.ioAI clothes remover ![]() Video Face SwapSwap faces in any video effortlessly with our completely free AI face swap tool! ![]() Hot Article
Roblox: Grow A Garden - Complete Mutation Guide
3 weeks ago
By DDD
Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How to fix KB5055612 fails to install in Windows 10?
3 weeks ago
By DDD
Blue Prince: How To Get To The Basement
1 months ago
By DDD
Nordhold: Fusion System, Explained
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
![]() Hot Tools![]() Notepad++7.3.1Easy-to-use and free code editor ![]() SublimeText3 Chinese versionChinese version, very easy to use ![]() Zend Studio 13.0.1Powerful PHP integrated development environment ![]() Dreamweaver CS6Visual web development tools ![]() SublimeText3 Mac versionGod-level code editing software (SublimeText3) ![]() Hot Topics
Java Tutorial
![]() ![]()
CakePHP Tutorial
![]() ![]()
Laravel Tutorial
![]() ![]()
PHP Tutorial
![]() ![]()
C# Tutorial
![]() ![]() ![]() JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably, ![]() What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency. ![]() Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead. ![]() PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners. ![]() PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved. ![]() PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7 ![]() PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers. ![]() PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries. ![]() |