4.3.7. Define the operators
From Computer Science
Revision as of 21:23, 9 November 2016 by Mr Russell (Talk | contribs)
All programming languages have a number of essential operators. Operators are the commands that compare, affect (change) or calculate something. The following essential operators are required byt he IB:
Comparison Operators
Comparison operators evaluate whether two variables are equal, and if not, how they are different:
| Operator | Name | PHP Syntax | Explaination | Usage example |
|---|---|---|---|---|
| = | Equals | == or === | Compares two numbers or strings to see if they are the same. Returns true or false
|
if ($x == $y) {
do something
}
if ($x === $y) {
do something
}
Caution if ($x = $y) {
do something
}
will always return true, and assign the value and type of $y to $x |
| ≠ | Not Equal to | != | Compares two numbers or strings to see if they are not the same. Returns true if they are not the same and false if they are the same |
if ($x != $y) {
do something
}
|
| < | Less than | < | Compares two numbers or strings to see if one is smaller than the other. Returns true if the first argument is smaller than the second, and false if they are equal or the second argument is smaller than the first
|
if ($a < $b) { do something }
|
| > | Greater than | > | Compares two numbers or strings to see if one is larger than the other. Returns true if the first argument is larger than the second, and false if they are equal or the second argument is larger than the first
|
if ($a < $b) {
do something
}
|
| <= | Less than or equal to | <= | Compares two numbers or strings to see if one is smaller than or equal to the other. Returns true if the first argument is smaller than or equal to the second, and false if the second argument is smaller than the first
|
if ($a <= $b) {
do something
}
|
| >= | Greater than or equal to | >= | Compares two numbers or strings to see if one is greater than or equal to the other. Returns true if the first argument is greater than or equal to the second, and false if the second argument is greater than the first
|
if ($a <= $b) {
do something } |
Calculation Operators
Calculation operators perform basic arithmetic on variables and return a result:
| Operator | Name | PHP Syntax | Explaination | Usage example |
|---|---|---|---|---|
| + | Addition | + or ++ | Adds two variables to each other, or increments a variable by 1.
Note, increment and decrement should only be used on integer variable types |
$x = $x + $y; or $x++; |
| - | Subtraction | - or -- | Subtracts two variables from each other, or decrements a variable by 1.
Note, increment and decrement should only be used on integer variable types |
$x = $x - $y; or $x--; |
| * | Multiplication | Multiplies two numbers together. |
$x = $x * $y; |
|
| / | Floating Point division | / | Floating point division calculates the floating point (fractional) division of two numbers. 1 divided by 2 = 0.5 |
%x = 1/2; |
| Div | Integer Division | intdiv() | Integer division returns the integer value only (rounded down value) of division. It takes two arguments, the numerator and the denominator.
|
intDiv($num, $den); |
| Mod | Modulus | % | The modulus is the "remainder" after integer division. Essentially, it is the number that must be subtracted from the numerator in order to allow for perfect integer division
|
$y = $x % %z; |
See also
You can see all php operators here: [[1]]