0% found this document useful (0 votes)
11 views18 pages

1.5. Expressions and Operators

The document outlines the first module of the PHP and JS Framework course at SRI Krishna College of Engineering and Technology, focusing on expressions and operators in PHP. It covers various types of operators including arithmetic, assignment, string, increment/decrement, comparison, and logical operators, along with examples for each. Additionally, it provides references for further reading on PHP.

Uploaded by

Karthikeyini S
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)
11 views18 pages

1.5. Expressions and Operators

The document outlines the first module of the PHP and JS Framework course at SRI Krishna College of Engineering and Technology, focusing on expressions and operators in PHP. It covers various types of operators including arithmetic, assignment, string, increment/decrement, comparison, and logical operators, along with examples for each. Additionally, it provides references for further reading on PHP.

Uploaded by

Karthikeyini S
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/ 18

SRI KRISHNA COLLEGE OF ENGINEERING AND TECHNOLOGY

Kuniamuthur, Coimbatore, Tamilnadu, India


An Autonomous Institution, Affiliated to Anna University,
Accredited by NAAC with “A” Grade & Accredited by NBA (CSE, ECE, IT, MECH ,EEE, CIVIL& MCT)

Course : 21CSI504 – PHP and JS Framework


Module : 1
Topic : Expressions and Operators
Faculty : Dr. S. Karthikeyini
Department : M.Tech – Computer Science & Engineering

1
Topics Covered

▪ PHP Expressions
▪ PHP Operators
▪ Arithmetic Operators
▪ Assignment Operators
▪ String Operators
▪ Increment / Decrement Operators
▪ Comparison Operators
▪ Logical Operators

2
PHP
Expressions

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 (=)
Syntax
$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
PHP
Arithmetic Operators
Operators
Example:
<?php
$x=10;
$y=6;
echo ($x + $y); // outputs 16
echo ($x - $y); // outputs 4
echo ($x * $y); // outputs 60
echo ($x / $y); // outputs 1.6666666666667
echo ($x % $y); // outputs 4
?>
PHP
Assignment Operators
Operators

• The PHP assignment operators are used to write a value to a variable.


• The basic assignment operator in PHP is "=". It means that the left
operand gets set to the value of the assignment expression on the right.
Example
<?php
$x=10; echo $x; // outputs 10

$y=20; $y += 100; echo $y; // outputs 120

$z=50; $z -= 25; echo $z; // outputs 25

$i=5;
$i *= 6; echo $i; // outputs 30

$j=10;
$j /= 5; echo $j; // outputs 2

$k=15;
$k %= 4; echo $k; // outputs 3
?>
PHP
Comparison Operators
Operators
Example
<?php
$x=100;
$y="100";
Output:
bool(true)
var_dump($x == $y); bool(false)
echo "<br>"; bool(false)
var_dump($x === $y);
bool(true)
echo "<br>";
var_dump($x != $y); bool(false)
echo "<br>"; bool(true)
var_dump($x !== $y);
echo "<br>";

$a=50;
$b=90;

var_dump($a > $b);


echo "<br>";
var_dump($a < $b);
?>
PHP
Logical Operators
Operators
PHP
Conditional Assignment Operators
Operators
PHP String Operators
Operator Name Example Result
. Concatenation $txt1 = "Hello" Now $txt2 contains
$txt2 = $txt1 . "Hello world!"
" world!"
.= Concatenation $txt1 = "Hello" Now $txt1 contains
assignment $txt1 .= " "Hello world!"
world!"

Example:
<?php
$a = "Hello";
$b = $a . " world!";
echo $b; // outputs Hello world!
$x="Hello";
$x .= " world!";
echo $x; // outputs Hello world!
?>
PHP Increment / Decrement Operators

Operator Name Description


++$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

Example:
<?php
$x=10;
echo ++$x; // outputs 11
$y=10;
echo $y++; // outputs 10
$z=5;
echo --$z; // outputs 4
$i=5;
echo $i--; // outputs 5
?>
PHP Array Operators
The PHP array operators are used to compare arrays :
Operator Name Example Result
+ Union $x + $y Union of $x and $y (but
duplicate keys are not
overwritten)
== Equality $x == $y True if $x and $y have the
same key/value pairs

=== Identity $x === $y True if $x and $y have the


same key/value pairs in the
same order and of the same
types

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

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

!== Non-identity $x !== $y True if $x is not identical to


$y
Example
• <?php  Output:
$x = array("a" => "red", "b" =>  array(4) { ["a"]=>
"green"); string(3) "red" ["b"]=>
string(5) "green"
$y = array("c" => "blue", "d" =>
["c"]=> string(4)
"yellow"); "blue" ["d"]=>
$z = $x + $y; // union of $x and $y string(6) "yellow" }
var_dump($z); bool(false)
var_dump($x == $y); bool(false)
var_dump($x === $y); bool(true)
var_dump($x != $y); bool(true)
var_dump($x <> $y); bool(true)
var_dump($x !== $y);
?>
Summary
▪ PHP Expressions
▪ PHP Operators
▪ Arithmetic Operators
▪ Assignment Operators
▪ String Operators
▪ Increment / Decrement Operators
▪ Comparison Operators
▪ Logical Operators
References
1. Steven Holzner, “PHP:The Complete
Reference”, McGraw Hill Education, 2017
2. https://www.w3schools.com/php/
3. https://www.tutorialspoint.com/php/index.htm
18

You might also like