|
PHP Tutorial >> Operators PHP - Operators
In all programming languages alike, operators are used to manipulate or perform
operations on variables and values. You have already
seen the string concatenation operator "." in the
Echo
Lesson and the assignment operator "=" in pretty much every PHP example so far.
There are many operators used in PHP, so we have separated them into
the following categories to make it easier to learn them all.
Assignment Operators
Assignment operators are used to set a variable equal to a value or set
a variable to another variable's value. Such an assignment of value is done with
the "=", or equal character. Example:
-
$my_var = 4;
-
$another_var = $my_var
Now both $my_var and $another_var contain the value 4. Assignments can
also be used in conjuction with arithmetic operators.
Arithmetic Operators
|
Operator |
English |
Example |
|
+ |
Addition |
2 + 4 |
|
- |
Subtraction |
6 - 2 |
|
* |
Multiplication |
5 * 3 |
|
/ |
Division |
15 / 3 |
|
% |
Modulus |
43 % 10 |
PHP Code:
$addition = 2 + 4;
$subtraction = 6 - 2;
$multiplication = 5 * 3;
$division = 15 / 3;
$modulus = 5 % 2;
echo "Perform addition: 2 + 4 = " . $addition . "<br />";
echo "Perform subtraction: 6 - 2 = " . $subtraction . "<br />";
echo "Perform multiplication: 5 * 3 = " . $multiplication . "<br />";
echo "Perform division: 15 / 3 = " . $division . "<br />";
echo "Perform modulus: 5 % 2 = " . $modulus
. ". Modulus is the remainder after the division operation has been performed.
In this case it was 5 / 2, which has a remainder of 1.";
Display:
Perform addition: 2 + 4 = 6
Perform subtraction: 6 - 2 = 4
Perform multiplication: 5 * 3 = 15
Perform division: 15 / 3 = 5
Perform modulus: 5 % 2 = 1. Modulus is the remainder after the division operation has been performed.
In this case it was 5 / 2, which has a remainder of 1.
Comparison Operators
Comparisons are used to check the relationship between variables and/or
values. You have already seen an "==" or "is equal to" comparison in the
If Statement Lesson. Comparison
operators are used inside conditional statements and evaluate to either true
or false. Here are the most important comparison operators of PHP.
$x = 4; $y = 5;
|
Operator |
English |
Example |
Result |
|
== |
Equal To |
$x == $y |
false |
|
!= |
Not Equal To |
$x != $y |
true |
|
< |
Less Than |
$x < $y |
true |
|
> |
Greater Than |
$x > $y |
false |
|
<= |
Less Than or Equal To |
$x <= $y |
true |
|
>= |
Greater Than or Equal To |
$x >= $y |
false |
String Operators
As we have already seen in the
Echo
Lesson, the period "." is used to add two strings together, or more technically,
the period is the concatenation operator for strings.
PHP Code:
$a_string = "Hello";
$another_string = " Billy";
$new_string = $a_string . $another_string;
echo $new_string . "!";
Display:
Combination Arithmetic & Assignment Operators
In programming it is a very common task to have to increment a variable by some fixed amount. The
most common example of this is a counter. Say you want to increment a counter by 1, you would
have:
However, there is a shorthand for doing this.
This combination assignment/arithmetic operator would accomplish the same task. The downside to this
combination operator is that it reduces code readability to those programmers who are not used to such
an operator. Here are an example of other common
shorthand operators. In general, "+=" and "-=" are the most widely used combination operators.
|
Operator |
English |
Example |
Equivalent Operation |
|
+= |
Plus Equals |
$x += 2; |
$x = $x + 2; |
|
-= |
Minus Equals |
$x -= 4; |
$x = $x - 4; |
|
*= |
Multiply Equals |
$x *= 3; |
$x = $x * 3; |
|
/= |
Divide Equals |
$x /= 2; |
$x = $x / 2; |
|
%= |
Modulo Equals |
$x %= 5; |
$x = $x % 5; |
|
.= |
Concatenate Equals |
$my_str.="hello"; |
$my_str = $my_str . "hello"; |
Pre/Post-Increment & Pre/Post-Decrement
This may seem a bit absurd, but there is even a shorter shorthand for the common
task of adding 1 or subtracting 1 from a variable. To add one to a variable or "increment"
use the "++" operator:
To subtract 1 from a variable, or "decrement" use the "--" operator:
In addition to this "shorterhand" technique, you can also specify whether you
want the increment to occur while the line of code is being executed or after the
line has executed. Our PHP code below will display the difference.
PHP Code:
$x = 4;
echo "The value of x with post-incrementing = " . $x++;
echo "<br /> The value of x after the post-increment is " . $x;
$x = 4;
echo "<br />The value of x with with pre-incrementing = " . ++$x;
echo "<br /> The value of x after the pre-increment is " . $x;
Display:
The value of x with post-incrementing = 4
The value of x after the post-increment is = 5
The value of x with with pre-incrementing = 5
The value of x after the pre-increment is = 5
As you can see the value of $x++ is not reflected in the echoed text because
the variable is not incremented until after the line of code is executed. However,
with the pre-increment "++$x" the variable does reflect the addition immediatly.
|