0% found this document useful (0 votes)
21 views

LECTURE 3-Expressions & Operators

The document discusses various operators in PHP including arithmetic, string, comparison, logical, and assignment operators. It covers topics like operator precedence, associativity, implicit type casting, and provides examples of different operators.

Uploaded by

Hemalatha B
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

LECTURE 3-Expressions & Operators

The document discusses various operators in PHP including arithmetic, string, comparison, logical, and assignment operators. It covers topics like operator precedence, associativity, implicit type casting, and provides examples of different operators.

Uploaded by

Hemalatha B
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43

Sri Krishna Arts And Science College

Department of ICT

Course Title: PHP & MYSQL


Course Code: 19CSS22
Class : III BSc – Computer Technology “A”
Google Class room Code: 7cxe5v3

Online Material
Lecture 3: Expressions & Operators

Facilitator
B.Hemalatha

Assistant Professor

Department of Computer Technology


SKASC
Objective
 Number of Operands
 Operator Precedence
 Operator Associativity
 Implicit Casting
 Arithmetic Operators
 String Concatenation Operator
 Auto-increment and Auto-decrement Operators
 Comparison Operators
 Bitwise Operators
 Logical Operators
 Casting Operators
 Assignment Operators
 Assignment - Assignment with operation
 Miscellaneous Operators
Text Books
1. Rasmus Lerdorf, Kevin Tatroe, Bob Kaehms, RicMcGredy
(2020), Programming PHP, O’REILLY(SPD), 4th Edition.
2. Vikram Vaswani, 2017, “PHP A Beginners Guide”, Tata
McGrawHill
3. Lee Babin, Nathan A. Good, Frank M. Kromann, Jon
Stephens (2013), “PHP 5Recipes, A problem solution
approach”, après
OPERATORS IN PHP

Topic: Operators in PHP


Reference: Kris Isaacson Channel
OPERATORS IN PHP

Topic: Operators in PHP


Reference: Chidres Tech Tutorial
Expression

 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.
 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
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 operators, 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

 Precedence of operators decides the order of execution of


operators in an expression.
 For example in 2+6/3, division of 6/3 is done first and then
addition of 2+2 takesplace because division operator / has
higher precedence over addition operator +.
 To force a certain operator to be called before other,
parentheses should be used.
 Some operators may have same level of precedence.
 In that case, the order of associativity (either left or right)
decides the order of operations
Operator Precedence
Operators Purpose
** exponentiation
++ -- increment/decrement
~(int) (float) (string) (array) casting
(object) (bool)
instanceof types
! logical
*/ multiplication/division
% modulo
+-. arithmetic and string
<< >> bitwise shift
< <= > >= comparison
== != === !== <> <=> comparison
& bitwise and/references
^ bitwise XOR
| bitwise OR
Operator Precedence
Operators Purpose
&& logical and
|| logical or
?? null coalescing

?: ternary
= += -= *= **= /= .= %= &= |= assignment operators
^= <<= >>= ??=
and logical
xor logical
or logical
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.
Implicit Casting
 Many operators have expectations of their operands binary
Math operators typically require both operands to be of the
same type.
 PHP's variables can store integers, floating-point numbers,
strings, and more, and to keep as much of the type details
away from the programmer as possible,
 PHP converts values from one type to another as necessary.
The conversion of a value from one type to another is called
casting.
 This kind of implicit casting is called type juggling in PHP
Implicit casting rules for binary arithmetic
operations
Type of first operand Type of second Conversion performed
operand

Integer Floating point The integer is converted to a


floating-point number

Integer String The string is converted to a


number; if the value after
conversion is a floating-point
number, the integer is
converted to a floating-point
number
Floating Point String The string is converted to a
floating-point number
Implicit Casting

 You can use a string anywhere PHP expects a number.


 The string is presumed to start with an integer or floating-
point number.
 If no number is found at the start of the string, the numeric
value of that string is 0.
 If the string contains a period (.) or upper- or lowercase e,
evaluating it numerically produces a floating-point number.
Example:"9 Lives" - 1; // 8 (int)
"3.14 Pies" * 2; // 6.28 (float)
"9 Lives." - 1; // 8.0 (float)
Arithmetic Operators
▪ The arithmetic operators are operators you'll recognize from
everyday use.
▪ Most of the arithmetic operators are binary; however, the
arithmetic negation and arithmetic assertion operators are
unary.
▪ These operators require numeric values, and non-numeric
values are converted into numeric values
▪ The arithmetic operators are: Addition (+) The result of the
addition operator is the sum of the two operands.
Arithmetic Operators
▪ Subtraction (-) The result of the subtraction operator is the
difference between the two operands; i.e., the value of the
second operand subtracted from the first.
▪ Multiplication (*) The result of the multiplication operator is the
product of the two operands.
▪ Division (/) The result of the division operator is the quotient of
the two operands.
▪ Modulus (%) The modulus operator converts both operands to
integers and returns the remainder of the division of the first
operand by the second operand.
Arithmetic Operators
▪ Arithmetic negation (-) The arithmetic negation operator
returns the operand multiplied by -1, effectively changing its
sign. For example, -(3 - 4) evaluates to 1.
▪ Arithmetic assertion (+) The arithmetic assertion operator
returns the operand multiplied by +1, which has no effect.
▪ It is used only as a visual cue to indicate the sign of a value.
For example, +(3 - 4) evaluates to -1, just as (3 - 4) does.
String Concatenation Operator
 Manipulating strings is such a core part of PHP applications
that PHP has a separate string concatenation operator (.).
 The concatenation operator appends the righthand operand to
the lefthand operand and returns the resulting string.
 Operands are first converted to strings, if necessary.

 Example: $n = 5;

 $s = 'There were ' . $n . ' ducks.'; // $s is 'There were 5 ducks'


Autoincrement and Autodecrement Operators
 These operators are unique in that they work only on variables;
 Operators change their operands' values as well as returning a
value.
 There are two ways to use autoincrement or autodecrement
in expressions.
 If you put the operator in front of the operand, it returns the new
value of the operand (incremented or decremented).
 If you put the operator after the operand, it returns the original
value of the operand
Autoincrement and autodecrement operations

Operator Name Value Effect on


returned $var
$var Post- ++ $var Incremented
increment

++$var Pre-increment $var + 1 Incremented


Incremented

$var-- Post- $var Decremented


decrement

--$var Pre- $var - 1 $var - 1


decrement
Comparison Operators
 As their name suggests, comparison operators compare
operands.
 The result is always either true, if the comparison is truthful,
or false, otherwise.
 Operands to the comparison operators can be both numeric,
both string, or one numeric and one string.
Comparison Operators

First Operand Second Operand Comparison

Number Number Numeric

String that is entirely String that is entirely Numeric


numeric numeric
String that is entirely Number Numeric
numeric
String that is entirely Number Lexigrophic
numeric
String that is entirely String that is entirely Lexigrophic
numeric numeric
String that is not String that is not Lexigrophic
entirely numeric entirely numeric
Comparison Operators
 Equality (==) If both operands are equal, this operator
returns true; otherwise, it returns false.
 Identical (===) If both operands are equal and are of the
same type, this operator returns true; otherwise, it returns
false. Note that this operator does not do implicit type casting
 Inequality (!= or <>) If both operands are not equal, this
operator returns true; otherwise, it returns false.
 Not identical (!==) If both operands are not equal, or they
are not of the same type, this operator returns true;
otherwise, it returns false.
Comparison Operators
 Greater than (>) If the lefthand operator is greater than the
righthand operator, this operator returns true; otherwise, it
returns false.
 Greater than or equal to (>=) If the lefthand operator is
greater than or equal to the righthand operator, this operator
returns true; otherwise, it returns false.
 Less than (<) If the lefthand operator is less than the
righthand operator, this operator returns true; otherwise, it
returns false.
Comparison Operators
Less than or equal to (<=)
 If the lefthand operator is less than or equal to the righthand
operator, this operator returns true; otherwise, it returns
false.
Bitwise operators
 The bitwise operators act on the binary representation of
their operands.
 Each operand is first turned into a binary representation of
the value
 Bitwise negation (~)

 Changes 1s to 0s and 0s to 1s in the binary


representations of the operands.
 Floating-point values are converted to integers before the
operation takes place.
Bitwise operators
 If the operand is a string, the resulting value is a string the
same length as the original, with each character in the
string negated.
 Bitwise AND (&)

 Compares each corresponding bit in the binary


representations of the operands.
 If both bits are 1, the corresponding bit in the result is 1;
otherwise, the corresponding bit is 0.
Bitwise operators
 Bitwise OR (|)

 Compares each corresponding bit in the binary representations


of the operands.
 If both bits are 0, the resulting bit is 0;

 If both operands are strings, the operator returns a string in


which each character is the result of a bitwise OR operation
between the two corresponding characters in the operands.
 The resulting string is the length of the longer of the two
operands, and the shorter string is padded at the end with
binary 0s.
Bitwise operators
▪ Left shift (<<)

▪ Shifts the bits in the binary representation of the lefthand


operand left by the number of places given in the righthand
operand.
▪ Both operands will be converted to integers if they aren't
already.
▪ Shifting a binary number to the left inserts a 0 as the rightmost
bit of the number and moves all other bits to the left one place.
▪ Ex:3 << 1 (or binary 11 shifted one place left) results in 6
(binary 110).
Bitwise operators
▪ Right shift (>>)

▪ Shifts the bits in the binary representation of the lefthand


operand right by the number of places given in the righthand
operand.
▪ Shifting a binary number to the right inserts a 0 as the leftmost
bit of the number and moves all other bits to the right one
place. The rightmost bit is discarded.
▪ Ex:13 >> 1 (or binary 1101) shifted one place right results in 6
(binary 110).
Logical Operators
 Treat their operands as Boolean values and return a Boolean
value.
 Logical AND (&&, and)

 Result of the logical AND operation is true if and only if both


operands are true; otherwise, it is false.
 If the value of the first operand is false, the logical AND
operator knows that the resulting value must also be false, so
the righthand operand is never evaluated.
 This process is called short-circuiting
Logical Operators
 Logical OR (||, or)

 Result of the logical OR operation is true if either operand is


true; otherwise, the result is false.
 Like the logical AND operator, the logical OR operator is short-
circuited.
 If the lefthand operator is true, the result of the operator must
be true, so the righthand operator is never evaluated
Logical Operators
 Logical XOR (xor)

 Result of the logical XOR operation is true if either operand,


but not both, is true; otherwise, it is false.
 Logical negation (!)

 Returns the Boolean value true if the operand evaluates to


false, and false if the operand evaluates to true.
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.
Operators Synonymous Changes type
operators
(int) (integer) Integer
(float) (real) Floating point
(string) String
(bool) (boolean) Boolean
(array) Array
(object) Object
Casting Operators
 Ex: $a = "5" $a = (int) $a; // now $a holds an integer

Casting an object to an array builds an array of the properties,


mapping property names to values:
class Person
{
var $name = "Fred";
var $age = 35; Fred
35
}
$o = new Person;
$a = (array) $o;
print_r($a);
Array ( [name] => Fred [age] => 35 )
Assignment operator
 The basic assignment operator (=) assigns a value to a
variable.
 Assignment with operation

 Plus-equals (+=) Adds the righthand operand to the value of


the lefthand operand, then assigns the result to the lefthand
operand.
 $a += 5 is the same as $a = $a + 5.

 Minus-equals (-=) Subtracts the righthand operand from the


value of the lefthand operand, then assigns the result to the
lefthand operand.
Assignment operator
 Divide-equals (/=) Divides the value of the lefthand operand
by the righthand operand, then assigns the result to the
lefthand operand.
 Multiply-equals (*=) Multiplies the righthand operand with the
value of the lefthand operand, then assigns the result to the
lefthand operand.
 Modulus-equals (%=) Performs the modulus operation on the
value of the lefthand operand and the righthand operand, then
assigns the result to the lefthand operand.
Assignment operator
 Bitwise-OR-equals (|=) Performs a bitwise OR on the value
of the lefthand operand and the righthand operand, then
assigns the result to the lefthand operand.
 Concatenate-equals (.=) Concatenates the righthand
operand to the value of the lefthand operand, then assigns the
result to the lefthand operand.
Miscellaneous Operators
 Error suppression (@) Some operators or functions can
generate error messages.
 Used to prevent these messages from being created.

 Execution (`...`) The backtick operator executes the string


contained between the backticks as a shell command and
returns the output.
 For example: $listing = `ls -ls /tmp`;

 echo $listing;
Miscellaneous Operators
 Conditional (?:) It is the only ternary (three-operand)
operator and is therefore sometimes just called the ternary
operator.
 The conditional operator evaluates the expression before
the ?.
 If the expression is true, the operator returns the value of the
expression between the ? and :;
 otherwise, the operator returns the value of the expression
after the :.
Assignment
Can I retrieve a single character of a string? How?

Identify which of the following variable names are invalid:

$24

$IAMHERE

$_error

$^b

${$var}

$yA_K
Do It Yourself

Click
THANK YOU

You might also like