PHP operators are symbols used to perform operations on variables and values. They are essential for manipulating data and controlling program flow in PHP scripts.
Arithmetic operators perform mathematical calculations on numeric values.
$a = 10;
$b = 3;
echo $a + $b; // Output: 13
echo $a % $b; // Output: 1
Assignment operators are used to assign values to variables. The basic assignment operator is =, but PHP also provides combined operators like +=, -=, *=, etc.
$x = 5;
$x += 3; // Equivalent to $x = $x + 3
echo $x; // Output: 8
Comparison operators compare two values and return a boolean result.
Logical operators combine conditional statements.
$a = true;
$b = false;
var_dump($a && $b); // Output: bool(false)
var_dump($a || $b); // Output: bool(true)
These operators increase or decrease a variable's value by one.
The concatenation operator (.) is used to combine two string values.
$str1 = "Hello";
$str2 = "World";
echo $str1 . " " . $str2; // Output: Hello World
PHP follows a specific order when evaluating expressions with multiple operators. Parentheses can be used to explicitly define the order of operations.
Mastering PHP operators is crucial for effective programming. They form the foundation for PHP Conditional Statements and are extensively used in PHP Loops. For more advanced usage, explore PHP Functions and PHP Arrays.