3. PHP Operators
3. PHP Operators
Outline
− Arithmetic operators
− Assignment operators
− Comparison operators
− Logical operators
PHP Operators
In PHP, operators are used to perform operations on variables and values. There are several types
of operators, each with specific purposes and use cases. This section covers the most commonly
used operators: arithmetic, assignment, comparison, and logical.
1. Arithmetic Operators
Example
$x = 10;
$y = 3;
echo $x + $y; // 13
echo $x - $y; // 7
echo $x * $y; // 30
echo $x / $y; // 3.33
echo $x % $y; // 1
echo $x ** $y; // 1000 (10^3)
2. Assignment Operators
Example:
Example:
$x = 10;
$x += 5; // Equivalent to $x = $x + 5
echo $x; // Output: 15
Example:
$x = 10;
$x -= 3; // Equivalent to $x = $x - 3
echo $x; // Output: 7
Example:
$x = 10;
$x *= 2; // Equivalent to $x = $x * 2
echo $x; // Output: 20
Example:
$x = 10;
$x /= 2; // Equivalent to $x = $x / 2
echo $x; // Output: 5
Calculates the remainder of the current value of a variable divided by a specified value.
Example:
$x = 10;
$x %= 3; // Equivalent to $x = $x % 3
echo $x; // Output: 1
Example:
$text = "Hello";
$text .= " World!"; // Equivalent to $text = $text . "
World!"
echo $text; // Output: Hello World!
Assignment operators make it easy to perform updates on variables with minimal code, simplifying
processes that involve cumulative data, mathematical updates, and string concatenation.
3. Comparison Operators
Comparison operators are used to compare two values, returning a Boolean value (true or
false) based on the outcome. These are often used in conditional statements.
Example
$x = 10;
$y = "10";
i. And (&&)
• The && operator returns true only if both conditions are true. If either one or both
are false, the result is false.
$age = 25;
$hasPermission = true;
ii. Or (||)
• The || operator returns true if at least one of the conditions is true. It only returns
false if both conditions are false.
$isStudent = false;
$isMember = true;
• The ! operator inverts the result of a condition, returning true if the condition is
false and false if the condition is true.
$isLoggedIn = false;
Logical operators are essential in making complex decisions within code, such as:
• User Authentication: Check if a user has both valid credentials (&&) and required
permissions to access specific sections.
• Form Validation: Ensure multiple form fields meet the required criteria (&&) or give
options if one of several fields can be accepted (||).
• E-commerce: Apply discounts or membership benefits to a customer if they meet certain
criteria, like being a returning customer or a premium member (||).
The conditional (or ternary) operator in PHP provides a shorthand way to write simple if-
else statements. It allows you to make quick decisions within a single line of code, which can
be useful for assigning values based on conditions.
Example
$age = 20;
In this example, if $age is 18 or greater, $isAdult will be set to "Yes". Otherwise, it will be
set to "No".
The ternary operator is useful for simplifying if-else statements when only one condition is
checked, such as:
1. Form Validation: Checking if form fields have values and assigning defaults if they are
empty.
2. User Role Display: Displaying different information based on user roles, e.g., admin vs.
standard user.
3. Discount Application: Applying discounts based on user status or purchase amount.
Sometimes, you may need to evaluate multiple conditions. You can nest ternary operators, but
this should be done sparingly as it can reduce readability.
$score = 85;
In this case:
The conditional operator is very helpful for straightforward assignments and decisions, and it can
keep your code concise and efficient when used appropriately.