PHP Operators


Arithmetic Operators:

Used to perform basic mathematical operations.

Operator

Description

Example

Result

+

Addition

5 + 2

7

-

Subtraction

5 - 2

3

*

Multiplication

5 * 2

10

/

Division

5 / 2

2.5

%

Modulus (Remainder)

5 % 2

1

**

Exponentiation

5 ** 2

25

++

Increment

let a = 1; a++

2

--

Decrement

let a = 1; a--

0

Example
<?php
$a = 5;
$b = 2;
echo "Addition: " . ($a + $b) . "<br>";
echo "Subtraction: " . ($a - $b) . "<br>";
echo "Multiplication: " . ($a * $b) . "<br>";
echo "Division: " . ($a / $b) . "<br>";
echo "Remainder: " . ($a % $b) . "<br>";
echo "Exponentiation: " . ($a ** $b) . "<br>";
echo "Increment: " . ($a++) . "<br>";
echo "Decrement: " . ($a--) . "<br>";
?>

Assignment Operators:

Used to assign values to variables.

Operator

Description

Example

Equivalent To

=

Assign

a = 5

-----

+=

Add and Assign

a += 2

a = a + 2

-=

Subtract and Assign

a -= 2

a = a - 2

*=

Multiply and Assign

a *= 2

a = a * 2

/=

Divide and Assign

a /= 2

a = a / 2

%=

Modulus and Assign

a %= 2

a = a % 2

**=

Exponentiate Assign

a **= 2

a = a ** 2

Example
<?php
$a = 5;
echo("Assign: ".$a."<br>");
$a += 2;
echo("Add and Assign: ".$a."<br>");
$a -= 2;
echo("Subtract and Assign: ".$a."<br>");
$a *= 2;
echo("Multiply and Assign: ".$a."<br>");
$a /= 2;
echo("Divide and Assign: ".$a."<br>");
$a %= 2;
echo("Modulus and Assign: ".$a."<br>");
$a **= 2;
echo("Exponentiate Assign: ".$a."<br>");
?>

Try it yourself


Comparison Operators:

Used to compare two values.

Operator

Description

Example

Result

==

Equal to

5 == "5"

true

===

Strict Equal (type & value)

5 === "5"

false

!=

Not Equal

5 != "5"

false

!==

Strict Not Equal

5 !== "5"

true

>

Greater Than

5 > 2

true

<

Less Than

5 < 2

false

>=

Greater or Equal

5 >= 5

true

<=

Less or Equal

5 <= 5

true

Example
<?php
echo("Equal to: ".(5 == "5")."<br>");
echo("Strict Equal (type & value): ".(5 === "5")."<br>");
echo("Not Equal: ".(5 != "5")."<br>");
echo("Strict Not Equal: ".(5 !== "5")."<br>");
echo("Greater Than: ".(5 > 2)."<br>");
echo("Less Than: ".(5 < 2)."<br>");
echo("Greater or Equal: ".(5 >= 5)."<br>");
echo("Less or Equal: ".(5 <= 5)."<br>");
?>


Try it yourself


Logical Operators:

Used to combine or invert Boolean values.

Operator

Description

Example

Result

&&

AND

true && false

false

||

OR

true || false

true

!

NOT

!true

false

Example
<?php
$a = true;
$b = false;
$c = true;
if ($a && !$b || $c) {
echo "Condition met"; // Output: Condition met
}
?>

Try it yourself


Bitwise Operators:

Operate on binary numbers.

Operator

Description

Example

Result

&

AND

5 & 1

1

|

OR

5 | 1

5

^

XOR

5 ^ 1

4

~

NOT

~5

-6

<<

Left Shift

5 << 1

10

>>

Right Shift

5 >> 1

2

Example
<?php
// AND operator
echo("AND: ". (5 & 1)."<br>");
// OR operator
echo("OR: ".(5 | 1)."<br>");
// XOR operator
echo("XOR: ".(5 ^ 1)."<br>");
// NOT operator
echo("NOT: ".(~5)."<br>");
// Left Shift operator
echo("Left Shift: ".(5 << 1)."<br>");
// Right Shift operator
echo("Right Shift: ".(5 >> 1)."<br>");
?>

Try it yourself


String Operators:

Used to concatenate strings.

Operator

Description

Example

Result

.

Concatenation

"Hello " . "World!"

Hello World!

.=

Concatenation assignment

$str = "Hello";

$str .= "World!"

Hello World!

Example
<?php
$str = "Hello";
$str .= " World!";
echo $str; // Hello World!
echo "Hello"."World!"; // Hello World!
?>

Try it yourself


Ternary Operator:

A shorthand for if-else.

Example
<?php
$age = 18;
$status = ($age >= 18) ? "Adult" : "Minor";
echo $status; // Adult
?>

Try it yourself


Nullish Coalescing Operator (??)

Returns the right-hand value if the left-hand value is null or undefined.

Example
<?php
$name = $_GET['name'] ?? "Guest";
echo $name; // If 'name' is not set, outputs "Guest"
?>

Try it yourself


Spaceship Operator (<=>)

Access nested object properties safely.


Compares two values and returns:

  • -1 if left is smaller
  • 0 if they are equal
  • 1 if left is greater
Example
<?php
echo 10 <=> 20; // -1
echo 10 <=> 10; // 0
echo 20 <=> 10; // 1
?>

Try it yourself


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.