Category Archives: Operators in php

PHP Arithmatic Operator

Arithmetic Operators
Example Name Result
-$a Negation Opposite of $a.
$a + $b Addition Sum of $a and $b.
$a – $b Subtraction Difference of $a and $b.
$a * $b Multiplication Product of $a and $b.
$a / $b Division Quotient of $a and $b.
$a % $b Modulus Remainder of $a divided by $b.

PHP-Logical Operator

Logical Operators
Example Name Result
$a and $b And TRUE if both $a and $b are TRUE.
$a or $b Or TRUE if either $a or $b is TRUE.
$a xor $b Xor TRUE if either $a or $b is TRUE, but not both.
! $a Not TRUE if $a is not TRUE.
$a && $b And TRUE if both $a and $b are TRUE.
$a || $b Or TRUE if either $a or $b is TRUE.`

Check out this example

<?php
$a=TRUE; //1
$b=FALSE;//null 0
Echo “A has:”.(bool)$a;
echo”B has:”.(bool)$b;
echo “A AND B has: “;
echo $a && $b;
echo “A OR B has: “;
echo $a || $b;
?>

Increment/Decrement operator in php

This may seem a bit confusing, but there is even a shorter shorthand for the common task of adding 1 or subtracting 1 from a variable. To add one to a variable or “increment” use the “++” operator:

* $x++; Which is equivalent to $x += 1; or $x = $x + 1;

To subtract 1 from a variable, or “decrement” use the “–” operator:

* $x–; Which is equivalent to $x -= 1; or $x = $x – 1;

Study this example:

<?php
$a = 5;
echo “<h2>A initially is “.$a.”.</h2>”;
echo “<h3>Postincrement</h3>”;
echo “Should be 5: ” . $a++ . “<br />
“;
echo “Should be 6: ” . $a . “<br />
“;
echo “<h3>Preincrement</h3>”;
$a = 5;
echo “A is reset to: “.$a. “<br />
“;
echo “Should be 6: ” . ++$a . “<br />
“;
echo “Should be 6: ” . $a . “<br />
“;
echo “<h3>Postdecrement</h3>”;
$a = 5;
echo “A is reset to: “.$a. “<br />
“;
echo “Should be 5: ” . $a– . “<br />
“;
echo “Should be 4: ” . $a . “<br />
“;
echo “<h3>Predecrement</h3>”;
$a = 5;
echo “A is reset to: “.$a. “<br />
“;
echo “Should be 4: ” . –$a . “<br />
“;
echo “Should be 4: ” . $a . “<br />
“;
?>

php-comparision operator

Comparisons are used to check the relationship between variables and/or values. Comparison operators are used inside conditional statements and evaluate to either true or false. Here are the most important comparison operators of PHP.

Operator Desc Example Result
== Equal To $x == $y false
!= Not Equal To $x != $y true
< Less Than $x < $y true
> Greater Than $x > $y false
<= Less Than or Equal To $x <= $y true
>= Greater Than or Equal To $x >= $y false

Check out this example:

<?php
$a=10;
$b=17;
/*
$a=17;
$b=10;
*/
echo  “<br>A is : “.$a;
echo  “<br>B is : “.$b;
echo “<br><br>”;
if($a==$b)
{
echo”<br>A equals B.”;//it wont be true
}
if($a != $b){
echo  “<br>A is not equals to B.”;//it will be true
}
if($a < $b){
echo  “<br>A is less than B;”;
}
if($a > $b){
echo  “<br>A is greater than B;”;
}
if($a <= $b){
echo  “<br>A is less than or equals to B;”;
}
if($a >= $b){
echo “<br>A is greater than or equals to B;”;
}
?>

php – operators

An operator is something that you feed with one or more values (or expressions, in programming jargon) which yields another value (so that the construction itself becomes an expression).Operators are used to manipulate or perform operations on variables and values.

There are many operators used in PHP.

1. Comparison Operator

2. Increment/Decrement Operator

3. Logical Operator

4. Arithmatic Operator