- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
A Server Side Web Tutorial Series # 3 - PHP Operators; Arithmetic, Array, Assignment, Comparison, Logical & more!
The Six Types of Operators in PHP
The six types of operators in PHP are: arithmetic operators, array operators, assignment operator, comparison operators, decrement/increment operators, and logical operators.
We will take each type, one by one with a simple explanation and follow each section with simple examples. Naturally, as we proceed through the tutorials we will see this operators again and again. The ones you may have difficulty in remembering will become old friends by the end of this tutorial series.
The Arithmetic Operators
The arithmetic operators are:
-
Addition operator represented by the plus sign. The result of addition of two numbers is there sum. Example:
$a = 5;
$b = 7;
$a + $b results in the value of 5 + 7 = 12.
-
Subtraction. represented by the (-) minus sign.The result is the difference between the two numbers. Example:
$a = 4;
$b = 6;
$a -$b results in the value of 4 – 6 = -2.
-
Multiplication is the product of two numbers. represented by the (*) asterisk symbol Example:
$a =10;
$b= 6;
$a * $b results in the value of 10 X 6 or 60.
-
Division results in the quotient of a by b. represented by the (/) forward slash symbol. Example
$a = 15;
$b = 5;
$a / $b result in 15/5 or the value 3.
-
Modulus is the remainder of a division operation. Example:
$a = 23;
$b = 5;
$a % $b $b does not divide into $a evenly. The result of the division is 4 with a remainder of 3. The $a modulus $b is 3.
-
Negation. Is the opposite of the value. If $a is a positive number -$a is negative. If $a is negative -$a is positive
-
Concatenation. This results in a concatenation of two strings. The concatenation of a string and a number value results in a sting due to the languages weak typing.
Arithmetic Operators Examples
The Array Operators
The PHP array operators can be put into three groups: identity/npn-identity, equal/not equal, identity/non-identity, and union.
Identity/non-identity
$ x === $y; Identity. For this statement to be true.Array $x and Array $y must have the same key/value pairs in the same order and be of the same type.
$x !== $y; Nonidentity. This will be true if Array $x is not identical to Array $y.
Equal/not Equal
$x == $y; Equality. To evaluate to true, they must agree for every key/value pair, but need not be of the same type.
$x != $y; To evaluate to true, Array $x and Array $y must have some different values.
$x <> $y; To evaluate to true, Array $x and Array $y must have some differences
Union
The union operator joins two or more arrays into a single array. In code it is represented by the (+) sign.
$x + $y;
One not on the union operator. It does not remove duplicate key values.
Array Operator Examples
The Assignment Operators
The basic assignment operator is represented by the equals sign (=). What it means is the left-hand side of the operator (referred to as the l-operand) is assigned the value of operation as it would be performed between the 1-operand and the r-operand. There are shorthand operators for addition, subtraction, multiplication, division, modulus, and concatenation. Examples
$x += $y; is the same as $x = $ x + $y;
$x -= $y; is the same as $x = $ x - $y;
$x *= $y; is the same as $x = $ x * $y;
$x /= $y; is the same as $x = $ x / $y;
$x %= $y; is the same as $x = $ x % $y;
$x .= $y; is the same as $x = $ x. $y;
The Assignment Operators
The Comparison Operators
The comparison operators come in pairs: identical/non-identical, equal/not equal, great than/less than,, greater than or equal to/less than or equal to.
identical/non-identical
$x === $y is true if the values are the same and they are of the same type.
$x !== $y is true if they differ in type or values
equal/not equal
$x == $y is true if they agree in values. They do not have to agree in type.
$x != $y is true if the have different values.
greater than/less than
$x > $y is true if the value of $x is greater than the value of $y.
$x < $y is true if the value of $x is less than the value of $y.
greater than or equal to/less than or equal to
$x >= $y is true if the value of $x is greater than or equal to the value of $y.
$x <= $y is true if the value of $x is less than or equal to the value of $y.
The Comparison Operators
Decrement/Increment Operators
There are four operators in this class: two pre- and two- post operands. The distinguish feature is when the value of the variable is changed.
The pre-operators:
++$x; $x is first incremented by 1 and then is available for use.
--$x; $x is decremented by 1 and then is available for use.
The post-operators:
$x++; $x is evaluated and used, then incremented.
$x--; $x is evaluated and used, then decremented.
Examples of the Use of Decrement Operators
The Logical Operators
The logical operators are and, or, exclusive or, and not.
The and operator can be expressed two different ways:
$x and $y is true only if $x is true and $y is true. It can also be expressed as $x && $y.
The or operator can be expressed two different ways as well.
$x or $y is true if at least one of them is true. It can also be expressed as $x || $y.
The exclusive or is expressed as xor. $x xor $y is true if either $x or $y is true, but not both.
The not operator !$x. This is true if $x was false. (or null????)
The Logical Operators
Wrap Up and What's Next
In the tutorial we covered all of the categories of operations that are found in the PHP language. Some of the items for review that you as a developer should consider are:
- the result of a logical expression when the variables are numbers
- the result of a logical expression when the "null" term is used
- and one thing we have not covered as yet, operator precedence when more than two arguments are be compared. The suggestion for right now is when in doubt use parenthesis to group the terms. For example, what would you do in the case of:
!$x || $y && $z xor $w
What would be the result given various values for the xariables $x, $y, $z, and $w
In the next tutorial we will start investigating conditional statement. Sever such as the if...else was used rater informally in some of the examples.The next tutorials will be more rigorous in out treatment of them.