PHP Operators

In PHP, operators are symbols or keywords used to perform operations on variables and values. PHP supports a wide range of operators that allow you to perform arithmetic, comparison, logical, assignment, and other operations. Here's an overview of the main types of operators in PHP:


1. Arithmetic Operators:

These operators are used to perform basic arithmetic operations.

php
$a = 10;
$b = 5;

$sum = $a + $b;
$difference = $a - $b;
$product = $a * $b;
$quotient = $a / $b;
$remainder = $a % $b; // Modulus (remainder)

2. Comparison Operators:

Comparison operators are used to compare values.

php
$x = 10;
$y = 5;

$isEqual = ($x == $y); // Equal
$isNotEqual = ($x != $y); // Not equal
$isGreaterThan = ($x > $y); // Greater than
$isLessThan = ($x < $y); // Less than
$isGreaterOrEqual = ($x >= $y); // Greater than or equal to
$isLessOrEqual = ($x <= $y); // Less than or equal to

3. Logical Operators:

Logical operators are used to combine or manipulate boolean values.

php
$a = true;
$b = false;

$andResult = ($a && $b); // Logical AND
$orResult = ($a || $b); // Logical OR
$notResult = !$a; // Logical NOT

4. Assignment Operators:

Assignment operators are used to assign values to variables.

php
$x = 10;
$y = 5;

$x += $y; // Equivalent to: $x = $x + $y;
$x -= $y; // Equivalent to: $x = $x - $y;
$x *= $y; // Equivalent to: $x = $x * $y;
$x /= $y; // Equivalent to: $x = $x / $y;

5. Increment and Decrement Operators:

These operators are used to increase or decrease a variable's value by one.

php
$count = 0;

$count++; // Increment by one
$count--; // Decrement by one

6. String Concatenation Operator:

The `.` operator is used to concatenate (join) strings.

php
$firstName = "John";
$lastName = "Doe";

$fullName = $firstName . " " . $lastName; // Concatenates strings

7. Ternary Operator:

The ternary operator (`? :`) is a shorthand way of writing conditional expressions.

php
$age = 18;
$isAdult = ($age >= 18) ? "Yes" : "No";

8. Null Coalescing Operator:

The null coalescing operator (`??`) returns the first non-null value in a list.

php
$name = $_GET['name'] ?? "Guest";

These are some of the most commonly used operators in PHP. They play a crucial role in performing calculations, making decisions, and manipulating data within your PHP scripts.



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext