| Start | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Null Coalescing Operator (??) Null coalescing is a new operator introduced in PHP 7. This operator returns its first operand if it is set and not The following example: is equivalent to both: and: This operator can also be chained (with right-associative semantics): which is an equivalent to: Note: This will output This will output |
||||||||||||||||||||||||||||||||||||||||||||||||
| Spaceship Operator (<=>) PHP 7 introduces a new kind of operator, which can be used to compare expressions. This operator will return -1, 0 or 1 if the first expression is less than, equal to, or greater than the second expression. Objects are not comparable, and so doing so will result in undefined behaviour. This operator is particularly useful when writing a user-defined comparison function using In PHP 5 this would have required a rather more elaborate expression. |
||||||||||||||||||||||||||||||||||||||||||||||||
| Ternary Operator (????? The ternary operator can be thought of as an inline If the Example:
The expression It is possible to leave out the middle part of the ternary operator. Expression This behaves like the Null Coalescing operator Example: In this example, Refer to Types (opens new window) for more information about conversion of variables to boolean. |
||||||||||||||||||||||||||||||||||||||||||||||||
| Incrementing (++) and Decrementing Operators (--) Variables can be incremented or decremented by 1 with More information about incrementing and decrementing operators can be found in the official documentation (opens new window). |
||||||||||||||||||||||||||||||||||||||||||||||||
| Execution Operator (``) The PHP execution operator consists of backticks (``) and is used to run shell commands. The output of the command will be returned, and may, therefore, be stored in a variable. Note that the execute operator and |
||||||||||||||||||||||||||||||||||||||||||||||||
| Logical Operators (&&/AND and ||/OR) In PHP, there are two versions of logical AND and OR operators.
Note that the
Because of this it's safer to use |
||||||||||||||||||||||||||||||||||||||||||||||||
| String Operators (. and .=) There are only two string operators: |
||||||||||||||||||||||||||||||||||||||||||||||||
| Object and Class Operators Members of objects or classes can be accessed using the object operator ( Note that after the object operator, the Also note that This causes a 'PHP Fatal error: Uncaught Error: Using $this when not in object context' These operators have left associativity, which can be used for 'chaining': These operators have the highest precedence (they are not even mentioned in the manual), even higher that The value of Note that using parentheses to influence precedence did not work in PHP version 5 and older (it does in PHP 7): |
||||||||||||||||||||||||||||||||||||||||||||||||
| Combined Assignment (+= etc) The combined assignment operators are a shortcut for an operation on some variable and subsequently assigning this new value to that variable. Arithmetic: Processing Multiple Arrays Together (opens new window) Combined concatenation and assignment of a string: Combined binary bitwise assignment operators: |
||||||||||||||||||||||||||||||||||||||||||||||||
| Altering operator precedence (with parentheses) The order in which operators are evaluated is determined by the operator precedence (see also the Remarks section). In
The precedence can be altered using parentheses: in
|
||||||||||||||||||||||||||||||||||||||||||||||||
Basic Assignment (=) results in The result of an assignment expression is the value being assigned. Note that a single equal sign does the following:
Thus: both |
||||||||||||||||||||||||||||||||||||||||||||||||
Association Left associationIf the preceedence of two operators is equal, the associativity determines the grouping (see also the Remarks section):
Now, the modulus operator occurs first (left) and is thus grouped. Right associationBoth |
||||||||||||||||||||||||||||||||||||||||||||||||
Comparison Operators EqualityFor basic equality testing, the equal operator The identical operator works the same as the equal operator, requiring its operands have the same value, but also requires them to have the same data type. For example, the sample below will display 'a and b are equal', but not 'a and b are identical'. When using the equal operator, numeric strings are cast to integers. Comparison of objects
Other commonly used operatorsThey include:
Example:
Example:
Example:
Example: 5/6. Not Equal/Identical To: To rehash the earlier example on equality, the sample below will display 'a and b are not identical', but not 'a and b are not equal'. |
||||||||||||||||||||||||||||||||||||||||||||||||
| instanceof (type operator) For checking whether some object is of a certain class, the (binary) The first (left) parameter is the object to test. If this variable is not an object, The second (right) parameter is the class to compare with. The class can be provided as the class name itself, a string variable containing the class name (not a string constant!) or an object of that class.
To check whether an object is not of some class, the not operator ( Note that parentheses around CaveatsIf a class does not exist, the registered autoload functions are called to try to define the class (this is a topic outside the scope of this part of the Documentation!). In PHP versions before 5.1.0, the As of PHP version 5.1.0, the registered autoloaders are not called anymore in these situations. Older versions of PHP (before 5.0)In older versions of PHP (before 5.0), the |
||||||||||||||||||||||||||||||||||||||||||||||||
Bitwise Operators Prefix bitwise operatorsBitwise operators are like logical operators but executed per bit rather than per boolean value. Bitmask-bitmask operatorsBitwise AND Bitwise OR Bitwise XOR Example uses of bitmasksThese operators can be used to manipulate bitmasks. For example: Here, the This example (assuming
Bear in mind that these comparison operators: ( Bit-shifting operatorsBitwise left shift
Bitwise right shift
Example uses of bit shifting:Fast division by 16 (better performance than On 32-bit systems, this discards all bits in the integer, setting the value to 0. On 64-bit systems, this unsets the most significant 32 bits and keep the least significant 32 bits, equivalent to Note: In this example, RemarksOperators 'operate' or act on one (unary operators such as Operator precedence influences how operators are grouped (as if there were parentheses). The following is a list of operators in order of there precendence (operators in the second column). If multiple operators are in one row, the grouping is determined by the code order, where the first column indicates the associativity (see examples).
Full information is at Stack Overflow (opens new window). Note that functions and language constructs (e.g. |
Read Article
https://develop.horje.com/learn/1434/reference