PHP
In this tutorial we will learn about the different types of operators used in PHP.
These operators are addition, subtraction, multiplication, division and modulus.
To assign value to any variable we use the assignment operator.
$name = "Yusuf Shakeel"; //we have assigned a string value to a variable.
We can also combine assignment operator with other operators like +, - etc. to shorten certain expression.
In the following expression we are assigning 10 to variable x and 20 to variable y. We are then adding x and y and saving the final result in variable x.
$x = 10; $y = 20; $x = $x + $y;
The above addition expression can be simplified in the following manner.
$x = 10; $y = 20; $x += $y; //this means $x = $x + $y
Following are combined assignment operators.
Bitwise operators are used to work with individual bits of integer values. Following are the list of bitwise operators.
Comparison operators are used to compare the two operands and the result is in boolean form i.e, either true or false.
Logical operators are used to work with Boolean values i.e., true or false. Let us first see the boolean values assigned by PHP to some expressions.
Following have boolean value false.
false
All other values evaluate to true.
true
Increment operator is used to increase value of a variable by 1. And decrement operator is used to decrease the value by 1.
++ is the Increment Operator and -- is the Decrement Operator.
$x = 10; //x set to 10 echo $x; //output 10 //now increase the value of x by 1 $x = $x + 1; echo $x //output 11
Now using the increment operator.
$x = 10; //x set to 10 echo $x; //output 10 //now increase the value of x by 1 $x++; echo $x //output 11
Decrement operation
$x = 10; //x set to 10 echo $x; //output 10 //now decrease the value of x by 1 $x = $x - 1; echo $x //output 9
Now using the decrement operator.
$x = 10; //x set to 10 echo $x; //output 10 //now decrease the value of x by 1 $x--; echo $x //output 9
Important points to note when using the increment and decrement operators in expression.
++$x; // adds one to $x and then returns the result $x++; // returns value of $x and then adds one to $x –-$x; // subtracts one from $x and then returns the result $x–-; // returns value of $x and then subtracts one from $x
Lets check some examples.
Pre-increment (++ before the variable)
//set x to 10 $x = 10; echo $x; //output 10 //now assign value of x to y using pre-increment //this is first increment x by 1 and then assign the new value to y $y = ++$x; echo $y; //output 11 echo $x; //output 11
Post-increment (++ after the variable)
//set x to 10 $x = 10; echo $x; //output 10 //now assign value of x to y using post-increment //first use value of x then increment by 1 $y = $x++; echo $y; //output 10 echo $x; //output 11
Pre-decrement (-- before the variable)
//set x to 10 $x = 10; echo $x; //output 10 //now assign value of x to y using pre-decrement //this will first decrease value of x by 1 then assign to y $y = --$x; echo $y; //output 9 echo $x; //output 9
Post-decrement (-- after the variable)
//set x to 10 $x = 10; echo $x; //output 10 //now assign value of x to y using post-decrement //this will first use the value of x then decrease by 1 $y = $x--; echo $y; //output 10 echo $x; //output 9