In PHP, operators are used to perform operations on variables and values. PHP has a variety of operators, including arithmetic operators, assignment operators, comparison operators, and logical operators.
Here are some examples of common operators in PHP:
- Arithmetic operators:
- + (addition)
- – (subtraction)
- * (multiplication)
- / (division)
- % (modulus)
- Assignment operators:
- = (assign a value to a variable)
- += (add a value to a variable and assign the result to the variable)
- -= (subtract a value from a variable and assign the result to the variable)
- *= (multiply a variable by a value and assign the result to the variable)
- /= (divide a variable by a value and assign the result to the variable)
- Comparison operators:
- == (equal to)
- != (not equal to)
- (greater than)
- < (less than)
- = (greater than or equal to)
- <= (less than or equal to)
- Logical operators:
- && (and)
- || (or)
- ! (not)
Here are some examples of how you can use these operators:
$x = 10;
$y = 5;
echo $x + $y; // Outputs 15
echo $x - $y; // Outputs 5
echo $x * $y; // Outputs 50
echo $x / $y; // Outputs 2
echo $x % $y; // Outputs 0
$x = 1;
$x += 10;
echo $x; // Outputs 11
$x = 'Hello';
$x .= ' World';
echo $x; // Outputs 'Hello World'
$x = 10;
$y = 5;
if ($x > $y) {
echo '$x is greater than $y';
}
if ($x == '10') {
echo '$x is equal to "10"';
}
if ($x !== '10') {
echo '$x is not equal to "10"';
}
if ($x > 0 && $y < 10) {
echo '$x is greater than 0 and $y is less than 10';
}
if ($x > 0 && $y < 10) {
echo '$x is greater than 0 and $y is less than 10';
}
if ($x > 0 || $y < 10) {
echo '$x is greater than 0 or $y is less than 10';
}
if (!($x > 0)) {
echo '$x is not greater than 0';
}