PHP Session 05 Document
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.
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:
Arithmetic Operators
The PHP arithmetic operators are used to perform common arithmetic operations such as
addition, subtraction, etc. with numeric values.
Page 3 of 14
r
Assignment Operators
The assignment operators are used to assign value to different variables. The basic assignment
operator is "=".
Page 4 of 14
Assign
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.
& And $a & $b Bits that are 1 in both $a and $b are set to 1,
otherwise 0.
~ 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
Comparison Operators
Comparison operators allow comparing two values, such as number or string. Below the list of
comparison operators are given:
!== Not identical $a !== Return TRUE if $a is not equal to $b, and
$b they are not of same data type
Page 6 of 14
>= Greater than or $a >= $b Return TRUE if $a is greater than or equal
equal to $b
Incrementing/Decrementing Operators
The increment and decrement operators are used to increase and decrease the value of a
variable.
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.
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
String Operators
The string operators are used to perform the operation on strings. There are two string
operators in PHP, which are given below:
Array Operators
The array operators are used in case of array. Basically, these operators are used to compare the
values of arrays.
Page 8 of 14
== Equality $a == $b Return TRUE if $a and $b have same key/value
pair
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.
`` backticks echo Execute the shell command and return the result.
`dir`; Here, it will show the directories available in
current folder.
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.
Page 10 of 14
PHP Operators Precedence
[ array() left
** Arithmetic right
Page 11 of 14
& bitwise AND left
| bitwise OR left
|| logical OR left
?: ternary left
or logical 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
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:
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:
Page 13 of 14
Session V
Page 14 of 14