What is Operator? Simple answer can be given using expression
4 + 5 is equal to 9. Here 4 and 5 are called operands and + is called operator. PHP language supports following type of operators.
- Arithmetic Operators
- Comparision Operators
- Logical (or Relational) Operators
- Assignment Operators
- Conditional (or ternary) Operators
Lets have a look on all operators one by one.
Arithmatic Operators:
There are following arithmatic operators supported by PHP language:
Assume variable A holds 10 and variable B holds 20 then:
Show Examples
Operator | Description | Example |
+ | Adds two operands | A + B will give 30 |
- | Subtracts second operand from the first | A - B will give -10 |
* | Multiply both operands | A * B will give 200 |
/ | Divide numerator by denumerator | B / A will give 2 |
% | Modulus Operator and remainder of after an integer division | B % A will give 0 |
++ | Increment operator, increases integer value by one | A++ will give 11 |
-- | Decrement operator, decreases integer value by one | A-- will give 9 |
Comparison Operators:
There are following comparison operators supported by PHP language
Assume variable A holds 10 and variable B holds 20 then:
Show Examples
Operator | Description | Example |
== | Checks if the value of two operands are equal or not, if yes then condition becomes true. | (A == B) is not true. |
!= | Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. | (A != B) is true. |
> | Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. | (A > B) is not true. |
< | Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. | (A < B) is true. |
>= | Checks if the value of left operand is greater than
or equal to the value of right operand, if yes then condition becomes
true. | (A >= B) is not true. |
<= | Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. | (A <= B) is true. |
Logical Operators:
There are following logical operators supported by PHP language
Assume variable A holds 10 and variable B holds 20 then:
Show Examples
Operator | Description | Example |
and | Called Logical AND operator. If both the operands are true then then condition becomes true. | (A and B) is true. |
or | Called Logical OR Operator. If any of the two operands are non zero then then condition becomes true. | (A or B) is true. |
&& | Called Logical AND operator. If both the operands are non zero then then condition becomes true. | (A && B) is true. |
|| | Called Logical OR Operator. If any of the two operands are non zero then then condition becomes true. | (A || B) is true. |
! | Called Logical NOT Operator. Use to reverses the logical
state of its operand. If a condition is true then Logical NOT operator
will make false. | !(A && B) is false. |
Assignment Operators:
There are following assignment operators supported by PHP language:
Show Examples
Operator | Description | Example |
= | Simple assignment operator, Assigns values from right side operands to left side operand | C = A + B will assigne value of A + B into C |
+= | Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand | C += A is equivalent to C = C + A |
-= | Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand | C -= A is equivalent to C = C - A |
*= | Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand | C *= A is equivalent to C = C * A |
/= | Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand | C /= A is equivalent to C = C / A |
%= | Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand | C %= A is equivalent to C = C % A |
Conditional Operator
There is one more operator called conditional operator. This first
evaluates an expression for a true or false value and then execute one
of the two given statements depending upon the result of the
evaluation. The conditional operator has this syntax:
Show Examples
Operator | Description | Example |
? : | Conditional Expression | If Condition is true ? Then value X : Otherwise value Y |
Operators Categories:
All the operators we have discussed above can be categorised into following categories:
- Unary prefix operators, which precede a single operand.
- Binary operators, which take two operands and perform a
variety of arithmetic and logical operations.
- The conditional operator (a ternary operator), which takes three operands and evaluates either the second or third
expression, depending on the evaluation of the first expression.
- Assignment operators, which assign a value to a variable.
Precedence of PHP Operators:
Operator precedence determines the grouping of terms in an
expression. This affects how an expression is evaluated. Certain
operators have higher precedence than others; for example, the
multiplication operator has higher precedence than the addition
operator:
For example x = 7 + 3 * 2; Here x is assigned 13, not 20 because
operator * has higher precedence than + so it first get multiplied with
3*2 and then adds into 7.
Here operators with the highest precedence appear at the top of the
table, those with the lowest appear at the bottom. Within an
expression, higher precedence operators will be evaluated first.
Category | Operator |
Associativity |
Unary | ! ++ -- | Right to left |
Multiplicative | * / % |
Left to right |
Additive |
+ - | Left to right |
Relational |
< <= > >= | Left to right |
Equality | == != | Left
to right |
Logical AND |
&& | Left to right |
Logical OR | || | Left to
right |
Conditional |
?: | Right to left |
Assignment | = += -= *= /= %= | Right to left |
The if, elseif ...else and switch statements are used to take decision based on the different condition.
You can use conditional statements in your code to make your decisions. PHP supports following threedecision making statements:
- if...else statement - use this statement if you want to execute a set of code when a condition is true and another if the condition is not true
- elseif statement - is used with the if...else statement to execute a set of code if one of several condition are true
- switch statement
- is used if you want to select one of many blocks of code to be
executed, use the Switch statement. The switch statement is used to
avoid long blocks of if..elseif..else code.
The If...Else Statement
If you want to execute some code if a condition is true and another code if a condition is false, use the if....else statement.
Syntax
if (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
|
Example
The following example will output "Have a nice weekend!" if the
current day is Friday, otherwise it will output "Have a nice day!":
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
else
echo "Have a nice day!";
?>
</body>
</html>
|
If more than one line should be executed if a condition is true/false, the lines should be enclosed within curly braces:
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
{
echo "Hello!<br />";
echo "Have a nice weekend!";
echo "See you on Monday!";
}
?>
</body>
</html>
|
The ElseIf Statement
If you want to execute some code if one of several conditions are true use the elseif statement
Syntax
if (condition)
code to be executed if condition is true;
elseif (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
|
Example
The following example will output "Have a nice weekend!" if the current day
is Friday, and "Have a nice Sunday!" if the current day is Sunday. Otherwise it will output "Have a nice day!":
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
elseif ($d=="Sun")
echo "Have a nice Sunday!";
else
echo "Have a nice day!";
?>
</body>
</html>
|
The Switch Statement
If you want to select one of many blocks of code to be executed, use the Switch statement.
The switch statement is used to avoid long blocks of if..elseif..else code.
Syntax
switch (expression)
{
case label1:
code to be executed if expression = label1;
break;
case label2:
code to be executed if expression = label2;
break;
default:
code to be executed
if expression is different
from both label1 and label2;
}
|
Example
The
switch statement works in an unusual way. First it
evaluates given expression then seeks a lable to match the resulting
value. If a matching value is found then the code associated with the
matching label will be executed or if none of the lables match then
statement will will execute any specified default code.
<html>
<body>
<?php
$d=date("D");
switch ($d)
{
case "Mon":
echo "Today is Monday";
break;
case "Tue":
echo "Today is Tuesday";
break;
case "Wed":
echo "Today is Wednesday";
break;
case "Thu":
echo "Today is Thursday";
break;
case "Fri":
echo "Today is Friday";
break;
case "Sat":
echo "Today is Saturday";
break;
case "Sun":
echo "Today is Sunday";
break;
default:
echo "Wonder which day is this ?";
}
?>
</body>
</html>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
No comments:
Post a Comment