4.3.7. Define the operators

From Computer Science
Jump to: navigation, search

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
PHP note
== converts the variables to the same data type and evaluates the values
=== evaluates both values and data type
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
Number comparison
Integers can be compared with other integers or floating point numbers. the numeric greater is assessed. 9<10, and 99.8<99.9
String Comparison
Strings are assessed alphabetically according to the ASCII (or unicode) table. "Mary" < "Fred" , but be aware that "Anton" < "mike" (since lower case characters appear earlier in the ASCII table than capital letters"
Boolean Comparison
Since true is given the value of 1, and false = 0, false < true
Null Comparison
null values (which you may get from a database result set) are undefined, and therefore will behave erratically in comparisons. Always initialise variables.
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
Number comparison
Integers can be compared with other integers or floating point numbers. the numeric greater is assessed. 10>9, and 99.9>99.8
String Comparison
Strings are assessed alphabetically according to the ASCII (or unicode) table. "Fred" > "Mary", but be aware that "mike" > "Anton" (since lower case characters appear earlier in the ASCII table than capital letters"
Boolean Comparison
Since true is given the value of 1, and false = 0, true > false
Null Comparison
null values (which you may get from a database result set) are undefined, and therefore will behave erratically in comparisons
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
Number comparison
Integers can be compared with other integers or floating point numbers. the numeric greater is assessed. 9<=10, and 99.8<=99.9
String Comparison
Strings are assessed alphabetically according to the ASCII (or unicode) table. "Mary" <= "Fred" , but be aware that "Anton" <= "mike" (since lower case characters appear earlier in the ASCII table than capital letters). Bear in mind as well that "Fred" is not equal to "fred".
Boolean Comparison
Since true is given the value of 1, and false = 0, false <= true
Null Comparison
null values (which you may get from a database result set) are undefined, and therefore will behave erratically in comparisons. Always initialise variables.
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
Number comparison
Integers can be compared with other integers or floating point numbers. the numeric greater is assessed. 10>=9, and 99.9>=99.8
String Comparison
Strings are assessed alphabetically according to the ASCII (or unicode) table. "Fred" >= "Mary" , but be aware that "mike" >= "Anton" (since lower case characters appear earlier in the ASCII table than capital letters). Bear in mind as well that "Fred" is not equal to "fred".
Boolean Comparison
Since true is given the value of 1, and false = 0, true >= flase
Null Comparison
null values (which you may get from a database result set) are undefined, and therefore will behave erratically in comparisons. Always initialise variables.
 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.
PHP
$i++ increments the variable and returns the value after it has been incremented.
++$i returns the value of the variable, and then increments it.

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.
PHP
$i-- decrements the variable and returns the value after it has been incremented.
++$i returns the value of the variable, and then decrements it.

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.
Note
1/2 = 0.5
intDiv(1, 2) = 0
intDiv(10,3) = 3
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
Note
1%2 = 1
10%3 = 1
11%3 = 2
12%3 = 0
$y = $x % %z;

See also

You can see all php operators here: [[1]]