search
HomeBackend DevelopmentPHP TutorialWhat are the methods for referencing PHP variables? Summary of methods of php variable reference mechanism

What this article brings to you is what are the methods of referencing PHP variables? This summary of the reference mechanism of PHP variables has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Variable reference, "&" in php:

##

$a  = "hello world";//定义一个变量,下面赋值给$b
$b = $a;            //这一步实际上原理是会将变量$a复制拷贝一份,也就是内存中重新申请一个地址存储变量$b

Note: In PHP, using "

=" to directly assign a value actually copies the variable on the right to $b, which will generate a memory space. The result may be that the same content is stored in two copies in the memory.

2. Use the symbols “&” to quote

##

$a  = "hello world";
$b = &$a;
Note: When using references, the PHP engine will not copy a variable. In fact, it points the pointer to the address of $a in memory, and $b stores this pointer.

So when using references,
change the value of $b, $a will also change accordingly.

##

$a  = "hello world";
$b = &$a;
$b = "test new value";  //把b的值改掉,a的值也会跟着改变
echo $a;                //输出test new value,因为改变了b的值也会改变a的值。
I often see situations like this when defining functions:

function test (&$param){
    $param++;
}
$k = 8;
test($k);
echo $k; //结果$k的值被函数里面改变了,输出9。
Note: $param is preceded by a reference, so the parameters passed in will not be copied in memory. Instead, it directly references the original memory space. Therefore: If the variable value passed in using the symbol & is modified, the value in the original memory space will also be

changed.


#3. When destroying variables. It will not change the original value.

$a = 'd';
$b = &$a;
$b = 8;   
var_dump($b,$a);   //因为是引用了,所以把b的值改掉,a的值也跟着改为8了。
unset($b);         //调用unset删除b变量,a变量不会删除
var_dump($b,$a);   //输出null和8
Note: When calling unset to delete the $b variable, the php engine found from the variable symbol table: the variable I want to delete $b originally refers to the variable $a, which is difficult to delete, because once deleted, the $a variable is gone, so we first copy the $a variable and then delete the $b variable.

Related recommendations:

php uses open and fwrite to export files (code) in multiple formats

phpExcel in Solution to memory overflow when exporting files

The above is the detailed content of What are the methods for referencing PHP variables? Summary of methods of php variable reference mechanism. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
What data can be stored in a PHP session?What data can be stored in a PHP session?May 02, 2025 am 12:17 AM

PHPsessionscanstorestrings,numbers,arrays,andobjects.1.Strings:textdatalikeusernames.2.Numbers:integersorfloatsforcounters.3.Arrays:listslikeshoppingcarts.4.Objects:complexstructuresthatareserialized.

How do you start a PHP session?How do you start a PHP session?May 02, 2025 am 12:16 AM

TostartaPHPsession,usesession_start()atthescript'sbeginning.1)Placeitbeforeanyoutputtosetthesessioncookie.2)Usesessionsforuserdatalikeloginstatusorshoppingcarts.3)RegeneratesessionIDstopreventfixationattacks.4)Considerusingadatabaseforsessionstoragei

What is session regeneration, and how does it improve security?What is session regeneration, and how does it improve security?May 02, 2025 am 12:15 AM

Session regeneration refers to generating a new session ID and invalidating the old ID when the user performs sensitive operations in case of session fixed attacks. The implementation steps include: 1. Detect sensitive operations, 2. Generate new session ID, 3. Destroy old session ID, 4. Update user-side session information.

What are some performance considerations when using PHP sessions?What are some performance considerations when using PHP sessions?May 02, 2025 am 12:11 AM

PHP sessions have a significant impact on application performance. Optimization methods include: 1. Use a database to store session data to improve response speed; 2. Reduce the use of session data and only store necessary information; 3. Use a non-blocking session processor to improve concurrency capabilities; 4. Adjust the session expiration time to balance user experience and server burden; 5. Use persistent sessions to reduce the number of data read and write times.

How do PHP sessions differ from cookies?How do PHP sessions differ from cookies?May 02, 2025 am 12:03 AM

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

How does PHP identify a user's session?How does PHP identify a user's session?May 01, 2025 am 12:23 AM

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

What are some best practices for securing PHP sessions?What are some best practices for securing PHP sessions?May 01, 2025 am 12:22 AM

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

Where are PHP session files stored by default?Where are PHP session files stored by default?May 01, 2025 am 12:15 AM

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use