


PHP Reference Passing and Reference & Some Usage Introduction_PHP Tutorial
Quotation in PHP is done using &. Now I will introduce to you some examples of usage and quotation issues and issues in PHP. Friends are welcome to enter for reference.
What is a quote
Quoting in PHP means accessing the same variable content with different names. This is not like a C pointer; instead, the reference is a symbol table alias. Note that in PHP, variable names and variable contents are different, so the same content can have different names. The closest analogy is Unix's filenames and the files themselves - the variable names are the directory entries, and the variable contents are the files themselves. References can be thought of as hardlinks in Unix file systems.
What does a quote do
PHP’s references allow two variables to point to the same content.
When $a =& $b; $a and $b point to the same variable.
Tip: $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.
You can pass a variable to a function by reference so that the function can modify the value of its argument. The syntax is as follows:
The code is as follows | Copy code | ||||||||||||
{ $var++; }
foo($a); echo $a;//The output is: 6
|
Regarding the role of PHP references (that is, adding the ampersand in front of variables, functions, objects, etc.), let’s first look at the following program.
The code is as follows | Copy code | ||||
"; echo "$b "; $a++; //Variable a increases by 1 echo "$a "; echo "$b ";//Check variable b, it also increased by 1, indicating that the same storage unit is used ?> Program execution result: 100 100 101 101 |
The code is as follows | Copy code |
$a = 20; $b = $a; $a = $a + 10; echo $a.' and '.$b; ?> |
The code is as follows | Copy code |
$a = 20; $b = &$a; $a = $a + 10; echo $a.' and '.$b; ?> |
Program execution result:
That is to say, & passes the address of $a to $b. In this case, the two variables now share a memory storage area, which means that their values are the same.
The same syntax can be used in functions, which return references, and in the new operator:
The code is as follows | Copy code | ||||||||
1
3 $foo =& find_var($bar);
|
The code is as follows | Copy code | ||||||||
function foo(&$val1, $val2) { $val1 += 1;
}
|
The code is as follows | Copy code |
$a = 1; $b = &$a; //$a and $b point to the same content $b = 2; echo $b; //2 echo $a; //2 Pass reference Passing by reference is very simple, it is just an "&" symbol, such as: function foo(&$a) { $a = 2; } $b = 1; foo($b); echo $b; //2 |
The code is as follows | Copy code |
class foo { Public $value = 42; public function &getValue() { // Requires a "&" return $this->value; } } $obj = new foo; $myValue = &$obj->getValue(); // One more "&" is needed, $myValue is a reference to $value in class foo $obj->value = 2; // Modify the $value attribute of the object echo $myValue; // Output 2, $myValue is the same as $value in class foo |
Differences from pointers
References are very similar to pointers, but they are not pointers. See the following code:
The code is as follows | Copy code |
代码如下 | 复制代码 |
$a = 0; $b = &a; echo $a; //0 unset($b); echo $a; //0 |
$b = &a;
echo $a; //0代码如下 | 复制代码 |
#include printf("%in", a); //0 |
The code is as follows | Copy code |
#include |
Since b is a pointer to a, after releasing the memory of b, an error will occur when accessing a again. This clearly illustrates the difference between PHP references and C pointers.
Objects and References
When using objects in PHP, everyone is always told that "objects are passed by reference". In fact, this is a misunderstanding. The object variable of PHP stores an identifier of the object. When passing the object, it is actually the identifier that is passed, not the reference. See the following code:
The code is as follows | Copy code | ||||
|
If the object is passed by reference, then the output content of $a, $b, $c should be the same, but in fact the result is not like this. Look at the following example of passing objects by reference:
The code is as follows
|
Copy code
|
||||
$aa = new A; |
* The relationship between $aa and $bb at this time:
*
*** * $bb --> | object id | ---> | object(Class A) |
* +-----------+ * ^ * * $aa ---------+
*
*
*/
$cc = new B;
$aa = $cc;
$aa->testB = "Changed Class B";

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc


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

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

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
