Operators are used to perform operations on variables and values.
PHP divides the operators in the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Increment/Decrement operators
- Logical operators
- String operators
- 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.
| Example | Name | Result |
| +$a | Identity | Conversion of $a to int or float as appropriate. |
| -$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 | Modulo | Remainder of $a divided by $b. |
| $a ** $b | Exponentiation | Result 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.
Example :1
Example : 2
PHP Comparison Operators:
The PHP comparison operators are used to compare two values (number or string):
Example : 1
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.
Example : 1
Output :
--------------
Should be 6: 6
Should be 4: 4
Should be 4: 4
PHP Logical Operators :
The PHP logical operators are used to combine conditional statements.
Example :1
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 Array Operators:
The PHP array operators are used to compare arrays.
Example :1
The PHP assignment operators are used with numeric values to write a value to a variable.
| Example | Name | Result |
| a = b | The left operand gets set to the value of the expression on the right | a = b |
| a += b | Addition | a = a + b |
| a -= b | Subtraction | a = a - b |
| a *= b | Multiplication | a = a * b |
| a /= b | Division | a = a / b |
| a %= b | Modulus | a = 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):
| Example | Name | Result |
| $a == $b | Equal | TRUE if $a is equal to $b after type juggling. |
| $a === $b | Identical | TRUE if $a is equal to $b, and they are of the same type. |
| $a != $b | Not equal | TRUE if $a is not equal to $b after type juggling. |
| $a <> $b | Not equal | TRUE if $a is not equal to $b after type juggling. |
| $a !== $b | Not identical | TRUE if $a is not equal to $b, or they are not of the same type. |
| $a < $b | Less than | TRUE if $a is strictly less than $b. |
| $a > $b | Greater than | TRUE if $a is strictly greater than $b. |
| $a <= $b | Less than or equal to | TRUE if $a is less than or equal to $b. |
| $a >= $b | Greater than or equal to | TRUE if $a is greater than or equal to $b. |
| $a <=> $b | Spaceship | An 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. |
<?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.
| Example | Name | Effect |
| ++$a | Pre-increment | Increments $a by one, then returns $a. |
| $a++ | Post-increment | Returns $a, then increments $a by one. |
| --$a | Pre-decrement | Decrements $a by one, then returns $a. |
| $a-- | Post-decrement | Returns $a, then decrements $a by one. |
<?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: 6Should be 6: 6
Postdecrement
Should be 5: 5Should be 4: 4
Predecrement
Should be 4: 4Should be 4: 4
PHP Logical Operators :
The PHP logical operators are used to combine conditional statements.
| 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. |
<?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.
| Example | Name | Result |
| $a + $b | Union | Union of $a and $b. |
| $a == $b | Equality | TRUE if $a and $b have the same key/value pairs. |
| $a === $b | Identity | TRUE if $a and $b have the same key/value pairs in the same order and of the same types. |
| $a != $b | Inequality | TRUE if $a is not equal to $b. |
| $a <> $b | Inequality | TRUE if $a is not equal to $b. |
| $a !== $b | Non-identity | TRUE if $a is not identical to $b. |
<?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