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

PHP Session 05 Document

The document discusses various PHP operators categorized into arithmetic, assignment, bitwise, comparison, increment/decrement, logical, string, array, and type operators. It provides examples and explanations of common operators like addition, assignment, AND, equality, increment, concatenation, and instanceof.

Uploaded by

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

PHP Session 05 Document

The document discusses various PHP operators categorized into arithmetic, assignment, bitwise, comparison, increment/decrement, logical, string, array, and type operators. It provides examples and explanations of common operators like addition, assignment, AND, equality, increment, concatenation, and instanceof.

Uploaded by

Arun Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

PHP Session 05 Document

Page 1 of 14
Topics Covered

 PHP Operators 03 To 12
 PHP Comments 12 To 13

Page 2 of 14
PHP Operators

PHP Operator is a symbol i.e used to perform operations on operands. In simple words,
operators are used to perform operations on variables or values. For example:

1. $num=10+20;//+ is the operator and 10,20 are operands  

In the above example, + is the binary + operator, 10 and 20 are operands and $num is variable.

PHP Operators can be categorized in following forms:

o Arithmetic Operators
o Assignment Operators
o Bitwise Operators
o Comparison Operators
o Incrementing/Decrementing Operators
o Logical Operators
o String Operators
o Array Operators
o Type Operators
o Execution Operators
o Error Control Operators

We can also categorize operators on behalf of operands. They can be categorized in 3 forms:

o Unary Operators: works on single operands such as ++, -- etc.


o Binary Operators: works on two operands such as binary +, -, *, / etc.
o Ternary Operators: works on three operands such as "?:".

Arithmetic Operators

The PHP arithmetic operators are used to perform common arithmetic operations such as
addition, subtraction, etc. with numeric values.

Operato Name Example Explanation

Page 3 of 14
r

+ Addition $a + $b Sum of operands

- Subtraction $a - $b Difference of operands

* Multiplication $a * $b Product of operands

/ Division $a / $b Quotient of operands

% Modulus $a % $b Remainder of operands

** Exponentiatio $a ** $b $a raised to the power $b


n

The exponentiation (**) operator has been introduced in PHP 5.6.

Assignment Operators

The assignment operators are used to assign value to different variables. The basic assignment
operator is "=".

Operato Name Example Explanation


r

= Assign $a = $b The value of right operand is assigned to


the left operand.

+= Add then Assign $a += Addition same as $a = $a + $b


$b

-= Subtract then $a -= $b Subtraction same as $a = $a - $b

Page 4 of 14
Assign

*= Multiply then $a *= $b Multiplication same as $a = $a * $b


Assign

/= Divide then $a /= Find quotient same as $a = $a / $b


Assign $b
(quotient)

%= Divide then $a %= Find remainder same as $a = $a % $b


Assign $b
(remainder)

Bitwise Operators

The bitwise operators are used to perform bit-level operations on operands. These operators
allow the evaluation and manipulation of specific bits within the integer.

Operato Name Example Explanation


r

& And $a & $b Bits that are 1 in both $a and $b are set to 1,
otherwise 0.

| Or (Inclusive $a | $b Bits that are 1 in either $a or $b are set to 1


or)

^ Xor (Exclusive $a ^ $b Bits that are 1 in either $a or $b are set to 0.


or)

~ Not ~$a Bits that are 1 set to 0 and bits that are 0 are
set to 1

Page 5 of 14
<< Shift left $a << Left shift the bits of operand $a $b steps
$b

>> Shift right $a >> Right shift the bits of $a operand by $b


$b number of places

Comparison Operators

Comparison operators allow comparing two values, such as number or string. Below the list of
comparison operators are given:

Operato Name Example Explanation


r

== Equal $a == $b Return TRUE if $a is equal to $b

=== Identical $a === Return TRUE if $a is equal to $b, and they


$b are of same data type

!== Not identical $a !== Return TRUE if $a is not equal to $b, and
$b they are not of same data type

!= Not equal $a != $b Return TRUE if $a is not equal to $b

<> Not equal $a <> $b Return TRUE if $a is not equal to $b

< Less than $a < $b Return TRUE if $a is less than $b

> Greater than $a > $b Return TRUE if $a is greater than $b

<= Less than or $a <= $b Return TRUE if $a is less than or equal $b


equal to

Page 6 of 14
>= Greater than or $a >= $b Return TRUE if $a is greater than or equal
equal to $b

<=> Spaceship $a Return -1 if $a is less than $b


<=>$b Return 0 if $a is equal $b
Return 1 if $a is greater than $b

Incrementing/Decrementing Operators

The increment and decrement operators are used to increase and decrease the value of a
variable.

Operato Name Example Explanation


r

++ Increment ++$a Increment the value of $a by one, then return $a

$a++ Return $a, then increment the value of $a by one

-- decrement --$a Decrement the value of $a by one, then return $a

$a-- Return $a, then decrement the value of $a by one

Logical Operators

The logical operators are used to perform bit-level operations on operands. These operators
allow the evaluation and manipulation of specific bits within the integer.

Operato Name Example Explanation


r

and And $a and $b Return TRUE if both $a and $b are true

Page 7 of 14
Or Or $a or $b Return TRUE if either $a or $b is true

xor Xor $a xor $b Return TRUE if either $ or $b is true but not both

! Not ! $a Return TRUE if $a is not true

&& And $a && $b Return TRUE if either $a and $b are true

|| Or $a || $b Return TRUE if either $a or $b is true

String Operators

The string operators are used to perform the operation on strings. There are two string
operators in PHP, which are given below:

Operato Name Example Explanation


r

. Concatenation $a . $b Concatenate both $a and $b

.= Concatenation and $a .= First concatenate $a and $b, then assign


Assignment $b the concatenated string to $a, e.g. $a =
$a . $b

Array Operators

The array operators are used in case of array. Basically, these operators are used to compare the
values of arrays.

Operator Name Example Explanation

+ Union $a + $y Union of $a and $b

Page 8 of 14
== Equality $a == $b Return TRUE if $a and $b have same key/value
pair

!= Inequality $a != $b Return TRUE if $a is not equal to $b

=== Identity $a === Return TRUE if $a and $b have same key/value


$b pair of same type in same order

!== Non- $a !== Return TRUE if $a is not identical to $b


Identity $b

<> Inequality $a <> $b Return TRUE if $a is not equal to $b

Type Operators

The type operator instanceof is used to determine whether an object, its parent and its derived
class are the same type or not. Basically, this operator determines which certain class the object
belongs to. It is used in object-oriented programming.

1. <?php  
2.     //class declaration  
3.     class Developer  
4.     {}  
5.     class Programmer  
6.     {}  
7.     //creating an object of type Developer  
8.     $charu = new Developer();  
9.       
10.     //testing the type of object  
11.     if( $charu instanceof Developer)  
12.     {  
13.         echo "Charu is a developer.";  
14.     }  
15.     else  
16.     {     
17.         echo "Charu is a programmer.";  
18.     }  

Page 9 of 14
19.     echo "</br>";  
20.     var_dump($charu instanceof Developer);           //It will return true.  
21.     var_dump($charu instanceof Programmer);       //It will return false.  
22. ?>  

Output:

Charu is a developer.
bool(true) bool(false)

Execution Operators

PHP has an execution operator backticks (``). PHP executes the content of backticks as a shell
command. Execution operator and shell_exec() give the same result.

Operato Name Example Explanation


r

`` backticks echo Execute the shell command and return the result.
`dir`; Here, it will show the directories available in
current folder.

Note: Note that backticks (``) are not single-quotes.

Error Control Operators

PHP has one error control operator, i.e., at (@) symbol. Whenever it is used with an expression,
any error message will be ignored that might be generated by that expression.

Operato Nam Example Explanation


r e

@ at @file ('non_existent_file') Intentional file error

Page 10 of 14
PHP Operators Precedence

Let's see the precedence of PHP operators with associativity.

Operators Additional Information Associativity

clone new clone and new non-


associative

[ array() left

** Arithmetic right

++ -- ~ (int) (float) (string) (array) increment/decrement and right


(object) (bool) @ types

instanceof types non-


associative

! logical (negation) right

*/% arithmetic left

+-. arithmetic and string left


concatenation

<< >> bitwise (shift) left

< <= > >= comparison non-


associative

== != === !== <> comparison non-


associative

Page 11 of 14
& bitwise AND left

^ bitwise XOR left

| bitwise OR left

&& logical AND left

|| logical OR left

?: ternary left

= += -= *= **= /= .= %= &= |= ^= assignment right


<<= >>= =>

and logical left

xor logical left

or logical left

, many uses (comma) left

PHP Comments

PHP comments can be used to describe any line of code so that other developer can understand
the code easily. It can also be used to hide any code.

PHP supports single line and multi line comments. These comments are similar to C/C++ and
Perl style (Unix shell style) comments.

Page 12 of 14
PHP Single Line Comments

There are two ways to use single line comments in PHP.

o // (C++ style single line comment)


o # (Unix Shell style single line comment)

1. <?php  
2. // this is C++ style single line comment  
3. # this is Unix Shell style single line comment  
4. echo "Welcome to PHP single line comments";  
5. ?>  

Output:

Welcome to PHP single line comments

PHP Multi Line Comments

In PHP, we can comments multiple lines also. To do so, we need to enclose all lines within /*
*/. Let's see a simple example of PHP multiple line comment.

1. <?php  
2. /* 
3. Anything placed 
4. within comment 
5. will not be displayed 
6. on the browser; 
7. */  
8. echo "Welcome to PHP multi line comment";  
9. ?>  

Output:

Welcome to PHP multi line comment

Page 13 of 14
Session V

1) Explain about PHP Operators?


2) What is an PHP Arithmetic Operators?
3) What is an PHP Assignment Operators?
4) What is an PHP Bitwise Operators?
5) What is an PHP Comparison Operators?
6) What is an PHP Increment & Decrement Operators?
7) What is an PHP Logical Operators?
8) What is an PHP String Operators?
9) What is an PHP Array Operators?
10) What is an PHP Type Operators?
11) What is an PHP Execution Operators?
12) What is an PHP Error control Operators?
13) Explain about PHP Comments?
14) What is PHP Single line Comment?
15) What is PHP Multi line Comment?

Page 14 of 14

You might also like