Difference between revisions of "4.3.7. Define the operators"
From Computer Science
Mr Russell (Talk | contribs) |
Mr Russell (Talk | contribs) |
||
| Line 32: | Line 32: | ||
| 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 | | 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 } | + | if ($x != $y) { |
| + | do something | ||
| + | } | ||
|- | |- | ||
| < | | < | ||
Revision as of 20:48, 7 November 2016
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 | <= | ||
| >= | Greater than or equal to | >= |
Calculation Operators
Clculation operators perform basic arithmatic on variables and return a result:
| Operator | Name | PHP Syntax | Explaination | Usage example |
|---|---|---|---|---|
| + | Addition | + or ++ | Compares two numbers or strings to see if they are the same.
PHP: == converts the variables to the same data type and evaluates the values, while === evaluates both values and data type |
if ($x == $y) { do something } |
| - | Subtraction | - or -- | ||
| * | Multiplication | |||
| / | Floating Point division | / | ||
| Div | Integer Division | div | ||
| Mod | Modulus | mod |