Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set the variable assignment of PHP

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces how the variable assignment of PHP is set, the content is very detailed, interested friends can refer to, I hope it can be helpful to you.

Variable assignment of PHP

It is estimated that many people will disdain this title, variable assignment? Excuse me? Let's learn the first lesson of development. However, it is such a basic thing that many people will be blinded by, for example, the relationship between values and references. Today, let's talk about it in detail.

First of all, there is no need to say much about defining variables and assignments.

$a = 1, new stdClass (b) ='2, [4, 5, 6];

Four variables, which define the objects of integer, string and array respectively. These are the four types we have to deal with every day.

The variable then assigns a value to the variable.

$A1 = $a * b1 = $b * c1 = $c * d1 = $d

Note that the first three assignments are normal assignments, that is, copies of the specific content. When we modify $A1, $a will not change. A1 is the newly opened memory space that holds our values. That is, their values are the same, but the memory addresses are not the same. It's just two unrelated people who look alike.

But $D1 and $d are not. Not only are the values the same, but the memory addresses are also the same. This situation is what we call reference assignment. When $D1 changes, $D2 also changes.

It can be said that a reference assignment is to establish a shortcut under Windows or a soft link in Linux for the original variable.

Use specific examples to illustrate, first of all, the assignment of ordinary values:

/ / normal assignment $v ='1 arr1;$arr2 [1] = 5 printout r ($arr1); / / [1 arr1;$arr2 [1] = 5 printmaker ($arr1); / / [1]

$c has no effect on the value of $v. $arr2 modifies subscript 1, that is, the second number is 5, which certainly has no effect on $arr1.

What about reference assignments in the form of objects?

/ / the objects are all referenced assignment class A {public $name ='I am an echo;} $a = new A (); $b = $a name echo $a-> name, PHP_EOL; / /'I am A'echo $b-> name, PHP_EOL; / /'I am an Abed echo b-> name ='I am a name, PHP_EOL; / /'I am a'

As expected, after $b modified the content of the name property, the name in $a became the modified content of $b.

In this case, if the object wants to be passed otherwise than by reference, one is to use _ _ clone (), that is, the prototype pattern, to make its own copy. Second, re-new one from the outside.

/ / use cloning to solve the problem of citation passing class Child {public $name ='I am a subordinate of A1';} class A1 {public $name ='I am Abigail; public $child; function _ construct () {$this- > child = new Child ();} function _ clone () {$this- > name = $this- > name; / / new or cloning with Child can / / $this- > child = new Child () $this- > child = clone $this- > child;}} $A1 = new A1 (); echo $A1-> name, PHP_EOL; / / output the original content of A1 echo $A1-> child- > name, PHP_EOL;$b1 = $A1 X echo $b1-> name, PHP_EOL; / / b1 is now also the content of A1 echo $b1-> child- > name, PHP_EOL;$b1- > name ='I am B1'; / / b1 modified content $b1-> child- > name ='I am a subordinate of B1' Echo $A1-> name, PHP_EOL;// A1 becomes b1 content echo $A1-> child- > name, PHP_EOL;// uses _ _ clone$b2 = clone$ b1; / / b2 clone b1 $b2-> name ='I am a B2 subordinate'; / / b2 modify content $b2-> child- > name ='I am a subordinate of B2'; echo $b1-> name, PHP_EOL;// b1 will not become b2 modified content echo $b1-> child- > name, PHP_EOL;echo $b2-> name, PHP_EOL / / the modified content of b2 is no problem. B1 and b2 are not a single product. Echo $b2-> child- > name, PHP_EOL

The reference to the object does make it easy to get hooked. Especially for more complex objects, internal properties and various references to other objects. In this case, be sure to carefully confirm whether the reference assignment will cause a problem, and if there is a problem, use new objects or cloning techniques to deal with the reference problem.

Finally, relax, the assignment of the reference variable is the same as we pass the reference parameter to the method, just use a & symbol!

/ / reference assignment $b = & $v _ position _ echo _ v, PHP_EOL

Today we have a deeper study and understanding of the assignment problem in PHP, especially the general assignment and reference assignment. Next time you look at the code and framework, you can pay attention to how others flexibly use these two assignments, ha, you can also try to use these two ways to transform the BUG you have written!

Test code: https://github.com/zhangyue0503/dev-blog/blob/master/php/201910/source/PHP%E7%9A%84%E5%8F%98%E9%87%8F%E8%B5%8B%E5%80%BC.php reference documentation: https://www.php.net/manual/zh/language.variables.basics.php on how to set the variable assignment of PHP is shared here, I hope the above content can be of some help to you. You can learn more. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report