operators in php

Jul 25, 2016 am 08:45 AM

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 of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles