Back to course

Operators: Arithmetic, Assignment, and Comparison

PHP: The Complete 0 to Hero Bootcamp

7. Operators: Arithmetic, Assignment, and Comparison

Operators are used to perform operations on values and variables.

1. Arithmetic Operators

Used for mathematical calculations:

OperatorNameExample
+Addition$a + $b
-Subtraction$a - $b
*Multiplication$a * $b
/Division$a / $b
%Modulus (Remainder)$a % $b
**Exponentiation$a ** $b (a raised to the power of b)

php

2. Assignment Operators

Used to assign values to variables. The basic assignment operator is = .

OperatorExampleSame as
=$x = 10Assigns 10 to x
+=$x += 5$x = $x + 5
*=$x *= 2$x = $x * 2
.=$str .= 'world'$str = $str . 'world' (String Concatenation)

3. Comparison Operators

Used to compare two values. They return a boolean (true or false).

OperatorNameDescription
==EqualChecks if values are equal (loose comparison)
===IdenticalChecks if values AND data types are equal (strict comparison)
!=Not equalChecks if values are not equal
<>Not equalAlternative to !=
!==Not identicalChecks if values OR data types are not equal
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

We will use comparison operators extensively when covering conditional statements.