Operators in php

Source: Internet
Author: User
Tags bitwise operators
Operators in php

I. arithmetic operators

  1. + (Plus) $ a + $ B
  2. -(Minus) $ a-$ B
  3. * (Multiplication) $ a * $ B
  4. /(Except) $ a/$ B
  5. % (Remainder) $ a % $ B

II. string operators

  1. . (Point) (the only string operator in php)

III. assignment operators
1. simple value assignment operator

  1. = (Equal to no)

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

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

  1. The value of $ a ++ $ a is not changed, but the value of the entire expression is changed to $ a + 1.
  2. ++ $ A's value has changed. $ a is first $ a = $ a + 1, and then $ a + 1 is returned.
  3. $ A -- $ a's own value has not changed, but the value of the entire expression will become $ a-1
  4. -- $ A's value has changed. $ a is first $ a = $ a-1, and then $ a + 1 is returned.

4. Reference operators

  1. &

Reference operator & can be used in correlated values. Generally, when the value of a variable is assigned to another variable, a copy of the variable is generated first, and then stored elsewhere in the memory. For example:

  1. $ A = 5;
  2. $ B = $;

In the above example, the first line of code is to assign a value to $ a, and the second line of code is to generate a copy of $ a, and then save it in $ B. If you change the value of $ a subsequently, the value of $ B will not change. Let's look at the example below:

  1. $ A = 5;
  2. $ B = & $;
  3. $ A = 7; // $ a and $ B are now both 7

Note: The reference is not an independent second pointer, but uses the pointer of the original variable, that is, both $ a and $ B point to the same address of the memory. In the above example, the second row is $ a referenced by $ B. When the value of $ a in the third row changes, the $ B that has referenced it also changes. We can reset to break this reference Association:

  1. Unsert ($ );

Note: This reset only resets $ a and does not change the value of $ B (7). unsert ($ a) only destroys the association between $ a and 7 stored in the memory. Unsert ($ a) can be interpreted as deregistering $.

IV. Comparison operators
The comparison operator returns a logical boolean value: true or false.

  1. = (Equal)
  2. ===( Constant equals)
  3. ! = (Not equal)
  4. ! = (Not constant)
  5. <> (Not equal)
  6. <(Less)
  7. > (Greater)
  8. <== (Less than or equal)
  9. >== (Greater than or equal)

5. logical operators

  1. ! (Not)
  2. & (And)
  3. | (Or)
  4. And (and)
  5. Or (or)
  6. Xor (exclusive or) $ a xor $ B returns true if $ a or $ B is true. If both $ a and $ B are true or both false, false is returned.

Note: and or are lower than & |.

VI. bitwise operators
Bitwise operators can treat an integer variable as the Bit (Bit, Bit) of some columns. Bit operators are not frequently used.

  1. & (Bitwise AND) $ a & $ B perform the "and" operation on each digit of $ a and $ B.
  2. | (By bit or) $ a | $ B performs the "or" operation on each digit of $ a and $ B.
  3. ~ (Not by bit )~ $ A: the result of a "non" operation on each digit of $.
  4. ^ (Bitwise OR) $ a ^ $ B returns the result of the "xor" operation for each digit of $ a and $ B
  5. <(Left shift) $ a <$ B shifts $ a left to $ B
  6. >>( Right shift) $ a >>$ B shifts $ a to the right of $ B

7. Other operators

  1. , (Comma) is used to separate function parameters or other list items. this operator is often used with (not independent.
  2. New (initialization class instance)
  3. -> (Member of the category class)

1. ternary operators? :

  1. Condition? Value if true: value if false

The ternary operator can be seen as a shorthand for the if else conditional statement.
2. error suppression operators

  1. @ (At symbol)

Error suppression operator @ can be used before any expression, that is, before any expression with a value or a value that can be calculated, for example:

  1. $ A = @ (57/0 );

If the error suppression operator @ is not used in the above example, the code in this row will throw a warning except 0. if @ is used, the warning will be blocked, that is, it will not be thrown.
If we use this method to suppress some warnings and encounter one warning, we need to write a statement to handle errors in advance.
If the track_errors feature in php. ini is enabled, the error message is saved in the global variable $ php_errormsg.
3. execution operators

  1. ''(A pair of reverse single quotes) is actually a pair of operators, which is a pair of reverse single quotes.

Php will try to run the command between single quotes in reverse direction as a server command. The expression value is the execution result of the command. For example, in a unix system, you can use:

  1. $ Out = 'ls-LA ';
  2. Echo'
     ' . $out . '
    ';

On windows Server, you can use:

  1. $ Out = 'dir c :';
  2. Echo'
     ' . $out . '
    ';

In both cases, a directory list is obtained and saved in $ out. then, the list is displayed in the browser or processed in other ways.
4. array operators
Note: In the following syntax description, $ a and $ B are not common scalar values, but arrays.

  1. + (Union) $ a + $ B returns an array containing all elements in $ a and $ B.
  2. = (Equivalent) $ ==$ B returns true if $ a and $ B have the same key-value pair.
  3. ===( Constant) $ a ===$ B if $ a and $ B have the same key-value pairs and the same order, true is returned.
  4. ! = (Non-equivalent) $! = $ B if $ a and $ B are not equivalent, true is returned.
  5. <> (Non-equivalent) $ a <> $ B returns true if $ a and $ B are not equivalent.
  6. ! ==( Non-constant) $! = $ B if $ a and $ B are not constant, true is returned.

5. type operators
Instanceof (a unique type operator), which is used in object-oriented programming.
The instanceof operator allows you to check 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. ?>

Php

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.