PHP as Operator 🎯

beginner
23 min

PHP as Operator 🎯

Welcome to our comprehensive guide on PHP Operators! This tutorial is designed for both beginners and intermediates. Let's dive into the world of PHP and understand how operators work.

What are Operators? πŸ“

In PHP, operators are symbols that perform specific mathematical, comparison, and logical operations on variables or values. They help you manipulate data and make decisions in your code.

Basic Operators πŸ’‘

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, division, and modulus.

php
// Example 1: Addition $a = 5; $b = 3; $sum = $a + $b; echo $sum; // Output: 8 // Example 2: Subtraction $c = 10; $d = 3; $difference = $c - $d; echo $difference; // Output: 7

Comparison Operators

Comparison operators are used to compare two values and return a boolean (true or false) value.

php
// Example: Equality (==) $e = 5; $f = 5; $isEqual = ($e == $f); echo $isEqual; // Output: 1 (true) // Example: Inequality (!=) $g = 5; $h = 6; $isNotEqual = ($g != $h); echo $isNotEqual; // Output: 1 (true)

Assignment Operator

The assignment operator (=) assigns a value to a variable.

php
// Example: Assignment $i = 10; echo $i; // Output: 10

Quiz πŸ’‘

Quick Quiz
Question 1 of 1

What does the equality operator (==) do in PHP?

Advanced Operators πŸ’‘

Increment (++) and Decrement (--) Operators

Increment and decrement operators are used to increase or decrease the value of a variable by 1.

php
// Example: Increment $j = 5; $j++; echo $j; // Output: 6 // Example: Decrement $k = 10; $k--; echo $k; // Output: 9

Modulus Operator (% )

The modulus operator (%) returns the remainder of a division operation.

php
// Example: Modulus $l = 17; $m = 5; $remainder = $l % $m; echo $remainder; // Output: 2

Logical Operators

Logical operators are used to combine conditional statements in PHP.

php
// Example: AND (&&) $n = 5; $o = 10; if ($n < 10 && $o > 10) { echo "Both conditions are true."; } // Example: OR (||) $p = 5; $q = 5; if ($p < 10 || $q > 10) { echo "At least one condition is true."; }

Quiz πŸ’‘

Quick Quiz
Question 1 of 1

What does the modulus operator (%) do in PHP?


Keep exploring and practicing with PHP operators to strengthen your programming skills! Happy coding! πŸš€