Saturday, January 5, 2013

Operators in PHP


Introduction
Operators are a special type of symbols, which are used for calculation and comparison in a programming language. Operators are also used to operate on values. There are six types operators in PHP, which are as follows:
  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • Increment/Decrement Operators
  • Conditional Operators
Arithmetic Operator
These operators are used for calculation like add, subtraction, multiplication, division and modulation.
  • Plus Operator (+) :- This operator is used for add two or more numbers. This is known as addition operator.
  • Minus Operator (-) :- This operator is used for subtract two more numbers. This operator is known as subtraction operator.
  • Multiplication Operator (*) :- This operator is used for find the product of two or more numbers.
  • Division Operator (/) :- This operator is used for find the quotient of two numbers.
  • Modulus Operator (%) :- This operator is used to find the quotient and remainder of two numbers.
Examples of arithmetic operators are as follows:
$x=5;
$y=2;
$addition=$x+$y;
//add two number$subtraction=$x-$y; //subtract two number$multiplication=$x*$y; //multiply two number$division=$x/$y; //divide two numbers$modulus=$x%$y; //modulus two numbersecho "The addition is :" .$addition.'
'
;echo "The subtraction is :" .$subtraction.'
'
;echo "The multiplication is :".$multiplication.'
'
;echo "The division is :".$division.'
'
;echo "The modulus is :".$modulus.'
'
;?>


Assignment Operators
The assignment operators are used to set a variable equal to a value or set a variable to another variable's value. This operator is denoted by the (equal) "=" sign. There are many types of assignment operators in PHP, which are as follows.
  • = :- This operator is used for assign the value to the variable. e.g. x=5.
  • += :- This operator is used for assign the value and add the value to the variable. e.g. x+=5 i.e. x=x+5.
  • -= :- This operator is used for assign the value and subtract the value to the variable. e.g. x-=5 i.e. x=x-5.
  • *= :- This operator is used for assign the value and multiply the value to the variable. e.g. x*=5 i.e. x=x*5.
  • /= :- This operator is used for assign the value to the variable and find the quotient. e.g. x/=5 i.e. x=x/5.
  • %= :- This operator is used for assign the value to the variable and find the modulation i.e. remainder and quotient. e.g. x%=5 i.e. x=x%5.
  • .= :- This operator is used for assign the value to the variable and add the new value in the variable.
Examples of assignment operators are as follows:
$x=5;
$x+=5;
//x=x+5 i.e. x=5+5=10echo "The value of x is :".$x.'
'
;
$y=8;
$y-=5;
//y=y-5 i.e. y=8-5=3echo "The value of y is :".$y.'
'
;
$z=2;
$z*=3;
//z=z*3 i.e. z=2*3=6echo "The value of z is :".$z.'
'
;
$p=7;
$p/=2;
// p=p/2 i.e. p=7/2=3.5echo "The value of p is :".$p.'
'
;
$q=8;
$q%=3;
//q=q%3 i.e. q=8%3=2 (remainder)echo "The value of q is :".$q.'
'
;?>



Decision Statement (if...else)
Before discussing comparison operators I will discuss the decision statement. A decision statement is represented by if...else. A decision statement has two parts i.e. one is if and second is else. If is the part of a decision statement where we supply a condition and it has a block of commands that execute when the condition is true.
The second part of the decision statement is else which has commands and executes when the condition is false.

Syntax of if...else
  if (expression)
   {
       true;
   }
  else
   {
      false;
    }

Comparison Operators
Comparison operators are used to check the relationship between variables and values. There are many types of comparison operators in PHP, which are as follows:
  • == :- This operator is known as equal to operator. e.g. 5==5 i.e. return true.
  • != :- This operator is known as not equal to operator. e.g. 5!=5 return false.
  • < :- This operator is known as less than operator. e.g. 5<5 false.="false." font="font" return="return">
  • > :- This operator is known as greater than operator. e.g. 5>5 return false.
  • <= :- This operator is known as less than equal to operator. e.g. 5<=2 return false.
  • >= :-  This operator is known as greater than equal to operator. e.g. 5>=2 return true.
Examples of comparison operators are as follows:
$x=5;
$y=2;
if($x==$y)
{
echo "x is equal to y i.e. True".'
'
;
}
else{echo "x is not equal to y i.e. False".'
'
;
}
if($x<$y)
{
echo "x is less than y i.e. True".'
'
;
}
else{echo "x is not less than y i.e. False".'
'
;
}
echo "Same as you can use other comparison operator ";?>



Logical Operators
Logical operators are used to convert the operands to Boolean values and then perform a respective comparison. There are many types of logical operators in PHP, which are as follows:
  • && :- This operator is known as logical and operator.
  • || : This operator is known as logical or operator.
  • ! :- This operator is known as logical not operator.
Examples of logical operators are as follows:

$x=5;
$y=5;
if($x&&$y) //logical  and operator{echo "True".'
'
;
}
else{echo "False".'
'
;
}
if($x||$y) //logical or operator{echo "True".'
'
;
}
else{echo "False".'
'
;
}
if($x!=$y) //logical not operator{echo "True".'
'
;
}
else{echo "False".'
'
;
}
?>



Increment/Decrement Operators
Increment/Decrement is a special type of operator. The Increment operator increases the value of an operand by 1. The Decrement operator decreases the value of an operand by 1. There are two types of increment/decrement operators which are as follows:
  • Prefix Increment/Decrement operator
  • Postfix Increment/Decrement operator
Examples of Increment/Decrement operators are as follows:
$x=5;
$y=$x++;
// postfix increment operator$z=++$x; // prefix increment operator$a=$x--; // postfix decrement operator$b=--$x; // prefix decrement operatorecho "The value of x in postfix increment operator is : " .$y.'
'
;echo "The value of x in prefix increment operator is :".$z.'
'
;echo "The value of x in postfix decrement operator is :".$a.'
'
;echo "The value of x in prefix decrement operator is :" .$b.'
'
;?>



Conditional Operators
The Conditional operator is a special type of operator which is conditional. There are two expressions in a conditional operator. If condition one is true then return expression 1 otherwise return expression 2.
Syntax of Conditional Operator
condition ? exp1 : exp 2 ;
Examples of the Conditional operator are as follows:
$x=5;
$y=6;
$z=($x>$y) ?
'True' : 'False' ;echo $z;?>









No comments:

Post a Comment