Lesson 4: Operators in PHP

Introduction

Operators in PHP are symbols or keywords used to perform specific operations on variables and values. PHP offers a wide range of operators grouped into categories such as arithmetic, assignment, comparison, and logical operators. This lesson explores each type with examples and use cases.


4.1 Arithmetic Operators

Purpose

Arithmetic operators are used to perform mathematical calculations.

List of Arithmetic Operators

Operator Description Example Result
+ Addition $a + $b Sum of $a and $b
- Subtraction $a - $b Difference of $a and $b
* Multiplication $a * $b Product of $a and $b
/ Division $a / $b Quotient of $a divided by $b
% Modulus (Remainder) $a % $b Remainder of $a divided by $b
** Exponentiation $a ** $b $a raised to the power of $b

Examples

$a = 10;
$b = 3;

echo "Addition: " . ($a + $b) . "
"; // Outputs: 13
echo "Subtraction: " . ($a - $b) . "
"; // Outputs: 7
echo "Multiplication: " . ($a * $b) . "
"; // Outputs: 30
echo "Division: " . ($a / $b) . "
"; // Outputs: 3.33333
echo "Modulus: " . ($a % $b) . "
"; // Outputs: 1
echo "Exponentiation: " . ($a ** $b); // Outputs: 1000


4.2 Assignment Operators

Purpose

Assignment operators assign values to variables, often performing operations as they assign.

List of Assignment Operators

Operator Description Example Equivalent to
= Assign $a = $b $a = $b
+= Add and assign $a += $b $a = $a + $b
-= Subtract and assign $a -= $b $a = $a - $b
*= Multiply and assign $a *= $b $a = $a * $b
/= Divide and assign $a /= $b $a = $a / $b
%= Modulus and assign $a %= $b $a = $a % $b

Examples

$a = 10;
$b = 5;

$a += $b; // $a = $a + $b
echo "Addition Assignment: $a
"; // Outputs: 15

$a -= $b; // $a = $a - $b
echo "Subtraction Assignment: $a
"; // Outputs: 10

$a *= $b; // $a = $a * $b
echo "Multiplication Assignment: $a
"; // Outputs: 50

$a /= $b; // $a = $a / $b
echo "Division Assignment: $a
"; // Outputs: 10

$a %= $b; // $a = $a % $b
echo "Modulus Assignment: $a
"; // Outputs: 0


4.3 Comparison Operators

Purpose

Comparison operators are used to compare two values and return a boolean (true or false) based on the result.

List of Comparison Operators

Operator Description Example Result
== Equal $a == $b true if $a equals $b
=== Identical $a === $b true if $a equals $b and is the same type
!= Not equal $a != $b true if $a is not equal to $b
<> Not equal $a <> $b true if $a is not equal to $b
!== Not identical $a !== $b true if $a is not equal to $b or not the same type
< Less than $a < $b true if $a is less than $b
> Greater than $a > $b true if $a is greater than $b
<= Less than or equal to $a <= $b true if $a is less than or equal to $b
>= Greater than or equal to $a >= $b true if $a is greater than or equal to $b
<=> Spaceship (Three-way comparison) $a <=> $b -1, 0, or 1 depending on comparison

Examples

$a = 5;
$b = 10;

echo "Equal: " . var_export($a == $b, true) . "
"; // Outputs: false
echo "Not Equal: " . var_export($a != $b, true) . "
"; // Outputs: true
echo "Identical: " . var_export($a === $b, true) . "
"; // Outputs: false
echo "Spaceship: " . ($a <=> $b) . "
"; // Outputs: -1 (since $a < $b)


4.4 Logical Operators

Purpose

Logical operators are used to combine conditional statements.

List of Logical Operators

Operator Description Example Result
&& Logical AND $a && $b true if both $a and $b are true
` ` Logical OR
! Logical NOT !$a true if $a is false
and Logical AND (lower precedence) $a and $b Same as &&
or Logical OR (lower precedence) $a or $b Same as `
xor Logical XOR $a xor $b true if $a or $b is true but not both

Examples

$a = true;
$b = false;

echo "Logical AND: " . var_export($a && $b, true) . "
"; // Outputs: false
echo "Logical OR: " . var_export($a || $b, true) . "
"; // Outputs: true
echo "Logical NOT: " . var_export(!$a, true) . "
"; // Outputs: false
echo "Logical XOR: " . var_export($a xor $b, true) . "
"; // Outputs: true


Hands-On Exercises

  1. Arithmetic Operations: Write a PHP script that calculates and outputs the area and perimeter of a rectangle with given length and width.
  2. Assignment Operators: Create a script to track a savings account balance where deposits and withdrawals are performed using assignment operators.
  3. Comparison and Logical Operators:
    • Write a script to check if a number is positive, negative, or zero using comparison operators.
    • Use logical operators to determine if a student passes based on their scores in two subjects (passing condition: both scores must be above 40).

Assignment

  • Write a PHP script to:
    1. Take two numbers as input.
    2. Perform all arithmetic operations on them.
    3. Compare the numbers using comparison operators.
    4. Determine if both numbers are even using logical operators.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *