0% found this document useful (0 votes)
24 views27 pages

PHP Expressions - Operators

Uploaded by

sachin248124
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views27 pages

PHP Expressions - Operators

Uploaded by

sachin248124
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

PHP OPERATORS

Presented By:-
Er. Yatika Hasija
Assistant Professor
CSE Department
Lovely Professional University
PHP Expressions
Almost everything in a PHP script is an expression.
Anything that has a value is an expression.
In a typical assignment statement ($x=100), a literal value,
a function or operands processed by operators is an
expression, anything that appears to the right of
assignment operator (=)
Expression
• $x=100; //100 is an expression
• $a=$b+$c; //b+$c is an expression
• $c=add($a,$b); //add($a,$b) is an expresson
• $val=sqrt(100); //sqrt(100) is an expression
• $var=$x!=$y; //$x!=$y is an expression
Introduction
• Operators are symbols that tell the PHP processor to
perform certain actions.

- Arithmetic operators
- Assignment operators
- Comparison operators
- Increment/Decrement operators
- String operators
- Array operators
- Conditional assignment operators
- Logical operators
Arithmetic operators
• The PHP arithmetic operators are used with numeric
values to perform common arithmetical operations, such
as addition, subtraction, multiplication etc.
Operator Description Example Result
+ Addition $x + $y Sum of $x and $y

- Subtraction $x - $y Difference of $x and


$y.
* Multiplication $x * $y Product of $x and $y.

/ Division $x / $y Quotient of $x and


$y
% Modulus $x % $y Remainder of $x
divided by $y
** Exponentiation $x ** $y Result of raising $x
to the $y'th power
Arithmetic operators(contd.)
<?php
$x = 10;
$y = 4;
echo($x + $y); // 0utputs: 14
echo "<br>";
echo($x - $y); // 0utputs: 6
echo "<br>";
echo($x * $y); // 0utputs: 40
echo "<br>";
echo($x / $y); // 0utputs: 2.5
echo "<br>";
echo($x % $y); // 0utputs: 2
echo "<br>";
echo($x ** $y); // 0utputs: 10000
?>
Assignment operators
• The PHP assignment operators are used with numeric
values to write a value to a variable.

Operator Description Example Is The Same


As
= Assign $x = $y $x = $y
+= Add and assign $x += $y $x = $x + $y
-= Subtract and assign $x -= $y $x = $x - $y
*= Multiply and assign $x *= $y $x = $x * $y
/= Divide and assign $x /= $y $x = $x / $y
quotient
%= Divide and assign $x %= $y $x = $x % $y
modulus
Assignment operators(contd.)
<?php
$x = 10;
echo $x; // Outputs: 10
echo "<br>";
$x = 20;
$x += 30;
echo $x; // Outputs: 50
echo "<br>";
$x = 50;
$x -= 20;
echo $x; // Outputs: 30
echo "<br>";
$x = 5;
$x *= 25;
echo $x; // Outputs: 125
echo "<br>";
$x = 50;
$x /= 10;
echo $x; // Outputs: 5
echo "<br>";
$x = 100;
$x %= 15;
echo $x; // Outputs: 10
?>
Assignment operators(contd.)
OUTPUT:
10
50
30
125
5
10
Precedence of Operators
• Multiplication (*), division (/), and modulo (%) have higher
precedence than addition (+) and subtraction (-).
• Exponentiation (**) has a higher precedence than
multiplication and division.
Comparison operators
• The comparison operators are used to compare two
values in a Boolean fashion.
Comparison operators(contd.)
Operator Name Example Result

== Equal $x == $y True if $x is equal to $y

=== Identical $x === $y True if $x is equal to $y, and


they are of the same type

!= Not equal $x != $y True if $x is not equal to $y

<> Not equal $x <> $y True if $x is not equal to $y

!== Not identical $x !== $y True if $x is not equal to $y, or


they are not of the same type.
used to check if two values are
not identical. In PHP, two
values are considered identical
if they are both equal (==) and
of the same type.
< Less than $x < $y True if $x is less than $y

> Greater than $x > $y True if $x is greater than $y

>= Greater than or equal to $x >= $y True if $x is greater than or


equal to $y
<= Less than or equal to $x <= $y True if $x is less than or equal
to $y
Comparison operators(contd.)
<?php
$x = 25;
$y = 35;
$z = "25";
var_dump($x == $z); // Outputs: boolean true
var_dump($x === $z); // Outputs: boolean false
var_dump($x != $y); // Outputs: boolean true
var_dump($x !== $z); // Outputs: boolean true
var_dump($x < $y); // Outputs: boolean true
var_dump($x > $y); // Outputs: boolean false
var_dump($x <= $y); // Outputs: boolean true
var_dump($x >= $y); // Outputs: boolean false
?>
Incrementing and Decrementing Operators
• The increment/decrement operators are used to
increment/decrement a variable's value.
Operator Name Effect
++$x Pre-increment Increments $x by one,
then returns $x
$x++ Post-increment Returns $x, then
increments $x by one
--$x Pre-decrement Decrements $x by
one, then returns $x
$x-- Post-decrement Returns $x, then
decrements $x by one
Incrementing and Decrementing
Operators(contd.)
<html>
<?php
$x = 10;
echo ++$x; // Outputs: 11
echo $x; // Outputs: 11
echo "<br>";
$x = 10;
echo $x++; // Outputs: 10
echo $x; // Outputs: 11
echo "<br>";
$x = 10;
echo --$x; // Outputs: 9
echo $x; // Outputs: 9
echo "<br>";
$x = 10;
echo $x--; // Outputs: 10
echo $x; // Outputs: 9
?>
</html>
Incrementing and Decrementing
Operators(contd.)
OUTPUT:
1111
1011
99
109
String Operators
• The string operators are used to perform the operation on
strings.

Operator Description Example Result


. Concatenation $str1 . $str2 Concatenation of
$str1 and $str2
.= Concatenation $str1 .= $str2 Appends the $str2
assignment to the $str1
String Operators(contd.)
<?php
$x = "Hello";
$y = " World!";
echo $x . $y; // Outputs: Hello World!
echo "<br>";
$x .= $y;
echo $x; // Outputs: Hello World!
?>

OUTPUT:
Hello World!
Hello World!
PHP Conditional Assignment Operators
• The PHP conditional assignment operators are used to
set a value depending on conditions:
?: Ternary $x = expr1 ? expr2 : expr3 Returns the value of $x.
The value of $x is expr2 if expr1 = TRUE.
The value of $x is expr3 if expr1 = FALSE

?? Null coalescing $x = expr1 ?? expr2 Returns the value of $x.


The value of $x is expr1 if expr1 exists, and is not NULL.
If expr1 does not exist, or is NULL, the value of $x
is expr2.
Introduced in PHP 7
PHP Conditional Assignment Operators(contd.)
– Ternary operator
<?php
echo $status = (empty($user)) ? "anonymous" : $user;
echo "<br>";

$user = "Michael";
echo $status = (empty($user)) ? "anonymous" : $user;
?>
PHP Conditional Assignment Operators(contd.)
– Null coalescing
<?php
echo $status = $user ?? 'anonymous';
echo "<br>";

$user = "Michael";
echo $status = $user ?? 'anonymous';
?>

OUTPUT:
anonymous
Michael
Logical Operators
• The logical operators are typically used to combine
conditional statements.
Operator Name Example Result
and And $x and $y True if both $x and $y are
true
or Or $x or $y True if either $x or $y is
true
xor Xor $x xor $y True if either $x or $y is
true, but not both
&& And $x && $y True if both $x and $y are
true
|| Or $x || $y True if either $x or $y is
true
! Not !$x True if $x is not true
Example
<?php
$x = 50;
$y = 30;
if ($x == 50 and $y == 30)
echo "and Success \n";

if ($x == 50 or $y == 20)
echo "or Success \n";

if ($x == 50 xor $y == 20)


echo "xor Success \n";

if ($x == 50 && $y == 30)


echo "&& Success \n";

if ($x == 50 || $y == 20)
echo "|| Success \n";

if (!$z)
echo "! Success \n";
?>
Logical Operators(contd.)
<?php
$year = 2014;
// Leap years are divisible by 400 or by 4 but not 100
if(($year % 400 == 0) || (($year % 100 != 0) && ($year % 4 ==
0))){
echo "$year is a leap year.";
} else{
echo "$year is not a leap year.";
}
?>

OUTPUT:
2014 is not a leap year.
Spaceship Operators:

PHP 7 has introduced a new kind of operator called


spaceship operator. The spaceship operator or combined
comparison operator is denoted by “<=>“. These operators
are used to compare values but instead of returning the
boolean results, it returns integer values.
If both the operands are equal, it returns 0.
If the right operand is greater, it returns -1.
If the left operand is greater, it returns 1.
Spaceship Operators:
Operator Syntax Operation

$x < $y $x <=> $y Identical to -1 (right is greater)

$x > $y $x <=> $y Identical to 1 (left is greater)

Identical to -1 (right is greater) or


$x <= $y $x <=> $y
identical to 0 (if both are equal)

Identical to 1 (if left is greater) or


$x >= $y $x <=> $y
identical to 0 (if both are equal)

$x == $y $x <=> $y Identical to 0 (both are equal)

$x != $y $x <=> $y Not Identical to 0


• <?php
• $x = 50;
• $y = 50;
• $z = 25;
• echo $x <=> $y; //output 0
• echo "\n";
• echo $x <=> $z; //output 1
• echo "\n";
• echo $z <=> $y; //output -1
• echo "\n";
• ?>

You might also like