-
Hajipur, Bihar, 844101
Operators in PHP are symbols or constructs that perform operations on variables and values. They are essential for mathematical calculations, logical comparisons, assignments, and string manipulations. Understanding PHP operators is critical for writing efficient and accurate code.
PHP operators are divided into several categories:
Arithmetic Operators – Perform mathematical operations.
Assignment Operators – Assign values to variables.
Comparison Operators – Compare two values.
Increment/Decrement Operators – Increase or decrease a value by 1.
Logical Operators – Combine conditional statements.
String Operators – Manipulate strings.
Array Operators – Compare arrays.
Ternary Operator – Short-hand for conditional statements.
Arithmetic operators perform basic calculations:
<?php
$a = 10;
$b = 3;
echo $a + $b . "<br>"; // Addition: 13
echo $a - $b . "<br>"; // Subtraction: 7
echo $a * $b . "<br>"; // Multiplication: 30
echo $a / $b . "<br>"; // Division: 3.3333
echo $a % $b . "<br>"; // Modulus: 1
echo $a ** $b . "<br>"; // Exponent: 1000
?>
+
, -
, *
, /
, %
, **
are commonly used in calculations.
Modulus %
returns the remainder, and exponent **
calculates powers.
Assignment operators combine assignment with arithmetic operations:
<?php
$num = 10;
$num += 5; // $num = $num + 5
echo $num . "<br>"; // 15
$num *= 2; // $num = $num * 2
echo $num . "<br>"; // 30
?>
Other operators: -=
, /=
, %=
, **=
.
Useful for updating values efficiently.
Comparison operators evaluate relationships between values:
<?php
$x = 5;
$y = 10;
var_dump($x == $y); // Equal: false
var_dump($x != $y); // Not equal: true
var_dump($x > $y); // Greater than: false
var_dump($x < $y); // Less than: true
var_dump($x >= $y); // Greater or equal: false
var_dump($x <= $y); // Less or equal: true
var_dump($x === $y); // Identical (value and type): false
var_dump($x !== $y); // Not identical: true
?>
==
checks value equality.
===
checks value and type equality.
Comparison operators are crucial in conditional statements and loops.
Increment and decrement operators increase or decrease a value by 1:
<?php
$num = 10;
echo $num++ . "<br>"; // Post-increment: prints 10, then increments
echo ++$num . "<br>"; // Pre-increment: increments first, prints 12
echo $num-- . "<br>"; // Post-decrement: prints 12, then decrements
echo --$num . "<br>"; // Pre-decrement: decrements first, prints 10
?>
Pre-increment/decrement modifies the value before evaluation.
Post-increment/decrement modifies the value after evaluation.
Useful in loops and counters.
Logical operators are used to combine conditional statements:
<?php
$a = true;
$b = false;
var_dump($a && $b); // AND: false
var_dump($a || $b); // OR: true
var_dump(!$a); // NOT: false
var_dump($a xor $b); // XOR: true
?>
&&
or AND
– True if both are true.
||
or OR
– True if at least one is true.
!
– Negates the boolean value.
xor
– True if only one is true.
PHP uses the concatenation operator .
to join strings:
<?php
$first = "Hello, ";
$second = "Vicky!";
echo $first . $second; // Outputs: Hello, Vicky!
?>
.=
appends a string to an existing variable:
$text = "Hello";
$text .= " World!";
echo $text; // Outputs: Hello World!
Useful for building dynamic messages or HTML content.
Array operators compare arrays:
<?php
$array1 = ["a" => "Apple", "b" => "Banana"];
$array2 = ["b" => "Banana", "a" => "Apple"];
var_dump($array1 == $array2); // True: Same key-value pairs
var_dump($array1 === $array2); // False: Different order
var_dump($array1 != $array2); // False
var_dump($array1 <> $array2); // False
?>
+
operator merges arrays, preserving keys from the first array.
Array operators are important when comparing configuration or dataset arrays.
The ternary operator is a short-hand conditional expression:
<?php
$age = 20;
$status = ($age >= 18) ? "Adult" : "Minor";
echo $status; // Outputs: Adult
?>
Syntax: (condition) ? value_if_true : value_if_false
.
Reduces multiple lines of if-else
statements.
Confusing =
(assignment) with ==
(comparison).
Using &&
vs AND
– &&
has higher precedence.
Forgetting to use .
for string concatenation.
Using ===
to compare different types – ensures type safety.
Always check array key existence before using array operators.
PHP operators are essential for mathematical, logical, comparison, string, and array operations.
Categories: arithmetic, assignment, comparison, increment/decrement, logical, string, array, ternary.
Understanding operator precedence ensures accurate calculations.
Use string concatenation for building messages and HTML content.
Array operators help compare or merge arrays effectively.
Ternary operators provide short, readable conditional expressions.
Mastering operators is crucial for program logic, decision making, and data manipulation.
Operators are the building blocks of PHP logic, and using them effectively ensures clean, maintainable, and efficient code.
Create a PHP script that adds two numbers $a = 12
and $b = 8
and prints the result. Include a comment explaining addition.
Write a PHP script that subtracts $b = 5
from $a = 20
and prints the result. Include a comment explaining subtraction.
Create a PHP script that multiplies $x = 7
and $y = 6
and prints the result. Include a comment explaining multiplication.
Write a PHP script that divides $num1 = 15
by $num2 = 4
and prints the result. Include a comment and check for division by zero.
Create a PHP script that calculates the remainder of $num1 = 17
divided by $num2 = 3
using the modulus operator %
and prints it. Include a comment.
Write a PHP script that uses the increment operator to increase $count = 10
by 1 and prints the value before and after using both pre-increment and post-increment. Include comments explaining the difference.
Create a PHP script that uses logical operators to evaluate two boolean variables $a = true
and $b = false
with AND, OR, NOT, and XOR. Include comments explaining each result.
Write a PHP script that concatenates two strings $first = "Hello, "
and $second = "World!"
using the .
operator and prints the result. Include a comment.
Create a PHP script that compares two arrays $array1 = ["x" => 1, "y" => 2]
and $array2 = ["y" => 2, "x" => 1]
using ==
and ===
, and prints the results. Include comments explaining the difference.
Write a PHP script that uses the ternary operator to check if $age = 18
is greater than or equal to 18 and prints "Adult"
or "Minor"
. Include a comment explaining the ternary operation.