In this article Operators in PHP we give the information about Operators in PHP are used to perform various types of operations on values.
Operators in PHP:
Arithmetic Operators
Mathematical operators are used to perform calculations.
Operator description example output
+ Addition $a + $b $a + $b
– Subtraction $a – $b $a – $b
* Multiplication $a * $b $a * $b
/ Division $a / $b $a / $b
% Modulo (Remainder) $a % $b $a % $b
** Exponentiation $a ** $b (PHP 5.6+) $a^$b
Example:
<?php
$a = 10;
$b = 3;
echo “Add: ” . ($a + $b). “<br>”;
echo “Subtraction: ” . ($a – $b). “<br>”;
echo “Multiplication: ” . ($a*$b). “<br>”;
echo “Part: ” . ($a / $b). “<br>”;
echo “Remainder: ” . ($a % $b). “<br>”;
echo “Exponent: ” . ($a ** $b);
?>
Assignment Operators
Assignment operators are used to assign values to variables.
operator description example output
= assign $a = $b $a = $b
+= assign by adding $a += $b $a = $a + $b
-= subtract assign $a -= $b $a = $a – $b
*= Multiply by assigning $a *= $b $a = $a * $b
/= divide by assigning $a /= $b $a = $a / $b
Assign with %= modulo $a %= $b $a = $a % $b
Example:
<?php
$x = 5;
$y = 10;
$x += $y; // $x = $x + $y
echo “New $x: $x”;
?>
Comparison Operators
These operators compare two values.
operator description example output
== Equal $a == $b true or false
=== Identical $a === $b true or false
!= Not Equal $a != $b true or false
<> Not Equal $a <> $b true or false
!== Not Identical $a !== $b true or false
< Less than $a < $b true or false
> Greater than $a > $b true or false
<= Less than or Equal to $a <= $b true or false
>= Greater than or Equal to $a >= $b true or false
<=> Spaceship Operator (Three-way Comparison) $a <=> $b -1, 0, or 1
Example:
<?php
$a = 5;
$b = 10;
echo ($a == $b) ? “Is equal.” : “Not equal to.”;
?>
Logical Operators
Logical operators are used to connect conditions.
operator description example output
&& AND $a && $b true or false
` ` OR
!NOT !$a true or false
and AND (low priority) $a and $b true or false
or OR (low priority) $a or $b true or false
xor
Example:
<?php
$x = true;
$y = false;
if ($x && !$y) {
echo “This is correct.”;
} else {
echo “This is wrong.”;
}
?>
Mixture of Operators
You can use different types of operators together.
Example:
<?php
$a = 10;
$b = 20;
$result = ($a < $b) && ($b > 15);
echo $result ? “The conditional is correct.” : “The conditional is false.”;
?>
Conclusion
Arithmetic, Assignment, Comparison, and Logical operators in PHP make a variety of tasks simple and effective. Using the correct operators makes code more efficient and readable.
String and Array Operators in PHP
String Operators and Array Operators in PHP are used to work with strings and arrays. These operators simplify tasks like concatenating and comparing strings, and adding and comparing elements to arrays.
String Operators
String operators are used to manipulate strings.
operator description example output
. String concatenation $str1 . $str2 Combination of $str1 and $str2
.= Concatenation Assignment $str1 .= $str2 $str1 = $str1 . $str2
Example 1: Concatenating String
<?php
$str1 = “Hello”;
$str2 = ” World!”;
$result = $str1 . $str2;
echo $result; //Output: Hello world!
?>
Example 2: String assignment and concatenation
<?php
$str1 = “PHP”;
$str1 .= ” is awesome!”;
echo $str1; // Output: PHP is awesome!
?>
Array Operators
Array operators are used to perform various operations with arrays.
operator description example output
+ Merges arrays (Union) $array1 + $array2 Merges both arrays.
== Equality of key and value of two arrays $array1 == $array2 true or false
=== Equality of keys, values and types of two arrays $array1 === $array2 true or false
!= two arrays not equal $array1 != $array2 true or false
<> two arrays not equal $array1 <> $array2 true or false
!== Two arrays differ in type and value $array1 !== $array2 true or false
Example 1: Merging Arrays (+)
<?php
$array1 = [“a” => “Apple”, “b” => “Banana”];
$array2 = [“c” => “Cherry”, “d” => “Date”];
$result = $array1 + $array2;
print_r($result);
// आउटपुट: Array ( [a] => Apple [b] => Banana [c] => Cherry [d] => Date )
?>
Example 2: Comparing Arrays (==)
<?php
$array1 = [“x” => 1, “y” => 2];
$array2 = [“x” => 1, “y” => 2];
echo ($array1 == $array2) ? “Arrays are the same.” : “Arrays are not equal.”;
//Output: Arrays are identical.
?>
Example 3: Equality and types of arrays (===)
<?php
$array1 = [“x” => 1, “y” => “2”];
$array2 = [“x” => 1, “y” => 2];
echo ($array1 === $array2) ? “Have the same type and value.” : “Do not have the same type and value.”;
// Output: Type and value are not the same.
?>
Example 4: Overwrite when merging arrays
<?php
$array1 = [“a” => “Apple”, “b” => “Banana”];
$array2 = [“b” => “Blueberry”, “c” => “Cherry”];
$result = $array1 + $array2;
print_r($result);
// Output: Array ( [a] => Apple [b] => Banana [c] => Cherry )
?>
Note: In $array1 + $array2, if both have the same key, then the value of the first array is kept.
Summary
String Operators are used to concatenate and assign strings.
Array Operators are used to merge, compare, and control overwriting of arrays.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining