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

php-unit-1-operator-and-expression_part-2

Uploaded by

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

php-unit-1-operator-and-expression_part-2

Uploaded by

madhandhoni2004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Unit 1: Operators

1. PHP Arithmetic Operators


The PHP arithmetic operators are used with numeric values to perform common arithmetical
operations, such as addition, subtraction, multiplication etc.
Operator Name 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 (Introduced in PHP 5.6)

Addition (+)
The result of the addition operator is the sum of the two operands.
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.
For example, 3 * 4 is 12.
Division (/)
The result of the division operator is the quotient of the two operands. Dividing two integers can give
an integer (e.g., 4/2) or a floating-point result (e.g., 1/2).
Modulus (%)
The modulus operator converts both operands to integers and returns the remainder of the division of
the first operand by the second operand. For example, 10% 6 is 4.
Arithmetic negation (-)
 The arithmetic negation operator returns the operand multiplied by -1, effectively changing its sign. For
example, -(3 - 4) evaluates to 1.
 Arithmetic negation is different from the subtraction operator, even though they both are written as a
minus sign.
 Arithmetic negation is always unary and before the operand.
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.

2. PHP Assignment Operators


 The PHP assignment operators are used with numeric values 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.
Assignment Same as... Description
x=y x=y The left operand gets set to the value of the
expression on the right
x += y x=x+y Addition
x -= y x=x-y Subtraction
x *= y x=x*y Multiplication
x /= y x=x/y Division
x %= y x=x%y Modulus

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.
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.
Bitwise-XOR-equals (^=)
Performs a bitwise XOR on the lefthand and righthand operands, then assigns the result to the lefthand
operand.
Bitwise-AND-equals (&=)
Performs a bitwise AND on the value of the lefthand operand and the righthand operand, then assigns
the result to the lefthand operand.
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.

3. PHP Comparison Operators


 As their name says, 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.
 The operators check for truthfulness in slightly different ways based on the types and values of the
operands, either using strictly numeric comparisons or using lexicographic (textual) comparisons.
 One important thing to note is that two numeric strings are compared as if they were numbers.
 If you have two strings that consist entirely of numeric characters and you need to compare them
lexicographically, use the strcmp( ) function.
The PHP comparison operators are used to compare two values (number or string):

Operator Name Example Result


== Equal $x == $y Returns true if $x is equal to $y
=== Identical $x === $y Returns true if $x is equal to $y, and they are
of the same type
!= Not equal $x != $y Returns true if $x is not equal to $y
<> Not equal $x <> $y Returns true if $x is not equal to $y
!== Not identical $x !== $y Returns true if $x is not equal to $y, or they
are not of the same type
> Greater than $x > $y Returns true if $x is greater than $y

< Less than $x < $y Returns true if $x is less than $y

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

<= Less than or equal to $x <= $y Returns true if $x is less than or equal to $y

Type of comparision performed by the comparision operators

First operand Second operand Comparison

Number Number Numeric

String that is entirely numeric String that is entirely numeric Numeric


String that is entirely numeric Number Numeric

String that is not entirely numeric Number Lexicographic

String that is entirely numeric String that is not entirely numeric Lexicographic

String that is not entirely numeric String that is not entirely numeric Lexicographic

The comparison operators are:


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.
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.
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.
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.

4. PHP Increment / Decrement Operators


 In programming, one of the most common operations is to increase or decrease the value of a variable
by one.
 The unary autoincrement (++) and autodecrement (--) operators provide shortcuts for these common
operations.
 There are two ways to use autoincrement or autodecrement in expressions.
i. If you put the operator in front of the operand, it returns the new value of the operand
(incremented or decremented).
ii. If you put the operator after the operand, it returns the original value of the operand
(before the increment or decrement). .
 The PHP increment operators are used to increment a variable's value.
 The PHP decrement operators are used to decrement a variable's value.

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

 These operators can be applied to strings as well as numbers.


 Incrementing an alphabetic character turns it into the next letter in the alphabet.
 Example: incrementing "z" or "Z" wraps it back to "a"or "Z" and increments the previous character by
one, as though the characters were in a base-26 number system.

Autoincrement with letters

Incrementing this Gives this


"a" "b"
"z" "aa"
"spaz" "spba"
"K9" "L0"
"42" "43"
5. PHP Logical Operators
 Logical operators provide ways for you to build complex logical expressions.
 Logical operators treat their operands as Boolean values and return a Boolean value.
 There are both punctuation and English versions of the operators (|| and or are the same operator).
 The PHP logical operators are 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

The logical operators are:

Logical AND (&&, and)


 The 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.
 For example, you might connect to a database only if some flag is not false:
$result = $flag and mysql_connect( );
 The && and and operators differ only in their precedence.
Logical OR (||, or)
 The 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.
 For example:
$result = fopen($filename) or exit( );
 The || and or operators differ only in their precedence.
Logical XOR (xor)
The result of the logical XOR operation is true if either operand, but not both, is true; otherwise, it
is false.
Logical negation (!)
The logical negation operator returns the Boolean value true if the operand evaluates to false,
and false if the operand evaluates to true.
6. PHP String Operators
 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. For example:
$n = 5;
$s = 'There were ' . $n . ' ducks.';
// $s is 'There were 5 ducks'

PHP has two operators that are specially designed for strings.

Operator Name Example Result


. Concatenation $txt1 . $txt2 Concatenation of $txt1 and $txt2
.= Concatenation assignment $txt1 .= $txt2 Appends $txt2 to $txt1

7. PHP 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.
 All the bitwise operators work on numbers as well as strings, but they vary in their treatment of string
operands of different lengths.

The bitwise operators are:


Bitwise negation (~)
 The bitwise negation operator 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.
 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 (&)


 The bitwise AND operator 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.
 For example, 0755 & 0671 is 0651. Octal 0755 is binary 111101101, and octal 0671 is binary
110111001.
111101101
& 110111001
---------------------
110101001
----------------------
The binary number 110101001 is octal 0651.

 Use the PHP functions bindec( ), decbin( ), octdec( ), and decoct( ) to convert numbers back and forth
to understand binary arithmetic.
 For example, "wolf" & "cat" is "cad".
Bitwise OR (|)
 The bitwise OR operator compares each corresponding bit in the binary representations of the operands.
 If both bits are 0, the resulting bit is 0; otherwise, the resulting bit is 1.
 For example, 0755 | 020 is 0775.
 For example, "pussy" | "cat" is "suwsy".

Bitwise XOR (^)


 The bitwise XOR operator compares each corresponding bit in the binary representation of the
operands.
 If either of the bits in the pair, but not both, is 1, the resulting bit is 1; otherwise, the resulting bit is 0.
 For example, 0755 ^ 023 is 776.
 For example, "big drink" ^ "AA" is "#(".

Left shift (<<)


 The left shift operator 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.
 For example, 3 << 1 (or binary 11 shifted one place left) results in 6(binary 110).

Right shift (>>)


 The right shift operator shifts the bits in the binary representation of the lefthand operand right by the
number of places given in the righthand operand.
 Both operands will be converted to integers if they aren't already.
 For example, 13 >> 1 (or binary 1101) shifted one place right results in 6 (binary 110).

8. PHP 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.

Operator Synonymous operators Changes type to


(int) (integer) Integer
(float) (real) Floating point
(string) String
(bool) (boolean) Boolean
(array) Array
(object) Object

 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
9. PHP Miscellaneous Operators
 The remaining PHP operators are for error suppression, executing an external command, and
selecting values:
i. Error suppression (@)
 Some operators or functions can generate error messages.
 The error suppression operator is used to prevent these messages from being created.
ii. 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;
iii. Conditional (?:)
 The conditional operator is, depending on the code you 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.
 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 :.
 For instance:
<a href="<?= $url ?>"><?= $linktext ? $linktext : $url ?></a>

You might also like