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.
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.
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.*/
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.
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