Home  >  Article  >  Backend Development  >  operators in php

operators in php

WBOY
WBOYOriginal
2016-07-25 08:45:531200browse

1. Arithmetic operators

  1. + (Add) $a + $b
  2. - (Subtract) $a - $b
  3. * (Multiple) $a * $b
  4. /(Divide) $a / $b
  5. % (Remainder) $ a % $b
Copy code

2. String operators

  1. . (dot) (the only string operator in php)
Copy code

3. Assignment operator
1. Simple assignment operator

  1. = (equal sign)
Copy code

2. Compound assignment operator

  1. += $a += $b is equivalent to $a = $a + $b
  2. -= $a -= $b is equivalent to $a = $a - $b
  3. *= $a *= $b is equivalent to $a = $a * $b
  4. /+ $a /= $b is equivalent to $a = $a / $b
  5. %= $a %= $b is equivalent to $a = $a % $b
  6. .= $a .= $b is equivalent to $a = $a . $b
Copy code

3. Pre-increment and decrement operations and post-increment and decrement operations

  1. $a++ The value of $a itself has not changed, but the value of the entire expression will become $a + 1
  2. ++$a The value of $a itself has changed, $a is first replaced by $a = $a + 1, and then return $a + 1
  3. $a-- The value of $a itself has not changed, but the value of the entire expression will become $a - 1
  4. --$a The value of $a itself has changed, $a First $a = $a - 1, then return $a + 1
Copy code

4. Reference operator

  1. &
Copy code

The reference operator & can be used in associative assignment. Usually, when assigning the value of one variable to another variable, a copy of the metavariable is first made and then stored elsewhere in memory. For example:

  1. $a = 5;
  2. $b = $a;
Copy code

In the above example, the first line of code assigns a value to $a; the second line of code first generates a copy of $a and then saves it in $b. If the value of $a is subsequently changed, the value of $b will not change. Looking at the example below:

  1. $a = 5;
  2. $b = &$a;
  3. $a = 7; // $a and $b are now both 7
Copy code

Note: The reference is not an independent second pointer, but a pointer using the original variable, that is, both $a and $b point to the same address in the memory. In the above example, the second line is $a referenced by $b. When the value of $a in the third line changes, the $b that referenced it also changes. We can break this reference association by resetting:

  1. unsert($a);
Copy code

Note: This reset only resets $a, it does not change the value of $b(7). unsert($a) only destroys the association between $a and the value 7 stored in memory. Unsert($a) can be understood as canceling $a.

4. Comparison operators
Comparison operators return logical Boolean values: true or false.

  1. ==(equal to)
  2. ===(constantly equal to)
  3. !=(not equal to)
  4. !==(not equal to)
  5. <>(not equal to)
  6. <(less than)
  7. > ;(greater than)
  8. <==(less than or equal to)
  9. >==(greater than or equal to)
Copy code

5. Logical operators

  1. ! (Not)
  2. && (AND)
  3. || (OR)
  4. and (AND)
  5. or (OR)
  6. xor (XOR) $a xor $b If $a or $b is true, then Return true. If $a and $b are both true or both false, return false.
Copy code

Note: and and or have lower priority than && and ||.

6. Bit operators
Bit operators can treat an integer variable as a sequence of bits (Bits). Bitwise operators are not used often.

  1. & (bitwise AND) $a & $b The result of ANDing each bit of $a and $b
  2. | (bitwise OR) $a | $b The result of ANDing each bit of $a and $b The result obtained by performing the "OR" operation on each bit of b
  3. ~ (bitwise NOT) ~$a The result obtained by performing the "NOT" operation on each bit of $a
  4. ^ (bitwise XOR) $a ^ $ b The result of performing the "XOR" operation on each bit of $a and $b
  5. << (left shift) $a << $b Shift $a to the left by $b bits
  6. >> (right shift) $a >> $b Shift $a right by $b
Copy code

7. Other operators

  1. , (comma) is used to separate function parameters or other list items. This operator is often used incidentally (not independently).
  2. new (initializing an instance of a class)
  3. -> (accessing members of a class)
Copy code

1. Ternary operator?:

  1. condition ? value if true : value if false
Copy code

The ternary operator can be seen as the abbreviation of if else conditional statement.
2. Error suppression operator

  1. @(at symbol)
Copy code

The error suppression operator @ can be used before any expression, that is, before any expression that has a value or can be calculated, for example:

  1. $a = @(57 / 0);
Copy code

If the error suppression operator @ is not used in the above example, then this line of code will throw a divide-by-0 warning. If @ is used, the warning will be suppressed, that is, not thrown.
If some warnings are suppressed through this method and a warning is encountered, it needs to be handled through the error handling statements we have written in advance.
If the track_errors feature in php.ini is enabled, error messages will be stored in the global variable $php_errormsg.
3. Execution operator

  1. `` (a pair of back single quotes) The execution operator is actually a pair of operators, a pair of back single quotes.
Copy code

php will try to execute commands between back single quotes as server-side commands. The value of the expression is the result of the command execution. For example, in a unix system, you can use:

  1. $out = `ls -la`;
  2. echo '
     ' . $out . '
    ';
Copy code

On Windows server, you can use:

  1. $out = `dir c:`;
  2. echo '
     ' . $out . '
    ';
Copy code

In both cases, a directory list will be obtained and the list will be saved in $out. Then, the list will be displayed in the browser or processed by other methods.
4. Array operators
Note: In the syntax description below, $a and $b are not ordinary scalar values, but array types

  1. + (Union) $a + $b Returns an array containing all elements in $a and $b
  2. == (Equivalent) $ == $b if $a and $b have the same key value Yes, return true
  3. === (identity) $a === $b If $a and $b have the same key-value pair and the same order, return true
  4. != (non-equivalence) $a != $b If $a and $b are not equivalent, return true
  5. <> (not equivalent) $a <> $b If $a and $b are not equivalent, return true
  6. !== (Non-Identity) $ !== $b If $a and $b are not identical, return true
Copy code

5. Type operator
instanceof (the only type operator), this operator is used in object-oriented programming.
The instanceof operator allows checking whether an object is an instance of a specific class. For example:

  1. class sampleClass();
  2. $myObject = new sampleClass();
  3. if ($myObject instanceof sampleClass) {
  4. echo 'myObject is an instance of sampleClass';
  5. }
  6. ?>
Copy code
php


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