Operators in PHP
Operators in PHP
In PHP, operators are special symbols or keywords used to perform various operations on data. PHP supports a wide range of operators, including arithmetic operators, comparison operators, logical operators, assignment operators, and more. Here's an overview of some common operators in PHP:
1. Arithmetic Operators:
- `+` (Addition): Adds two values.
- `-` (Subtraction): Subtracts the second value from the first.
- `*` (Multiplication): Multiplies two values.
- `/` (Division): Divides the first value by the second.
- `%` (Modulus): Returns the remainder of the division.
- `**` (Exponentiation): Raises the first value to the power of the second (PHP 5.6+).
2. Comparison Operators:
- `==` (Equal): Checks if two values are equal.
- `!=` or `<>` (Not Equal): Checks if two values are not equal.
- `===` (Identical): Checks if two values are equal and of the same data type.
- `!==` (Not Identical): Checks if two values are not equal or not of the same data type.
- `<` (Less Than): Checks if the first value is less than the second.
- `>` (Greater Than): Checks if the first value is greater than the second.
- `<=` (Less Than or Equal To): Checks if the first value is less than or equal to the second.
- `>=` (Greater Than or Equal To): Checks if the first value is greater than or equal to the second.
3. Logical Operators:
- `&&` or `and` (Logical AND): Returns true if both conditions are true.
- `||` or `or` (Logical OR): Returns true if at least one condition is true.
- `!` or `not` (Logical NOT): Inverts the result of a condition.
4. Assignment Operators:
- `=` (Assignment): Assigns a value to a variable.
- `+=` (Add and Assign): Adds a value to the variable's current value and assigns the result.
- `-=` (Subtract and Assign): Subtracts a value from the variable's current value and assigns the result.
- `*=` (Multiply and Assign): Multiplies the variable's current value by a value and assigns the result.
- `/=` (Divide and Assign): Divides the variable's current value by a value and assigns the result.
- `%=` (Modulus and Assign): Calculates the modulus of the variable's current value and a value, then assigns the result.
5. Increment and Decrement Operators:
- `++` (Increment): Increases the value of a variable by 1.
- `--` (Decrement): Decreases the value of a variable by 1.
6. Concatenation Operator:
- `.` (Concatenation): Combines two strings.
7. Ternary Operator:
- `condition ? expr1 : expr2`: A shorthand way to write conditional statements. If the condition is true, `expr1` is returned; otherwise, `expr2` is returned.
8. Null Coalescing Operator (PHP 7.0+):
- `??`: Returns the first non-null value in a list of expressions.
9. Type Operators:
- `instanceof`: Checks if an object is an instance of a specific class.
10. Bitwise Operators:
- `&` (Bitwise AND): Performs a bitwise AND operation.
- `|` (Bitwise OR): Performs a bitwise OR operation.
- `^` (Bitwise XOR): Performs a bitwise XOR (exclusive OR) operation.
- `~` (Bitwise NOT): Inverts the bits of a value.
- `<<` (Left Shift): Shifts the bits to the left.
- `>>` (Right Shift): Shifts the bits to the right.
These are some of the most commonly used operators in PHP. Operators are crucial for performing various operations, making decisions, and manipulating data in your PHP scripts.
Comments
Post a Comment