This tutorial explain you how to use PHP operators in your scripts with sample
codings. Php variables are great for storing values in your script,
but they're not much use on their own. To manipulate variable values
and get useful results, you need to use operators.
What are PHP operators?
PHP operators let you combine values together to produce new values. For example, to add two numbers together to produce a new value, you use the + (addition) operator. The following PHP code displays the number 15:
Common PHP operators
There are many PHP operators, but this article concentrates on the ones you're likely to use most often. These can be broken down into the following operator types: Arithmetic => Carry out arithmetic operations such as addition and multiplication Assignment => Assign values to variables Comparison => Compare 2 values Increment/decrement => Increase or decrease the value of a variable Logical => Perform Boolean logic The following sections explore each of these operator types.
Arithmetic operators
Assignment operators
The basic assignment operator is = (an equals sign). This is used to assign a value to a variable
Comparison operators
PHP's comparison operators compare 2 values, producing a Boolean result of true if the comparison succeeded, or false if it failed. You often use comparison operators with statements such as if and while. PHP supports the following 8 comparison operators:
Increment/decrement operators
These two operators are very simple — they increase or decrease the value of a variable by 1:
Logical operators
PHP's logical operators combine values using Boolean logic. Each value to be combined is treated as either true or false — for example, 1, true, a non-empty string, and a successful comparison are all considered true, while 0, false, an empty string, and an unsuccessful comparison are all considered false. The true and/or false values are then combined to produce a final result of either true or false.
What are PHP operators?
PHP operators let you combine values together to produce new values. For example, to add two numbers together to produce a new value, you use the + (addition) operator. The following PHP code displays the number 15:
echo 2 + 13;
Common PHP operators
There are many PHP operators, but this article concentrates on the ones you're likely to use most often. These can be broken down into the following operator types: Arithmetic => Carry out arithmetic operations such as addition and multiplication Assignment => Assign values to variables Comparison => Compare 2 values Increment/decrement => Increase or decrease the value of a variable Logical => Perform Boolean logic The following sections explore each of these operator types.
Arithmetic operators
Symbol | Name | Example | Result |
---|---|---|---|
+ |
addition | echo 7 + 5 |
12 |
- |
subtraction | echo 7 - 5 |
2 |
* |
multiplication | echo 7 * 5 |
35 |
/ |
division | echo 7 / 5 |
1.4 |
% |
modulus | echo 7 % 5 |
2 |
Assignment operators
The basic assignment operator is = (an equals sign). This is used to assign a value to a variable
$firstNum = 4; $secondNum = 2; $myVariable = $firstNum + $secondNum;
Comparison operators
PHP's comparison operators compare 2 values, producing a Boolean result of true if the comparison succeeded, or false if it failed. You often use comparison operators with statements such as if and while. PHP supports the following 8 comparison operators:
Symbol | Name | Usage | Result |
---|---|---|---|
== |
equal to | a == b |
true if a equals b, otherwise false |
!= |
not equal to | a != b |
true if a does not equal b, otherwise false |
=== |
identical to | a === b |
true if a equals b and they are of the same type, otherwise false |
!== |
not identical to | a !== b |
true if a does not equal b or they are not of the same type, otherwise false |
< |
less than | a < b |
true if a is less than b, otherwise false |
> |
greater than | a > b |
true if a is greater than b, otherwise false |
<= |
less than or equal to | a <= b |
true if a is less than or equal to b, otherwise false |
>= |
greater than or equal to | a >= b |
true if a is greater than or equal to b, otherwise false |
Increment/decrement operators
These two operators are very simple — they increase or decrease the value of a variable by 1:
Symbol | Name | Example | Result |
---|---|---|---|
++ |
increment | $x++ or ++$x |
Adds 1 to the variable $x |
-- |
decrement | $x-- or --$x |
Subtracts 1 from the variable $x |
Logical operators
PHP's logical operators combine values using Boolean logic. Each value to be combined is treated as either true or false — for example, 1, true, a non-empty string, and a successful comparison are all considered true, while 0, false, an empty string, and an unsuccessful comparison are all considered false. The true and/or false values are then combined to produce a final result of either true or false.
Symbol | Name | Example | Result |
---|---|---|---|
&& |
and | a && b |
true if a and b are true , otherwise false |
and |
and | a and b |
true if a and b are true , otherwise false |
|| |
or | a || b |
true if a or b are true , otherwise false |
or |
or | a or b |
true if a or b are true , otherwise false |
xor |
xor | a xor b |
true if a or b — but not both — are true , otherwise false |
! |
not | !a |
true if a is false ; false if a is true |
0 comments:
Post a Comment