Monday, July 31, 2017

PHP Operators


Operators are used to perform operations on variables and values.

PHP divides the operators in the following groups:

  1. Arithmetic operators
  2. Assignment operators
  3. Comparison operators
  4. Increment/Decrement operators
  5. Logical operators
  6. String operators
  7. Array operators
PHP Arithmetic Operators :
The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc.

ExampleNameResult
+$aIdentityConversion of $a to int or float as appropriate.
-$aNegationOpposite of $a.
$a + $bAdditionSum of $a and $b.
$a - $bSubtractionDifference of $a and $b.
$a * $bMultiplicationProduct of $a and $b.
$a / $bDivisionQuotient of $a and $b.
$a % $bModuloRemainder of $a divided by $b.
$a ** $bExponentiationResult of raising $a to the $b'th power. Introduced in PHP 5.6.

Example :1 
<?php

echo (5 % 3)."\n";           // prints 2
echo (5 % -3)."\n";          // prints 2
echo (-5 % 3)."\n";          // prints -2
echo (-5 % -3)."\n";         // prints -2

?>

Example : 2
<?php
  if (($a % 2) == 1)
  { echo "$a is odd." ;}
  if (($a % 2) == 0)
  { echo "$a is even." ;}
?>


PHP Assignment Operators :
The PHP assignment operators are used with numeric values to write a value to a variable.
ExampleNameResult
a = bThe left operand gets set to the value of the expression on the righta = b
a += bAdditiona = a + b
a -= bSubtractiona = a - b
a *= bMultiplicationa = a * b
a /= bDivisiona = a / b
a %= bModulusa = a % b

Example :1
<?php

$a = ($b = 4) + 5; // $a is equal to 9 now, and $b has been set to 4.

?>

Example : 2
<?php

$a = 3;
$a += 5; // sets $a to 8, as if we had said: $a = $a + 5;
$b = "Hello ";
$b .= "There!"; // sets $b to "Hello There!", just like $b = $b . "There!";

?>


PHP Comparison Operators:
The PHP comparison operators are used to compare two values (number or string):
ExampleNameResult
$a == $bEqualTRUE if $a is equal to $b after type juggling.
$a === $bIdenticalTRUE if $a is equal to $b, and they are of the same type.
$a != $bNot equalTRUE if $a is not equal to $b after type juggling.
$a <> $bNot equalTRUE if $a is not equal to $b after type juggling.
$a !== $bNot identicalTRUE if $a is not equal to $b, or they are not of the same type.
$a < $bLess thanTRUE if $a is strictly less than $b.
$a > $bGreater thanTRUE if $a is strictly greater than $b.
$a <= $bLess than or equal toTRUE if $a is less than or equal to $b.
$a >= $bGreater than or equal toTRUE if $a is greater than or equal to $b.
$a <=> $bSpaceshipAn integer less than, equal to, or greater than zero when $a is respectively less than, equal to, or greater than $b. Available as of PHP 7.
Example : 1

<?php
var_dump(0 == "a"); // 0 == 0 -> true
var_dump("1" == "01"); // 1 == 1 -> true
var_dump("10" == "1e1"); // 10 == 10 -> true
var_dump(100 == "1e2"); // 100 == 100 -> true

switch ("a") {
case 0:
    echo "0";
    break;
case "a": // never reached because "a" is already matched with 0
    echo "a";
    break;
}
?>



PHP Increment / Decrement Operators :
The PHP increment operators are used to increment a variable's value.
The PHP decrement operators are used to decrement a variable's value.
ExampleNameEffect
++$aPre-incrementIncrements $a by one, then returns $a.
$a++Post-incrementReturns $a, then increments $a by one.
--$aPre-decrementDecrements $a by one, then returns $a.
$a--Post-decrementReturns $a, then decrements $a by one.
Example : 1
<?php
echo "<h3>Postincrement</h3>";
$a = 5;
echo "Should be 5: " . $a++ . "<br />\n";
echo "Should be 6: " . $a . "<br />\n";

echo "<h3>Preincrement</h3>";
$a = 5;
echo "Should be 6: " . ++$a . "<br />\n";
echo "Should be 6: " . $a . "<br />\n";

echo "<h3>Postdecrement</h3>";
$a = 5;
echo "Should be 5: " . $a-- . "<br />\n";
echo "Should be 4: " . $a . "<br />\n";

echo "<h3>Predecrement</h3>";
$a = 5;
echo "Should be 4: " . --$a . "<br />\n";
echo "Should be 4: " . $a . "<br />\n";
?>

Output :
--------------

Preincrement

Should be 6: 6
Should be 6: 6

Postdecrement

Should be 5: 5
Should be 4: 4

Predecrement

Should be 4: 4
Should be 4: 4

PHP Logical Operators :
The PHP logical operators are used to combine conditional statements.
ExampleNameResult
$a and $bAndTRUE if both $a and $b are TRUE.
$a or $bOrTRUE if either $a or $b is TRUE.
$a xor $bXorTRUE if either $a or $b is TRUE, but not both.
! $aNotTRUE if $a is not TRUE.
$a && $bAndTRUE if both $a and $b are TRUE.
$a || $bOrTRUE if either $a or $b is TRUE.
Example :1 
<?php
$x = 100;  
$y = 50;

if ($x == 100 and $y == 50) {
    echo "match found";
}
?>

Output :
---------------
match found

PHP String Operators:
PHP has two operators that are specially designed for strings.
1.  Concatenate the two variables using dot operator (.)
2. Append content using Concatenate  assignment operator (.=)

Example :1 
<?php
$a = "Hello ";
$b = $a . "World!"; // now $b contains "Hello World!"

$a = "Hello ";
$a .= "World!";     // now $a contains "Hello World!"
?>

PHP Array Operators:
The PHP array operators are used to compare arrays.
ExampleNameResult
$a + $bUnionUnion of $a and $b.
$a == $bEqualityTRUE if $a and $b have the same key/value pairs.
$a === $bIdentityTRUE if $a and $b have the same key/value pairs in the same order and of the same types.
$a != $bInequalityTRUE if $a is not equal to $b.
$a <> $bInequalityTRUE if $a is not equal to $b.
$a !== $bNon-identityTRUE if $a is not identical to $b.
Example :1
<?php
$a = array("a" => "apple", "b" => "banana");
$b = array("a" => "pear", "b" => "strawberry", "c" => "cherry");

$c = $a + $b; // Union of $a and $b
echo "Union of \$a and \$b: \n";
var_dump($c);

$c = $b + $a; // Union of $b and $a
echo "Union of \$b and \$a: \n";
var_dump($c);

$a += $b; // Union of $a += $b is $a and $b
echo "Union of \$a += \$b: \n";
var_dump($a);
?>










No comments:

Post a Comment