


Analysis of citation reasons for using PHP with caution_PHP Tutorial
Reference types are used in many computer languages and exist as a very powerful and practical feature. It has an implementation similar to a pointer, but behaves differently from a pointer. For example, C++ references can allow different variables to point to the same object, while maintaining the direct use of dot to obtain object members, without the cumbersome use of dereference operator (*) and Pointer to Member operator (->). In Java and C#, references are directly used as the main type, and developers try to avoid using pointers.
Reference types have also been introduced in PHP. In terms of object assignment and transfer, it can basically be regarded as the same reference transfer as Java/C# (see Objects and references for details). But at the same time, it supports obtaining a reference to the content through the reference operator (&) on the basic type. However, in actual use, PHP's reference type has many problems due to the entire PHP design structure, causing unexpected results in the program.
Reference variables can be assigned new references
In C++, a reference type variable can only be assigned a reference value when it is defined, so we As long as you trace the definition of the variable, you can know what the variable is operating on.
But PHP is different. The definition of variables is blurred in PHP, and variables can be used without defining them. So a variable can be assigned a reference value multiple times.
$x = 21;
$y = 7;
$z = &$x;
$z = &$y;
var_dump($x,$y,$z);
At first glance, let The human feeling is that $z becomes a reference to $x, and then the content of $z becomes a reference to $y. That is to say, both $x and $z become references to $y. But the actual output result is:
int(21)
int(7)
int(7)
From the result, $x remains unchanged, but $z is changed to a reference to $y. It is equivalent to unsetting the $z variable first and then assigning a new value.
$z = &$x;
unset($z);
$z = &$y;
This is actually more reasonable logic. For example, in the code below, we do not get something like "Pointer point to a Pointer" The "Reference refer to a Reference" is just a reference variable that refers to the same piece of content.
$x = 21;
$y = &$x;
$z = &$y
Referring to an array element will make the element a reference type
For variable reference, it will not This causes the original variable type to change, but if the element in the array is taken, the element will also become a reference type.
Before looking at the question code, the first thing to point out is:
Array assignment always involves value copying. Use the reference operator to copy an array by reference.
That is to say, PHP Array assignment is a copy rather than a reference. The assignment process will create a new array and assign it to the assigned variable. Array operations on the new variable will not affect the contents of the original array variable.
$a = array(21, 7);
$b = $a ;
$b[0] = 7;
var_dump($a);
echo '
';
var_dump($b);
/ /Output:
//array(2) { [0]=> int(21) [1]=> int(7) }
//array(2) { [0]=> int(7) [1]=> int(7) }
Let’s take a look at what exceptions will occur if we reference elements in the array.
$a = array(21, 7);
$c = & $ a[0];
$b = $a;
$b[0]= "21";
$b[1]= "7";
var_dump($a );
echo '
';
var_dump($b);
echo '
';
var_dump($c);
echo '
';
// Output:
// array(2) { [0]=> &string(2) "21" [1]=> int( 7) }
// array(2) { [0]=> &string(2) "21" [1]=> string(1) "7" }
// string(2) " 21"
$b in the code is just a simple assignment, except that there is an extra reference to get the first element, but it should still copy a new array. But the result is a modification of $b, which also changes the first element of $a, while the second element has no effect.
We also see an unusual thing from the output, that is, the type of the first element of the array has an extra '&' symbol. And this is exactly the reference operator. That is to say, the first element of the array has become a reference type. Therefore, assignment is also a reference copy, not a value copy.
This problem is very strange, and it also caused a lot of unnecessary trouble during development. I originally thought that the copied array was not related to the original array, but because of this unexpected reference type, I was confused during the operation. The original array is affected.
I don’t know if this is a bug in PHP or if it is intentionally designed this way. I have been searching online for a long time but there is no relevant explanation for this convenience. Only Float Middle's "PHP: References To Array Elements Are Risky" and Symmetric Designs' "Problems w/accessing a PHP array by reference" talk about this, but No reason was given.
Later, I saw several related reports (Bug6417, Bug7412, Bug15025, Bug20993) in the PHP Bug Report. Some say this is a bug and has been fixed in later versions. I don't understand the specifics, I can only avoid using references on arrays.
The more interesting thing is that if you unset those references and leave only one, then the array elements will become normal types without references.
unset($b);
unset($c);
var_dump($a);
// Output:
//array(2) { [0]=> string(2) "21" [1]=> int( 7) }
Avoid using PHP references
This is actually something to pay attention to mentioned in the PHP Array Manual. It most often occurs in foreach, where you hope to change the value of the far array through a reference (see this article).
In fact, I want to change the value of the array element by using foreach with references, mainly because PHP's array is an Associative Array. This kind of array has "indefinite length, the index can be discontinuous, and strings and integers can be used as indexes at the same time." So we cannot simply increment the integer index using a for loop.
Of course we can directly change the value of the array element through $key like the code below, but this may have certain efficiency issues.
foreach ($array_var as $key => $value)
$array_var [$key] = $newValue;
Another common place for references is to pass parameters by reference in function calls. The main reason is to use this method to allow the function to return multiple return values. For example, we want to use a representation to indicate whether an error occurs during execution of the function and the return value is invalid.
But because PHP functions can return different types, there is no need to pass in reference parameters as representation. Even if you really need multiple return values, you can still return an "array with a string as the primary key" as a solution, but you may need to point out in the documentation that each element corresponds to that result.
A better way to operate it is to use unset on the variable immediately to switch the connection with the content whenever the referenced variable no longer needs to be used. And even if the variable is not a reference type, we confirm that it is no longer used , and there will be no problem calling unset on it. At least it is guaranteed that reassigning the variable later will not affect the previous result.
- Problems w/accessing a PHP array by reference - Symmetric Designs
- PHP: References To Array Elements Are Risky – Float Middle
- References and foreach - Johannes Schlüter
- References Explained - PHP Manual

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Key players in HTTP cache headers include Cache-Control, ETag, and Last-Modified. 1.Cache-Control is used to control caching policies. Example: Cache-Control:max-age=3600,public. 2. ETag verifies resource changes through unique identifiers, example: ETag: "686897696a7c876b7e". 3.Last-Modified indicates the resource's last modification time, example: Last-Modified:Wed,21Oct201507:28:00GMT.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.


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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Linux new version
SublimeText3 Linux latest version

Zend Studio 13.0.1
Powerful PHP integrated development environment