Learn web design and programming with our free video and text tutorials.
Web Designer? Design and market your own professional website with easy-to-use tools.
In PHP, operators are used to manipulate or perform operations on values and expressions. Operators allow you to assign values to variables, perform arithmetic operations, concatenate strings, compare values and perform Boolean operations.
The assignment operator (= or equals), is used to assign a value to a variable or to assign a variable to another variable's value. The assignment operator can also be used in combination with other mathematical operators to change the value of a variable.
The = (assignment) operator sets the value of the first operand to the value of the second operand.
<?php
$x = 1;
echo $x;
?>
The code above outputs:
1
The += (addition-assigment) operator sets the value of the first operand to its value plus the second value.
<?php
$x = 1;
$x += 2;
echo $x;
?>
The code outputs:
3
The -= (subtraction-assignment) operator sets the value of the first operand to its value minus the secode value.
<?php
$x = 3;
$x -= 2;
echo $x;
?>
The code above outputs:
1
The *= (multiplication-assignment) operator sets the value of the first operand to its value multiplied by the second value.
<?php
$x = 2;
$x *= 2;
echo $x;
?>
The code above outputs:
4
The /= (division-assignment) operator sets the value of the first operancd to its value divided by the second value.
<?php
$x = 4;
$x /= 2;
echo $x;
?>
The code above outputs:
2
The .= (concatenation-assignment) operator sets the value of the first operand to a string containing its value with the second value appended at the end.
<?php
$x = "WebDevelopmentTutorials.com ";
$x .= "is the best.";
echo $x;
?>
The code above outputs:
WebDevelopmentTutorials.com is the best.
The %= (modulo-assignment) operator sets the value of the first operand to the remainder of its value divided by the second value.
<?php
$x = 3;
$x %= 2;
echo $x;
?>
The code above outputs:
1
PHP arithmetic operators are used to perform mathematical operations.
The +(addition) operator calculates the sum of 2 values.
<?php
echo 1 + 1;
?>
The code above outputs:
2
The -(subtraction) operator calculates the difference between 2 values.
<?php
echo 2 - 1;
?>
The code above outputs:
1
The PHP *(multiplication) operator multiplies 2 values.
<?php
echo 2 * 2;
?>
The code above outputs:
4
The PHP /(division) operator divides the first value by the second value.
<?php
echo 4 / 2;
?>
The code above outputs:
2
The PHP %(modulus) operator calculates the remainder of the division of the first value by the second.
<?php
echo 3 % 2;
?>
The code above outputs:
1
The PHP concatenation operator (.) is used to combine 2 sting values to create one string. The concatenation operator us usefull when you are combining strings with values from constants and arrays.
<?php
$variable1="Hello";
$variable2="World";
echo $variable1 . " " . $variable2;
?>
The code above outputs:
Hello World
Notice that the blank space between the . (concatenation) operator and the double quote (") is kept intact.
PHP increment and decrement operators are used to increment and decrement numeric values.
The PHP ++$value(pre-increment) operator adds 1 to a value before the value is used in the expression in which it is contained.
<?php
$value = 1;
echo ++$value;
?>
The code above outputs:
2
The PHP $value++(post-increment) operator adds 1 to a value after the value is used in the expression in which it is contained.
<?php
$value = 1;
echo $value++;
echo "<br />";
echo $value;
?>
The code above outputs:
1
2
The PHP --$value(pre-decrement) operator subtracts 1 from a value before the value is used in the expression in which it is contained.
<?php
$value = 2;
echo --$value;
?>
The code above outputs:
1
The PHP $value--(post-drecrement) operator subtracts 1 from a value after the value is used in the expression in which it is contained.
<?php
$value = 2;
echo $value--;
echo "<br />;
echo $value;
?>
The code above outputs:
2
1
PHP logical operators are used to perform Boolean operations to compare one value to another to determine if a statement is true or false.
The PHP and operator checks if all values are true.
<?php
$a = 1;
$b = 2;
if (($a == 1) and ($b == 2)) echo "True";
?>
The code above outputs:
True
The PHP && operator checks if all values are true. This operator does the same thing as the and operator.
<?php
$a = 1;
$b = 2;
if (($a == 1) && ($b == 2)) echo "True";
?>
The PHP or operator checks if any values are true.
<?php
$a = 1;
$b = 2;
if (($a == 1) or ($b == 100)) echo "True";
?>
The code above outputs:
True
The PHP || operator checks if any values are true. This operator does the same thing as the and operator.
<?php
$a = 1;
$b = 2;
if (($a == 1) || ($b == 100)) echo "True";
?>
The code above outputs:
True
The PHP xor operator checks if only one value is true.
<?php
$a = 1;
$b = 2;
if (($a == 1) xor ($b == 100)) echo "True";
?>
The code above outputs:
True
The PHP ! operator checks if a statement is true
<?php
$a = 1;
if (!($a == 1)) echo "True";
?>
The code above outputs:
True
PHP comparison operators compare values and return Boolean true or false values depending on the result.
The == (equl to) operator checks if the first operand is equal to the second operand.
<?php
$a == 1;
$b == 1;
if ($a == $b) echo "$a is equal to $b.";
?>
The code above outputs:
<?php
1 is equal to 1.
?>
The PHP === (identical to) operator checks if the first operand has the same value and type as the second operand.
<?php
$a = 1;
$b = 1;
if ($a === $b) echo "1 is identical to 1.";
?>
The code above outputs:
<?php
1 is identical to 1.
?>
The PHP != (equal to) operator checks if the first operand is not equal to the second operand.
<?php
$a = 1;
$b = 2;
if ($a != $b) echo "$a is not equal to $b.";
?>
The code above outputs:
1 is not equal to 2.
The PHP !== (not identical to) operator checks if the first operand is not equal to the second operand.
<?php
$a = 1;
$b = 2;
if ($a !== $b) echo "$a is not identical to $b.";
?>
The code above outputs:
1 is not equal to 2.
The PHP < (less than) operator checks if the first operand is less than the second operand.
<?php
$a = 1;
$b = 2;
if ($a < $b) echo "$a is less than $b.";
?>
The code above outputs:
1 is less than 2.
The PHP > (greater than) operator checks if the first operand is greater than the second operator.
<?php
$a = 2;
$b = 1;
if ($a > $b) echo "$a is greater than $b.";
?>
The code above outputs:
2 is greater than 1.
The PHP <= (less than or equal to) operator checks if the first operand is less than or equal to the second operand.
<?php
$a = 1;
$b = 20;
if ($a <= $b) echo "$a is less than or equal to $b.";
?>
The code above outputs:
1 is equal to or less than 20.
The PHP >= (greater or equal to) operator checks if the first operand is greater than or equal to the second operand.
<?php
$a = 5;
$b = 1;
if ($a >= $b) echo "$a is greater than or equal to $b.";
?>
The code above outputs:
5 is greater than or equal to 1.