In all
programming languages, operators are used to manipulate or perform operations
on variables and values.
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
·
Arithmetic Operators
·
Comparison Operators
·
String Operators
·
Logical Operators
·
Ternary Operators
·
Incremet/ Decrement Operators
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.
Operator
|
Same as
|
Example
|
=
|
$x=$y
|
$x=$y
|
+=
|
$x += $y
|
$x = $x +
$y
|
-+
|
$x -= $y
|
$x = $x - $y
|
*=
|
$x *= $y
|
$x = $x *
$y
|
/=
|
$x /= $y
|
$x = $x /
$y
|
%=
|
$x %= $y
|
$x = $x %
$y
|
Example:
·
$my_var = 4;
·
$another_var = $my_var;
Now both $my_var and
$another_var contain the value 4.
Arithmetic Operators:
Operator
|
English
|
Example
|
- $a
|
Negation
|
Opposite of $a
|
$a + $b
|
Addition
|
2 + 4
|
$a - $b
|
Subtraction
|
6 - 2
|
$a * $b
|
Multiplication
|
5 * 3
|
$a / $b
|
Division
|
15 / 3
|
$a % $b
|
Modulus
|
43 % 10
|
PHP Code:
Comparison Operators:
Comparisons are used to check
the relationship between variables and/or values.
Comparison operators are used
inside conditional statements and evaluate to either true or false. Here are
the most important comparison operators of PHP.
Assume: $x = 4 and $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
|
PHP Code:
Output:
String Operators :
There are two string operators.
The first is the
concatenation operator (“ . ”) , which returns the concatenation of the its
right and left arguments.
The second is
the concatenating assignment operator (“ .= ”), which appends the argument on
the right side to the argument on the left side.
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:
·
$x++; Which is equivalent to $x += 1; or $x = $x + 1;
To subtract 1 from a variable,
or "decrement" use the "--" operator:
·
$x--; Which is equivalent to $x -= 1; or $x = $x - 1;
In addition to this
"shorterhand" technique, you can specify whether you want to
increment before the line of code is being executed or after the line has
executed. Our PHP code below will display the difference.
PHP Code:
Output:
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 immediately.
Ternary operator:
It is called the ternary operator
because it takes three operands - a condition, a result for true, and a result
for false. If that sounds like an if statement to you, you are right on the
money - the ternary operator is a shorthand (albeit very hard to read) way of
doing if statements. Here's an example:
First there is a condition ($age
< 16), then there is a question mark, and then a true result, a colon, and a
false result. If $age is less than 16, $agestr will be set to 'child',
otherwise it will be set to 'not child'. That one-liner ternary statement can
be expressed in a normal if statement like this:
So, in
essence, using the ternary operator allows you to compact five lines of code
into one, at the expense of some readability.
No comments:
Post a Comment