Operator in php- Like any other programming language operator play a very important role in php. Operator is a symbol as like +, -, *, / , etc. Operators are used to perform operations on variables and values.
Php support a rich set of built in operator we have already used several of them such as equal to add(+)Minus(-) multiplication(*) and division(/) operator is a symbol that tells the computer to perform certain mathematics or logical manipulation. operators are used in program to manipulate data and variables. they usually form a part of mathematical or logical expression.
Types of operator in php- In the php has many operator these are the
- Arithmetic Operators
- Comparison Operators
- Assignment Operators
- Logical Operators
- Increment / Decrements Operators
- Array Operators
- String Operators
Arithmetic Operators -: The php Arithmetic Operators use the numeric value as like add(+)Minus(-) multiplication(*) and division(/) etc.
For example of adding two number..
<!DOCTYPE html> <html> <body> <?php $a = 14; $b = 7; echo $a + $b; ?> </body> </html>
Output of this program
21
Like this way we use all numeric operator in php
<!DOCTYPE html> <html> <body> <?php $a = 14; $b = 7; echo $a - $b; echo "<br>"; echo $a * $b; echo "<br>"; echo $a / $b; echo "<br>"; echo $a % $b; ?> </body> </html>
Output of this program
7 98 2 0
Comparison operator in php-: Comparison operator are use to compare two values as like equal (=), not equal (!=) etc. For example equal (=)
<!DOCTYPE html> <html> <body> <?php $a = 7; $b = "7"; var_dump($a == $b); ?> </body> </html>
Output of this program
bool(true)
Another example of comparison operator
<!DOCTYPE html> <html> <body> <?php $a = 7; $b = "7"; var_dump($a === $b); //identical echo "<br>"; var_dump($a != $b); //not equal echo "<br>"; var_dump($a <> $b); //not equal echo "<br>"; var_dump($a !== $b); //not identical echo "<br>"; var_dump($a > $b); //grater then echo "<br>"; var_dump($a < $b); //less than echo "<br>"; var_dump($a >= $b); //grater than equal to echo "<br>"; var_dump($a <= $b); //less than equal to echo "<br>"; ?> </body> </html>
Output of this program
bool(false) bool(false) bool(false) bool(true) bool(false) bool(false) bool(true) bool(true)
Assignment operator in php-: This operator used to assign variable value of numeric. The basic assignment operator is ( “= ” ). It means the left side value is equal to right side for example if we write (a = b). Then the value of a and b are the same. php use this operator for many way like
<!DOCTYPE html> <html> <body> <?php $a = 7; echo $a; echo "<br>"; $b=10; $b +=20; // for adding echo $b; echo "<br>"; $c=10; $c -=20; // for subtract echo $c; echo "<br>"; $d=10; $e = 20; echo $d * $e ; // for multiple echo "<br>"; $f=20; $f /= 4; // for division echo $f; echo "<br>"; $g =20; $g %= 6; echo $g ; echo "<br>"; ?> </body> </html>
Output of this program
7 30 -10 200 5 2
Logical operator in php-: These operator are use combine condition statement. These are the and,or,not etc. types. A list has been given here these operators
- And logical operator(&&) -: when the both condition are same
- Or logical operator(!!)-: One condition is true and another is false
- not logical operator (!)-: either both are same or false
A complete example of logical operator are given here..
<!DOCTYPE html> <html> <body> <?php $a = 7; $b = 10; if(&a == 7 && $b == 10) { echo "hello netnic" ; } echo "<br>"; $c=7 ; $d=8 ; if ($c==7 || $d ==6) { echo "hello netnic"; } echo "<br>"; $e = 20; if($e !== 10) { echo "hello netnic php"; } ?> </body> </html>
Output of this program
hello netnic hello netnic hello netnic php
Increment and decrements operator in php-: This operator are use the variable value.These are the increment (i++) and decrements (i–) operator of any variable. These are use in four way as like
- ++$a -: Pre increment. It means it increase the one value in “a” and return the value of “a”
- $a++ -: post increment. It means it return the value in “a” and increase in value one.
- –%a -: Pre decrements. it means in decrease the one valuie in “a” and return the value of”a”.
- $a– post decrements. It means it return the value and decrease the value of “a”
A complete example of increment and decrements operator
<!DOCTYPE html> <html> <body> <?php $a = 10; echo ++$a; echo $a; echo "<br>"; $b = 10; echo $b++; echo $b; echo "<br>"; $c = 10; echo --$c; echo $c; echo "<br>"; $d = 10; echo $d--; echo $d; echo "<br>"; ?> </body> </html>
Output of this program
1111 1011 99 109
Array operator in php-:This type operator are used to compare array.These are the
- union(+) a+b;
- equality (++) like a==b;
- identity (===) like a===b;
- Inequality(!=)
- non identity (!==)
- inequality (<>)
String operator in php-: There are two type string operator are use in php. these are the (.) concatenation and (.=) concatenation assignment operators.For example of these operator
<!DOCTYPE html> <html> <body> <?php $netnic1 = "Hello"; // example of concatenation $netnic2 = " netnic"; echo $netnic1 . $netnic2; echo "<br>"; $netnic1 = "Hello"; // example of concatenation assignment $netnic2 = " netnic"; echo $netnic1 .= $netnic2; echo $netnic1; ?> </body> </html>
Output of this program
Hello netnic Hello netnicHello netnic