PHP OPERATOR | BCA SEM 02 BKNMU JUNAGADH

PHP Operators

PHP Operators

Operators are symbols that enable you to use one or more values to produce a new value. A value that is operated on by an operator is referred to as an operand. An operator is a symbol or series of symbols that, when used in conjunction with values, performs an action and usually produces a new value. An operand is a value used in conjunction with an operator. There are usually two operands to one operator.

PHP language supports the following types of operators:

  • Arithmetic operators
  • Comparison operators
  • Logical Operators
  • Assignment Operators
  • Conditional (or ternary) operators
  • String operator

Arithmetic Operators

Operator Name Example Result
+Addition$a + $bSum of $a and $b
-Subtraction$a - $bDifference of $a and $b
*Multiplication$a * $bProduct of $a and $b
/Division$a / $bQuotient of $a divided by $b
%Modulus$a % $bRemainder of $a divided by $b
**Exponentiation$a ** $b$a raised to the power of $b
<?php
$a = 10;
$b = 3;

// Examples
$sum = $a + $b;
echo "Addition: $a + $b = $sum\n";
$difference = $a - $b;
echo "Subtraction: $a - $b = $difference\n";
$product = $a * $b;
echo "Multiplication: $a * $b = $product\n";
$quotient = $a / $b;
echo "Division: $a / $b = $quotient\n";
$remainder = $a % $b;
echo "Modulus: $a % $b = $remainder\n";
$power = $a ** $b;
echo "Exponentiation: $a ** $b = $power\n";
?>

Comparison Operators

Operator Name Example Result
==Equal$a == $btrue if $a is equal to $b
===Identical$a === $btrue if $a is equal to $b and of the same type
!=Not equal$a != $btrue if $a is not equal to $b
<>Not equal (alternative)$a <> $btrue if $a is not equal to $b
!==Not identical$a !== $btrue if $a is not equal to $b or not of the same type
<Less than$a < $btrue if $a is less than $b
>Greater than$a > $btrue if $a is greater than $b
<=Less than or equal to$a <= $btrue if $a is less than or equal to $b
>=Greater than or equal to$a >= $btrue if $a is greater than or equal to $b
<?php
$a = 10;
$b = 20;
$c = 10;

// Examples
echo ($a == $c) ? "true" : "false";
echo ($a < $b) ? "true" : "false";
?>

Logical Operators

Operator Name Example Result
&&And$a && $btrue if both $a and $b are true
||Or$a || $btrue if either $a or $b is true
!Not!$atrue if $a is not true

Assignment Operators

Operator Name Example Equivalent To
=Assignment$a = $b$a = $b
+=Add and assign$a += $b$a = $a + $b
<?php
$a = 10;
$a += 5;
echo $a;
?>

Conditional (Ternary) Operator

Shorthand for an if-else statement:

$result = (condition) ? value_if_true : value_if_false;
<?php
$age = 18;
$status = ($age >= 18) ? "Adult" : "Minor";
echo $status;
?>

String Operators

Operator Name Example Result
.Concatenation$a . $bConcatenates $a and $b
.=Concatenate and assign$a .= $bAppends $b to $a
<?php
$message = "Hello";
$message .= " World!";
echo $message;
?>

Post a Comment

Thanks for comment.

Previous Post Next Post