SlideShare a Scribd company logo
Expressions and Operators
Expressions
 An expression is a bit of PHP that can be
evaluated to produce a value. The simplest
expressions are literal values and variables.
 A literal value evaluates to itself, while a variable
evaluates to the value stored in the variable.
 More complex expressions can be formed using
simple expressions and operators.
Operators
 An operator takes some values (the operands)
and does something (for instance, adds them
together).
 Operators are written as punctuation symbols—
for instance, the + and - familiar to us from math.
 Some operators modify their operands, while
most do not.
Number of operands
 Most operators in PHP are binary operators(they
combine two operands (or expressions)into a
single ,more complex
expression.
 PHP also supports a number of unary operator
which
convert a single expression into a more complex
expression.
 PHP supports a single ternary operator that
combines three
expressions into a single expression.
Operator Precedence
 The order in which operators in an expression are
evaluated depends on their relative precedence. For
example, you might write:
 2 + 4 * 3
 As you can see in, the addition and multiplication
operators have different precedence, with
multiplication higher than addition. So the
multiplication happens before the addition,
giving 2 + 12, or 14, as the answer.
 One way many programmers deal with the
complex precedence rules in programming languages
is to reduce precedence down to two rules:
 Multiplication and division have higher precedence
than addition and subtraction.
 Use parentheses for anything else
Operator Associativity
 Associativity defines the order in which operators with
the same order of precedence are evaluated. For
example, look at:
 2 / 2 * 2
 The division and multiplication operators have the
same precedence, but the result of the expression
depends on which operation we do first:
 2/(2*2) // 0.5
 (2/2)*2 // 2
 The division and multiplication operators are left-
associative; this means that in cases of ambiguity, the
operators are evaluated from left to right. In this
example, the correct result is 2.
PHP Arithmetic Operators
Addition
<html>
<body>
<?php
$x=10;
$y=9;
$z=$x + $y;
echo $z;
?>
</body>
</html>
Subtraction
<html>
<body>
<?php
$x = 10;
$y = 6;
echo $x - $y;
?>
</body>
</html>
Multiplication
<html>
<body>
<?php
$x=10;
$y=9;
$z=$x - $y;
echo $z;
?>
</body>
</html>
Modulus
<html>
<body>
<?php
$x=10;
$y=9;
$z=$x % $y;
echo $z;
?>
</body>
</html>
PHP Incrementing/Decrementing
Operators
Pre-increment
<html>
<body>
<?php
$x=10;
++$x;
echo $x;
?>
</body>
</html>
Post-increment
<html>
<body>
<?php
$x=10;
$x++;
echo $x;
?>
</body>
</html>
Pre-decrement
<html>
<body>
<?php
$x=10;
--$x;
echo $x;
?>
</body>
</html>
Post-decrement
<html>
<body>
<?php
$x=10;
$x--;
echo $x;
?>
</body>
</html>
PHP Assignment Operators
The left operand gets set to the value
of the expression on the right
<html>
<body>
<?php
$x = 10;
echo $x;
?>
</body>
</html>
Addition(x += y)
<?php
$x = 20;
$x += 100;
echo $x;
?>
</body>
</html>
Subtraction(x -= y)
<html>
<body>
<?php
$x = 50;
$x -= 30;
echo $x;
?>
</body>
</html>
Multiplication(x *= y)
<html>
<body>
<?php
$x = 10;
$y = 6;
echo $x *= $y;
?>
</body>
</html>
Division(x /= y)
<html>
<body>
<?php
$x = 10;
$x /= 5;
echo $x;
?>
</body>
</html>
Modulus(x %= y)
<html>
<body>
<?php
$x = 15;
$x %= 4;
echo $x;
?>
</body>
</html>
PHP Comparison Operators
Equal(==)
<html>
<body>
<?php
$x = 100;
$y = "100";
var_dump($x == $y); // returns true because values
are equal
?>
</body>
</html>
Identical(===)
<html>
<body>
<?php
$x = 100;
$y = "100";
var_dump($x === $y); // returns false because types are
not equal
?>
</body>
</html>
Not equal(!=)
<html>
<body>
<?php
$x = 100;
$y = "100";
var_dump($x != $y); // returns false because values
are equal
?>
</body>
</html>
Not equal(<>)
<html>
<body>
<?php
$x = 100;
$y = "100";
var_dump($x <> $y); // returns false because
values are equal
?>
</body>
</html>
Not identical(!==)
<html>
<body>
<?php
$x = 100;
$y = "100";
var_dump($x !== $y); // returns true because types are
not equal
?>
</body>
</html>
Greater than(>)
<html>
<body>
<?php
$x = 100;
$y = 50;
var_dump($x > $y); // returns true because $x is greater
than $y
?>
</body>
</html>
Greater than or equal to(>=)
<html>
<body>
<?php
$x = 50;
$y = 50;
var_dump($x >= $y); // returns true because $x is
greater than or equal to $y
?>
</body>
</html>
Less than or equal to(<=)
<html>
<body>
<?php
$x = 50;
$y = 50;
var_dump($x <= $y); // returns true because $x is less
than or equal to $y
?>
</body>
</html>
PHP Logical Operators
and $x and $y)
<html>
<body>
<?php
$x = 100;
$y = 50;
if ($x == 100 and $y == 50) {
echo "Hello world!";
}
?>
</body>
</html>
&& ($x && $y)
<html>
<body>
<?php
$x = 100;
$y = 50;
if ($x == 100 && $y == 50) {
echo "Hello world!";
}
?>
</body>
</html>
Or($x or $y)
<html>
<body>
<?php
$x = 100;
$y = 50;
if ($x == 100 or $y == 80) {
echo "Hello world!";
}
?>
</body>
</html>
II ($x || $y)
<html>
<body>
<?php
$x = 100;
$y = 50;
if ($x == 100 || $y == 80) {
echo "Hello world!";
}
?>
</body>
</html>
Xor ($x xor $y)
<html>
<body>
<?php
$x = 100;
$y = 50;
if ($x == 100 xor $y == 80)
{
echo "Hello world!";
}
?>
</body>
</html>
! (!$x)
<html>
<body>
<?php
$x = 100;
if ($x !== 90) {
echo "Hello world!";
}
?>
</body>
</html>
Bitwise Operators
 PHP ’ s bitwise operators let you work on the
individual bits within integer variables. A bit with a
value of 1 is said to be set , whereas a bit with a
value of 0 is unset (or not set).
Example
<?php
$x=2;
$y=4;
echo $x & $y;
echo $x | $y;
echo $x ^ $y;
echo $x << 2;
echo $y >> 2;
?>
 Output: 0, 6, 6, 8, 1
2= 0010
4= 0100
var_dump()
 The var_dump function is used to display
structured information (type and value) about one
or more variable.
 Syntax
 Var_dump(var1,var2,…varn);
<?php
$x=10;
$y=“10”;
Var_dump($x===$y);
?>
String Operators
 There ’ s really only one string operator, and that ’
s the concatenation operator , . (dot). This
operator simply
takes two string values, and joins the right - hand
string onto the left - hand one to make a longer
string.
 For example:
<?php
echo “Amit, “ . “is good boy ”; // “ Amit, is good boy”
?>
Concatenation (.)
<html>
<body>
<?php
$txt1 = "Hello";
$txt2 = " world!";
echo $txt1 . $txt2;
?>
</body>
</html>
Example
<html>
<head>
<title>Some More Practice</title>
</head>
<body>
<?php
$first_number = 10;
$direct_text = 'My variable contains the value of ';
print($direct_text . $first_number);
?>
</body>
</html>
Casting Operators
 The casting operators, (int), (float), (string), (bool),
(array), and (object), allow you to force a value
into a particular type.
 To use a casting operator, put the operator to the
left of the operand.
PHP casting operators
Type Casting
 PHP is a loosely typed language and assigns
types to variables depending what is assigned to
it.
 Example: if a string value is assigned to variable
$var, $var
becomes a string. If an integer value is then
assigned to $var,
it becomes an integer.
 In PHP there are various ways for Type Casting
1. Automatic or implicit type casting( by using
addition operator '+‘)
2. settype() function(To force a variable to be
evaluated as a certain type, or To change the type
of a variable
Implicit Casting or type juggle
 The conversion of a value from one type to
another is called casting.
 This kind of implicit casting is called type
juggling in PHP.
 PHP is a loosely typed language that allows you
to declare a variable and its type simply by using
it. It also automatically converts values from one
type to another whenever required. This is
called implicit casting or type juggle.
Automatic or implicit type
casting
 Example:
<?php
$a = "2";
echo $a."<br>";
$a+= 2;
echo $a."<br>";
$a = $a + 1.3;
echo $a."<br>";
$a = 5 + "10 Little ";
echo $a."<br>";
$a = 5 + "10 Small ";
echo $a."<br>"
?>
Output
Implicit casting rules for binary
arithmetic operations
Type of first operand Type of second operand Conversion performed
Integer Floating point The integer is converted
to a floating-point number
Integer String The string is converted to
a number
Floating point String The string is converted to
a floating-point number
settype() function
 Example:
<?php
$a = "5bar"; // string
$b = true; // Boolean
settype($a, "integer"); // $a is now 5 (integer)
settype($b, "string"); // $b is now "1" (string)
?>
Explicit Casting or Casting
Operator
 The casting operators are allow us to force a
value into
a particular type
 In explicit casting the casting operator put the
operator to the left of the operand.
Example
 Casting affects the way other operators interpret
a value, rather than changing the value in a
variable. For example, the code:
 $a = "5";
 $b = (int)$a;
 assigns $b the integer value of $a; $a remains
the string "5". To cast the value of the variable
itself, you must assign the result of a cast back
into the variable:
 $a = "5"
 $a = (int)$a; // now $a holds an integer
Miscellaneous Operators
 Error suppression (@) Some operators or functions can generate
error messages.
If you don't want a warning thrown when using functions like fopen(),
you can suppress the error but use exceptions:
 Example:
try
{
if (($fp = @fopen($filename, "r")) == false)
{ throw new Exception; }
else
{ do_file_stuff(); }
}
catch (Exception $e)
{ handle_exception(); }
 Conditional (?:) The conditional operator is, depending on the code
we look at, either the most overused or most underused operator. It is
the only ternary (three-operand) operator and is therefore sometimes
just called the ternary operator.

More Related Content

Similar to Expressions and Operators.pptx

Php using variables-operators
Php using variables-operatorsPhp using variables-operators
Php using variables-operators
Khem Puthea
 
Free PHP Book Online | PHP Development in India
Free PHP Book Online | PHP Development in IndiaFree PHP Book Online | PHP Development in India
Free PHP Book Online | PHP Development in India
Deepak Rajput
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requirePHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and require
TheCreativedev Blog
 
P H P Part I, By Kian
P H P  Part  I,  By  KianP H P  Part  I,  By  Kian
P H P Part I, By Kianphelios
 
PHP variables
PHP  variablesPHP  variables
PHP variables
Siddique Ibrahim
 
DIWE - Fundamentals of PHP
DIWE - Fundamentals of PHPDIWE - Fundamentals of PHP
DIWE - Fundamentals of PHP
Rasan Samarasinghe
 
What Is Php
What Is PhpWhat Is Php
What Is Php
AVC
 
2014 database - course 2 - php
2014 database - course 2 - php2014 database - course 2 - php
2014 database - course 2 - phpHung-yu Lin
 
Web app development_php_05
Web app development_php_05Web app development_php_05
Web app development_php_05Hassen Poreya
 
Php
PhpPhp
Php introduction
Php introductionPhp introduction
Php introduction
Pratik Patel
 
Web Technology_10.ppt
Web Technology_10.pptWeb Technology_10.ppt
Web Technology_10.ppt
Aftabali702240
 
Php Crash Course
Php Crash CoursePhp Crash Course
Php Crash Coursemussawir20
 
02 Php Vars Op Control Etc
02 Php Vars Op Control Etc02 Php Vars Op Control Etc
02 Php Vars Op Control Etc
Geshan Manandhar
 
unit 1.pptx
unit 1.pptxunit 1.pptx
unit 1.pptx
adityathote3
 
Php-Continuation
Php-ContinuationPhp-Continuation
Php-Continuationlotlot
 
Php mysql
Php mysqlPhp mysql
Php mysql
Alebachew Zewdu
 

Similar to Expressions and Operators.pptx (20)

Php using variables-operators
Php using variables-operatorsPhp using variables-operators
Php using variables-operators
 
Free PHP Book Online | PHP Development in India
Free PHP Book Online | PHP Development in IndiaFree PHP Book Online | PHP Development in India
Free PHP Book Online | PHP Development in India
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requirePHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and require
 
P H P Part I, By Kian
P H P  Part  I,  By  KianP H P  Part  I,  By  Kian
P H P Part I, By Kian
 
PHP variables
PHP  variablesPHP  variables
PHP variables
 
DIWE - Fundamentals of PHP
DIWE - Fundamentals of PHPDIWE - Fundamentals of PHP
DIWE - Fundamentals of PHP
 
Php Basic
Php BasicPhp Basic
Php Basic
 
What Is Php
What Is PhpWhat Is Php
What Is Php
 
2014 database - course 2 - php
2014 database - course 2 - php2014 database - course 2 - php
2014 database - course 2 - php
 
Web app development_php_05
Web app development_php_05Web app development_php_05
Web app development_php_05
 
Php
PhpPhp
Php
 
Php introduction
Php introductionPhp introduction
Php introduction
 
Web Technology_10.ppt
Web Technology_10.pptWeb Technology_10.ppt
Web Technology_10.ppt
 
Php Crash Course
Php Crash CoursePhp Crash Course
Php Crash Course
 
02 Php Vars Op Control Etc
02 Php Vars Op Control Etc02 Php Vars Op Control Etc
02 Php Vars Op Control Etc
 
unit 1.pptx
unit 1.pptxunit 1.pptx
unit 1.pptx
 
Introduction to php basics
Introduction to php   basicsIntroduction to php   basics
Introduction to php basics
 
Php-Continuation
Php-ContinuationPhp-Continuation
Php-Continuation
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
My cool new Slideshow!
My cool new Slideshow!My cool new Slideshow!
My cool new Slideshow!
 

Recently uploaded

Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 

Recently uploaded (20)

Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 

Expressions and Operators.pptx

  • 2. Expressions  An expression is a bit of PHP that can be evaluated to produce a value. The simplest expressions are literal values and variables.  A literal value evaluates to itself, while a variable evaluates to the value stored in the variable.  More complex expressions can be formed using simple expressions and operators.
  • 3. Operators  An operator takes some values (the operands) and does something (for instance, adds them together).  Operators are written as punctuation symbols— for instance, the + and - familiar to us from math.  Some operators modify their operands, while most do not.
  • 4. Number of operands  Most operators in PHP are binary operators(they combine two operands (or expressions)into a single ,more complex expression.  PHP also supports a number of unary operator which convert a single expression into a more complex expression.  PHP supports a single ternary operator that combines three expressions into a single expression.
  • 5. Operator Precedence  The order in which operators in an expression are evaluated depends on their relative precedence. For example, you might write:  2 + 4 * 3  As you can see in, the addition and multiplication operators have different precedence, with multiplication higher than addition. So the multiplication happens before the addition, giving 2 + 12, or 14, as the answer.  One way many programmers deal with the complex precedence rules in programming languages is to reduce precedence down to two rules:  Multiplication and division have higher precedence than addition and subtraction.  Use parentheses for anything else
  • 6. Operator Associativity  Associativity defines the order in which operators with the same order of precedence are evaluated. For example, look at:  2 / 2 * 2  The division and multiplication operators have the same precedence, but the result of the expression depends on which operation we do first:  2/(2*2) // 0.5  (2/2)*2 // 2  The division and multiplication operators are left- associative; this means that in cases of ambiguity, the operators are evaluated from left to right. In this example, the correct result is 2.
  • 9. Subtraction <html> <body> <?php $x = 10; $y = 6; echo $x - $y; ?> </body> </html>
  • 18. The left operand gets set to the value of the expression on the right <html> <body> <?php $x = 10; echo $x; ?> </body> </html>
  • 19. Addition(x += y) <?php $x = 20; $x += 100; echo $x; ?> </body> </html>
  • 20. Subtraction(x -= y) <html> <body> <?php $x = 50; $x -= 30; echo $x; ?> </body> </html>
  • 21. Multiplication(x *= y) <html> <body> <?php $x = 10; $y = 6; echo $x *= $y; ?> </body> </html>
  • 22. Division(x /= y) <html> <body> <?php $x = 10; $x /= 5; echo $x; ?> </body> </html>
  • 23. Modulus(x %= y) <html> <body> <?php $x = 15; $x %= 4; echo $x; ?> </body> </html>
  • 25. Equal(==) <html> <body> <?php $x = 100; $y = "100"; var_dump($x == $y); // returns true because values are equal ?> </body> </html>
  • 26. Identical(===) <html> <body> <?php $x = 100; $y = "100"; var_dump($x === $y); // returns false because types are not equal ?> </body> </html>
  • 27. Not equal(!=) <html> <body> <?php $x = 100; $y = "100"; var_dump($x != $y); // returns false because values are equal ?> </body> </html>
  • 28. Not equal(<>) <html> <body> <?php $x = 100; $y = "100"; var_dump($x <> $y); // returns false because values are equal ?> </body> </html>
  • 29. Not identical(!==) <html> <body> <?php $x = 100; $y = "100"; var_dump($x !== $y); // returns true because types are not equal ?> </body> </html>
  • 30. Greater than(>) <html> <body> <?php $x = 100; $y = 50; var_dump($x > $y); // returns true because $x is greater than $y ?> </body> </html>
  • 31. Greater than or equal to(>=) <html> <body> <?php $x = 50; $y = 50; var_dump($x >= $y); // returns true because $x is greater than or equal to $y ?> </body> </html>
  • 32. Less than or equal to(<=) <html> <body> <?php $x = 50; $y = 50; var_dump($x <= $y); // returns true because $x is less than or equal to $y ?> </body> </html>
  • 34. and $x and $y) <html> <body> <?php $x = 100; $y = 50; if ($x == 100 and $y == 50) { echo "Hello world!"; } ?> </body> </html>
  • 35. && ($x && $y) <html> <body> <?php $x = 100; $y = 50; if ($x == 100 && $y == 50) { echo "Hello world!"; } ?> </body> </html>
  • 36. Or($x or $y) <html> <body> <?php $x = 100; $y = 50; if ($x == 100 or $y == 80) { echo "Hello world!"; } ?> </body> </html>
  • 37. II ($x || $y) <html> <body> <?php $x = 100; $y = 50; if ($x == 100 || $y == 80) { echo "Hello world!"; } ?> </body> </html>
  • 38. Xor ($x xor $y) <html> <body> <?php $x = 100; $y = 50; if ($x == 100 xor $y == 80) { echo "Hello world!"; } ?> </body> </html>
  • 39. ! (!$x) <html> <body> <?php $x = 100; if ($x !== 90) { echo "Hello world!"; } ?> </body> </html>
  • 40. Bitwise Operators  PHP ’ s bitwise operators let you work on the individual bits within integer variables. A bit with a value of 1 is said to be set , whereas a bit with a value of 0 is unset (or not set).
  • 41. Example <?php $x=2; $y=4; echo $x & $y; echo $x | $y; echo $x ^ $y; echo $x << 2; echo $y >> 2; ?>  Output: 0, 6, 6, 8, 1 2= 0010 4= 0100
  • 42. var_dump()  The var_dump function is used to display structured information (type and value) about one or more variable.  Syntax  Var_dump(var1,var2,…varn); <?php $x=10; $y=“10”; Var_dump($x===$y); ?>
  • 43. String Operators  There ’ s really only one string operator, and that ’ s the concatenation operator , . (dot). This operator simply takes two string values, and joins the right - hand string onto the left - hand one to make a longer string.  For example: <?php echo “Amit, “ . “is good boy ”; // “ Amit, is good boy” ?>
  • 44. Concatenation (.) <html> <body> <?php $txt1 = "Hello"; $txt2 = " world!"; echo $txt1 . $txt2; ?> </body> </html>
  • 45. Example <html> <head> <title>Some More Practice</title> </head> <body> <?php $first_number = 10; $direct_text = 'My variable contains the value of '; print($direct_text . $first_number); ?> </body> </html>
  • 46. Casting Operators  The casting operators, (int), (float), (string), (bool), (array), and (object), allow you to force a value into a particular type.  To use a casting operator, put the operator to the left of the operand.
  • 48. Type Casting  PHP is a loosely typed language and assigns types to variables depending what is assigned to it.  Example: if a string value is assigned to variable $var, $var becomes a string. If an integer value is then assigned to $var, it becomes an integer.  In PHP there are various ways for Type Casting 1. Automatic or implicit type casting( by using addition operator '+‘) 2. settype() function(To force a variable to be evaluated as a certain type, or To change the type of a variable
  • 49. Implicit Casting or type juggle  The conversion of a value from one type to another is called casting.  This kind of implicit casting is called type juggling in PHP.  PHP is a loosely typed language that allows you to declare a variable and its type simply by using it. It also automatically converts values from one type to another whenever required. This is called implicit casting or type juggle.
  • 50. Automatic or implicit type casting  Example: <?php $a = "2"; echo $a."<br>"; $a+= 2; echo $a."<br>"; $a = $a + 1.3; echo $a."<br>"; $a = 5 + "10 Little "; echo $a."<br>"; $a = 5 + "10 Small "; echo $a."<br>" ?> Output
  • 51. Implicit casting rules for binary arithmetic operations Type of first operand Type of second operand Conversion performed Integer Floating point The integer is converted to a floating-point number Integer String The string is converted to a number Floating point String The string is converted to a floating-point number
  • 52. settype() function  Example: <?php $a = "5bar"; // string $b = true; // Boolean settype($a, "integer"); // $a is now 5 (integer) settype($b, "string"); // $b is now "1" (string) ?>
  • 53. Explicit Casting or Casting Operator  The casting operators are allow us to force a value into a particular type  In explicit casting the casting operator put the operator to the left of the operand.
  • 54. Example  Casting affects the way other operators interpret a value, rather than changing the value in a variable. For example, the code:  $a = "5";  $b = (int)$a;  assigns $b the integer value of $a; $a remains the string "5". To cast the value of the variable itself, you must assign the result of a cast back into the variable:  $a = "5"  $a = (int)$a; // now $a holds an integer
  • 55. Miscellaneous Operators  Error suppression (@) Some operators or functions can generate error messages. If you don't want a warning thrown when using functions like fopen(), you can suppress the error but use exceptions:  Example: try { if (($fp = @fopen($filename, "r")) == false) { throw new Exception; } else { do_file_stuff(); } } catch (Exception $e) { handle_exception(); }  Conditional (?:) The conditional operator is, depending on the code we look at, either the most overused or most underused operator. It is the only ternary (three-operand) operator and is therefore sometimes just called the ternary operator.