The previous Lesson of PHP Lessons Series was about PHP Data Types and in this lesson we will learn about PHP Operators.
The various operators used in PHP are as follows:
Arithmetic operators are the ones we use in simple maths like, Addition, Subtraction, Multiplication, Division and Modulus etc.
A few examples of the use of arithmetic operators are shown below:
If, $x and $y are two variables then arithmetic operations can be performed like this,
Addition Operation: $x + $y
Subtraction Operation: $x - $y
Multiplication Operation: $x * $y
Division Operation: $x / $y
Modulus Operation: $x % $y
Assignment Operators are used to assign values to variables. A few examples of assigning values to a variable are shown below:
If, $x and $y are two variables then assignment operations can be performed like this,
1). $x = $y, here the variable $x gets the same value as the variable $y.
2). $x = $x + $y, here the value of $y gets added to the value of $x. This operation can also be performed like this, $x += $y.
3). $x = $x - $y, here the value of $y gets subtracted from the value of $x. This operation can also be performed like this, $x -= $y.
4). $x = $x * $y, here the value of $x gets multiplied with the value of $y. This operation can also be performed like this, $x *= $y.
5). $x = $x / $y, here the value of $x gets divided with the value of $y. This operation can also be performed like this, $x /= $y.
6). $x = $x % $y, here the value of $x gets equal to its modulus with the value of $y. This operation can also be performed like this, $x %= $y.
Comparison Operators are used to compare a variable with another variable. A few examples of comparison operators are shown below:
If, $x and $y are two variables then comparisons operations can be performed like this,
1). $x == $y, this (==) operator checks if the value of $x is equal to $y.
2). $x === $y, this (===) operator checks if the value and type of $x are same as $y.
3). $x != $y, this (!=) operator checks if the value of $x is not equal to $y.
4). $x <> $y, this (<>) operator checks if the value of $x is not equal to $y same as (!=) operator.
5). $x !== $y, this (!==) operator checks if the value and type of $x are not same as $y.
6). $x < $y, this (<) operator checks if the value of $x is less than $y.
7). $x > $y, this (>) operator checks if the value of $x is greater than $y.
8). $x <= $y, this (<=) operator checks if the value of $x is less than or equal to $y.
9). $x >= $y, this (>=) operator checks if the value of $x is greater than or equal to $y.
Logical Operators are used to perform logical operations in PHP. The logical operators used in PHP are:
1. && operator: && is used to perform logical AND between two variables. Like for example,
If $x = 1 and $y = 0, and we perform logical AND operation on them i.e $x && $y then the value returned from the operation would be 0. Because in && operation the values of both $x and $y should be 1 in order to get the result 1, if any one of the two variables has the 0 then the return value will be 0.
2. || operator: || is used to perform logical OR between two variables. Like for example,
If $x = 1 and $y = 0, and we perform logical OR operation on them i.e $x || $y then the value returned from the operation would be 1. Because in || operation if any one of the two variables has value 1, then the return value of the operation will be 1.
3. ! operator: ! operator is basically used to invert the output of the variable. Like for example,
If the value of $x = 1, and you perform ! operation on it then the return value of the operation will become 0 and vice versa.
The various operators used in PHP are as follows:
Arithmetic Operators:
Arithmetic operators are the ones we use in simple maths like, Addition, Subtraction, Multiplication, Division and Modulus etc.
A few examples of the use of arithmetic operators are shown below:
If, $x and $y are two variables then arithmetic operations can be performed like this,
Addition Operation: $x + $y
Subtraction Operation: $x - $y
Multiplication Operation: $x * $y
Division Operation: $x / $y
Modulus Operation: $x % $y
Assignment Operators:
Assignment Operators are used to assign values to variables. A few examples of assigning values to a variable are shown below:
If, $x and $y are two variables then assignment operations can be performed like this,
1). $x = $y, here the variable $x gets the same value as the variable $y.
2). $x = $x + $y, here the value of $y gets added to the value of $x. This operation can also be performed like this, $x += $y.
3). $x = $x - $y, here the value of $y gets subtracted from the value of $x. This operation can also be performed like this, $x -= $y.
4). $x = $x * $y, here the value of $x gets multiplied with the value of $y. This operation can also be performed like this, $x *= $y.
5). $x = $x / $y, here the value of $x gets divided with the value of $y. This operation can also be performed like this, $x /= $y.
6). $x = $x % $y, here the value of $x gets equal to its modulus with the value of $y. This operation can also be performed like this, $x %= $y.
Comparison Operators:
Comparison Operators are used to compare a variable with another variable. A few examples of comparison operators are shown below:
If, $x and $y are two variables then comparisons operations can be performed like this,
1). $x == $y, this (==) operator checks if the value of $x is equal to $y.
2). $x === $y, this (===) operator checks if the value and type of $x are same as $y.
3). $x != $y, this (!=) operator checks if the value of $x is not equal to $y.
4). $x <> $y, this (<>) operator checks if the value of $x is not equal to $y same as (!=) operator.
5). $x !== $y, this (!==) operator checks if the value and type of $x are not same as $y.
6). $x < $y, this (<) operator checks if the value of $x is less than $y.
7). $x > $y, this (>) operator checks if the value of $x is greater than $y.
8). $x <= $y, this (<=) operator checks if the value of $x is less than or equal to $y.
9). $x >= $y, this (>=) operator checks if the value of $x is greater than or equal to $y.
Logical Operators:
Logical Operators are used to perform logical operations in PHP. The logical operators used in PHP are:
1. && operator: && is used to perform logical AND between two variables. Like for example,
If $x = 1 and $y = 0, and we perform logical AND operation on them i.e $x && $y then the value returned from the operation would be 0. Because in && operation the values of both $x and $y should be 1 in order to get the result 1, if any one of the two variables has the 0 then the return value will be 0.
2. || operator: || is used to perform logical OR between two variables. Like for example,
If $x = 1 and $y = 0, and we perform logical OR operation on them i.e $x || $y then the value returned from the operation would be 1. Because in || operation if any one of the two variables has value 1, then the return value of the operation will be 1.
3. ! operator: ! operator is basically used to invert the output of the variable. Like for example,
If the value of $x = 1, and you perform ! operation on it then the return value of the operation will become 0 and vice versa.
No comments:
Post a Comment