In PHP&Do you really know signs?

The introduction

Recently, I took over the old project five or six years ago, using the CI2.0 framework. When I looked at the source code of the framework, there was a place that made me stunned for a while, so I had this article.

The earliest history of the character & can be traced back to the 1st century AD, as a ligature of Latin et (meaning and).

& refers to the logical relations both belong to be short of one cannot, also said the meaning is the meaning of a person and another person, synonymous with the and.

& is one of the most commonly used operators in PHP projects, such as bitwise and, logical operations, referential variables, referential passing, referential return.

Bitwise and

$a & $b will set the bits in both $a and $b to 1.

Odd-even judgment

  • The integer with1Perform bitwise and operation, and the result is1Is an odd number, and the result is0It’s going to be an even number. Such as:
  • 5 and 1 for bitwise and&Is equal to 1.
The decimal system binary
5 101
1 001
  • 6 and 1 for bitwise and&, the result is 0.
The decimal system binary
6 110
1 001
  • But in the project we don’t write it that way, we just use itn % 2 == 0, because the parity judgment is used%Is more efficient and easier to understand.

User permission judgment

Suppose there is a user permission assignment module in a system, and its permission setting is as follows:

Permission to name Permission values
To view 1
new 2
Modify the 4
delete 8

So instead of storing the comma-separated string 1,2,4,8 to store the user’s permissions, you can store an int :15.

If user 1 has the permissions to add, view and modify, then the permissions to be stored are: 1+2+4=7.

If user 2 has all the permissions, the permissions that need to be stored are :1+2+4+8=15.

Check if user 1 has permission to modify: 7&4 results in 4, indicating that user 1 has permission to modify.

Check if user 2 has delete permission: 15&8 The result is 8, indicating that user 2 has delete permission.

Conditional operator

$a && $b is true only if $a and $b are both true.

Reference variables

PHP references allow you to use two variables to point to the same thing. Whenever you change the value of any variable name, the content accessed by the other variable names will also change.

It is different from the pointer in C language. C language pointer inside the storage is the contents of the variable, stored in memory address.

<? php $a = 10; $b = &$a; $a = 11; var_dump($a, $b); $b = $b = $b; var_dump($a, $b); // Output 12, 12

What’s wrong with adding ampersand to foreach loops? For example, does the following code normally output the expected value?

<? php $values = ['Python', 'Php', 'Go']; foreach ($values as &$value) { $value = strtoupper($value); } foreach ($values as $value){ echo $value . PHP_EOL; }

Expected result: [‘ PYTHON ‘, ‘PHP’, ‘GO’] the actual result is [‘ PYTHON ‘, ‘PHP’, ‘PHP’].

$arr[2] and $value refer to the same address space (the shared variable value). On the second foreach, the value in $values is assigned to $value, and the value in $arr[2] refers to the same address space. The value that causes $arr[2] has also been modified.

reference

You can pass a variable by reference to a function so that the function can change the value of its arguments.

/** * square the value passed in * Author: ClassmateLin * Email: [email protected] * Site: https://www.classmatelin.top * @param $n */ function f(&$n) { $n *= $n; } $n = 2; f($n); var_dump($n); / / output 4

Passing by value requires a copy of the variable. Passing by reference is the same memory space.

If it’s a large string or object, passing it by reference saves some memory than passing it by value, but passing it by reference makes the code slightly less readable.

Reference to return

A reference to a function is returned, with the ampersand definition before the method. You also need to receive a return value and an &, otherwise it will not work, for example:

<? php class Foo { public $value = 10; /** * returns value, the reference returns value, and changes to the returned value will affect the value. * Author: ClassmateLin * Wechat: ClassmateLin_ * Email: [email protected] * Site: https://www.classmatelin.top * @return int */ public function &getValue() { return $this->value; } } $foo = new Foo(); $val1 = $foo->getValue(); $val1 = 11; Var_dump ($val1, $foo->, $foo, $foo, $foo); $foo->getValue(); $foo->; $val2 = 13; $val2 = 13; var_dump($val2, $foo->getValue()); // Output 13, 13