PHP Operators
- In programming and logic, an operator symbol is used to perform an operation on some value such as addition, substraction, and so on
- Operators assign values, works with strings and numbers and can control program flow
- Types of operators
- Assignment
- Math (Same as when you learned math in grade school)
- String
- Comparison
- Logical
Assignment Operators
The Assignment Operators ( = ) sets the value to the left of the operand to the value on the right
You can also combine it with other operators
<?php
$a=$b; $a=$b;
$a+=$b; $a=$a+$b;
$a-=$b; $a=$a-$b;
$a*=$b; $a=$a*$b;
$a/=$b; $a=$a/$b;
$a%=$b; $a=$a%$b;
$a.=$b; $a=$a.$b;
?>
Concatenate Operator is Often used to create output
TIP: Do not think of = as "equal" ... think of it as "assignment"
TIP: Think of == as "equal"
Math Operators
The PHP math/arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc.
Examples | Name | Result |
-$a | Negative | Negative Value of $a |
$a + $b | Addition | Sum of $a and $b |
$a - $b | Substraction | 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 |
$a ** $b | Exponentiation | Result of raising $a to the $b'th power |
TIP: The division operator("/") return a float value anytime, even if the two operands are integers (or strings that get converted to integers)
There are also / operators and
Note: $a++ is equal to $a+1
String Operators
PHP has two operators that are specially designed for strings.
The conceatenation operator (.) allows you to combine two strings together into a single string
Examples | Name | Result |
$txt1 . $txt2 | Concatenation | Concatenation of $txt1 and $txt2 |
$txt1 .= $txt2 | Concatenation assignment | Appends $txt2 to $txt1 |
for example, both these lines of code have the same result
<?php
echo "Hello "."World";
echo "Hello World";
?>
Comparison Operators
If you compare an integer with a string, the string is converted to a number
If you compare two numerical strings, they are compared as integers
Examples | Name |
$a==$b | equal |
$a===$b | Identical (same value and type) |
$a!=$b | Not equal |
$a<>$b | Not equal (numerical) |
$a!==$b | Not Identical (values and types not the same) |
$a<$b | Less than |
$a>$b | Greater than |
$a<=$b | Less than or equal to |
$a>=$b | Greater than or equal to |
$a <=> $y=b | Spaceship(Returns an integer less than, equal to, or greater than zero, depending on if $a is less than, equal to, or greater than $b. Introduced in PHP 7.) |
Note: "Not identical" values and types are not the same
Logical Operators
You are already be femiliar with some of these operators
- and ("and" and && symbols are both supported)
- or ("or" and || symbols are both supported)
- xor ("xor" and ^ symbols are both supported)
- not ("not" and ! symbols are both supported)
Why are two symbols that do same thing?
- Order Precedence -it depends upon which symbol you use
- For Example- and or && - the entire statement may evaluate to a different result
- The reason for the two difference variation of "and" and "or" operators is that they operate at different precedences
- Be sure to define xor: $a xor $b if either is true but not both