Friday, 11 December 2015

PHP OPERATORS

Php operators
   Variables are simply containers for information. In order to do anything useful with them, you need Operators .Operators are symbols that tell the PHP processor to perform certain actions.
For example, the addition(+) symbol is an Operators 
that tells PHP to 
add two variables or values, while the greater-than(>) symbol
is an 
Operators that tells PHP to compare two values.

 Type of operators  
There are four type of operators:-
Arithmetic Operators
Assignment Operators
Comparison Operators
Logical Operators

Arithmetic operators in PHP
<?php
$x=10;  $y=5;
//addition
$sum=$x+$y;
echo "sum=".$sum."<br/>";
?>
Output : sum = 15

Assignment operators in PHP
 <?php
$x = 500;
$x+= 500; // $x=$x+$x;
echo "sum=".$x."<br/>";
?>
Output : sum=1000

PHP comparative operators
 <?php
$x=10;
$y=10.0;
echo ($x==$y);
//it returns true because both the variable contains same value.
echo ($x===$y);
/*it returns false because === strongly compares. here both variable contain same value i.e 10 but different data
type one is integer and another is float.*/
?>



Figure

PHP logical operators
 <?php
$name="alex";
$pass="alex123";
if($name=="alex" && $pass=="alex123")
{
  header('location:one.php');
}
else
{
         echo "Invalid name or password";
}
?>







Operator Precedence in PHP
You would have probably learned about BOD-MAS, a mnemonic that specifies the order in which a calculator or a computer performs a sequence of mathematical operations.
Brackets , order , Division , Multiplication , Addition , and Subtraction.
PHP’s precedence rules are tough to remember.
Parentheses always have the highest precedence, so wrapping an expression in these will force PHP to evaluate it first, when using multiple sets of parentheses.
<?php
echo (((4*8)-2)/10);
echo (4*8-2/10);
?>
Output : with parentheses :3
  without parentheses :31.8 




Related Posts:

  • PHP CONSTANTS Constant in PHP —Constants are PHP container that remain constant and never change —Constants are used for data that is unchanged at mult… Read More
  • $var & $$var and php super global variables Difference Between $var and $$var in Php —$$var uses the value of the variable whose name is the value of $var. —It means $$var is known … Read More
  • PHP echo & Print, PHP Comments Difference between echo and print in PHP   —PHP echo and print both are PHP Statement.    Both are used to d… Read More
  • Magic Constants Figure 1 1 .__LINE__         The current line number of the file.               &… Read More
  • PHP data types PHP data types   Data types specify the size and type of values that can be stored. —Variable does not need to be declared ITS DATA… Read More