【PHP基础知识】$GLOBALS['']和global区别
在php程序开发中,很多开发者没有注意到$GLOBALS[]和global存在的差别,这两种写法其实差别挺大的、并非只是字面上面的差别,下面我来了解一下他们的具体差别吧。
具体差别
1.$GLOBALS['var']是外部的全局变量本身(实实在在的外部$var本身)。
2.global $var是外部$var的同名引用或者指针(可以理解为是一个外部$var的替身)。
举例说明一下:
Copy to Clipboard引用的内容:[www.bkjia.com] $var1 = "test1";$var2 = "test2";
function test(){
$GLOBALS['var2'] = &$GLOBALS['var1'];
}
test();
echo $var2; // 将输出 test1
?>
上面代码的输出结果为 test1
Copy to Clipboard引用的内容:[www.bkjia.com] $var1 = "test1";$var2 = "test2";
function test(){
global $var1,$var2;
$var2 = &$var1;
}
test();
echo $var2; // 将输出test2
?>
上面代码的输出有点意外,结果为test2
为什么会输出test2呢?其实就是因为$var1的引用指向了$var2的引用地址(通俗的讲test函数中的$var1是一个替身)。导致实质的值没有改变。
我们再来看一个例子。
Copy to Clipboard引用的内容:[www.bkjia.com] $var1 = "test1";function test(){
unset($GLOBALS['var1']);
}
test();
echo $var1; // 什么也输出不了
?>
因为$var1已经被真实的删除了,所以什么东西输出不了。
Copy to Clipboard引用的内容:[www.bkjia.com] $var1 = "test1";function test(){
global $var1;
unset($var1);
}
test();
echo $var1; // 输出 test1
?>
这次又意外的输出了test1。证明删除的只是别名或引用(替身),变量本身的值没有受到任何的改变。
明白了吧?
也就是说global $var其实就是$var = &$GLOBALS['var']调用外部变量的一个别名而已。

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



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.

The difference between Ethereum and Bitcoin is significant. Technically, Bitcoin uses PoW, and Ether has shifted from PoW to PoS. Trading speed is slow for Bitcoin and Ethereum is fast. In application scenarios, Bitcoin focuses on payment storage, while Ether supports smart contracts and DApps. In terms of issuance, the total amount of Bitcoin is 21 million, and there is no fixed total amount of Ether coins. Each security challenge is available. In terms of market value, Bitcoin ranks first, and the price fluctuations of both are large, but due to different characteristics, the price trend of Ethereum is unique.

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,

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

There is no built-in sum function in C language, so it needs to be written by yourself. Sum can be achieved by traversing the array and accumulating elements: Loop version: Sum is calculated using for loop and array length. Pointer version: Use pointers to point to array elements, and efficient summing is achieved through self-increment pointers. Dynamically allocate array version: Dynamically allocate arrays and manage memory yourself, ensuring that allocated memory is freed to prevent memory leaks.

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.

PHP and Laravel are not directly comparable, because Laravel is a PHP-based framework. 1.PHP is suitable for small projects or rapid prototyping because it is simple and direct. 2. Laravel is suitable for large projects or efficient development because it provides rich functions and tools, but has a steep learning curve and may not be as good as pure PHP.

An application that converts XML directly to PDF cannot be found because they are two fundamentally different formats. XML is used to store data, while PDF is used to display documents. To complete the transformation, you can use programming languages and libraries such as Python and ReportLab to parse XML data and generate PDF documents.
