Expressions In PHP, an expression is a combination of variables, constants, operators, and function calls that evaluates to a value. Expressions can be simple or complex and are fundamental in performing computations, making decisions, and manipulating data.
Assignment Expressions Assignment expressions assign values to variables. You can also use shorthand operators. Example : $ a = 5; // Simple assignment $a += 3; // Equivalent to $a = $a + 3; (Addition assignment) $a *= 2; // Equivalent to $a = $a * 2; (Multiplication assignment
Increment and Decrement Expressions These expressions increase or decrease a variable's value by one. Example : $ count = 10; $count++; // Increment: $count becomes 11 $count--; // Decrement: $count becomes 10 again
Array Expressions PHP allows the creation and manipulation of arrays. Example : $ fruits = array("apple", "banana", "cherry"); $ firstFruit = $fruits[0]; // "apple"
Ternary Operator The ternary operator is a shorthand for an if-else statement. Example : $ age = 18; $message = ($age >= 18) ? "Adult" : "Minor"; // "Adult"
Function Call Expressions Calling a function is an expression that evaluates to the return value of that function. Example : function add($x, $y) { return $x + $y; } $result = add(5, 10); // $result is 15