Thursday, 14 March 2013
Complete All PHP Operators Tutorials
What We Have Learnt So Far About Php ?
- Php Web Development Tutorial Series And Introduction .
- Php Tools And Xampp Installation And Customization .
- How To Write Php Source Code And Run / Execute It In Web Browser ?
- How To Add Comments In Php Code ?
- How To Open And Close Php Tags And Their Types ?
- Everything About Php Variables , Their Decalaration And Assignments .
- What Are Php Programming Data Types ?
- Data Type Conversion Using Type Casting In Php .
- Data Type Conversion Using Functions In Php .
- How To Print Special Characters In Php ?
Php Operators |
Operators :
Now Its time to learn about programming operators. So now we can make a variable and assign a value to it easily and also we can find its data type and change data type of variable.
What Are Operators ?
for example : 5+10
Here in the above example 5 and 10 are operands and the + (plus sign) is the Operator. Using Operators on Operands performs a Operation . As a result, new value is produced. The new value produced in above example is 15 as a result of + operator. Except some operators, all operators are used between operands.
Lets have a look on some other operators.
Types Of Operators :
Lets have a look on some other operators.
- The Assignment Operator .
- Arithmetic Operators .
- Concatenation Operator .
- Combined Assignment Operators .
- Comparison Operators .
- Logical Operators.
- Increment And Decrement Operators .
The Assignment Operator ( = ) :
We have already discussed in our previous tutorials, How to assign a value to a variable. So the Equal sign (=) is called "Assignment Operator". It is used to assign a value to a variable.
For example
$name = "Asim";
Arithmetic Operators :
Types Of Arithmetic Operators :
- Operator Symbol : +
Name :Plus Operator
Example : 10 + 3
Result : 13
- Operator Symbol : -
Name : Minus Operator
Example : 10 - 7
Result : 3
- Operator Symbol : /
Name : Plus Operator
Example : 10 / 2
Result : 5
- Operator Symbol : *
Name : Multiplication Operator
Example : 10 * 2
Result : 20
- Operator Symbol : %
Name : Modulus (Remainder) Operator
Example : 10 % 3
Result : 1
I think You will be all familiar with all above arithmetic operators except %(Modulus) Operator. It will be new for you. % Mod operator actually return the remainder after dividing one operand with another.
Concatenation Operator (.) :
Concatenation Operator is an operator which joins two or more than two strings.
for Example : If you want to join strings, "Hello" and "World" , then this can be done using concatenation operator as follows: "Hello"."World". The output result of this concatenation example will be "HelloWorld"
Here is another example of Concatenation
$centimeters = 300;
print "The width is ".($centimeters/100). "meters";
Output : The width is 3 meters
In the above statement the value of centimeters variable is concatenated with two strings "The width is " and " meters".
Combined Assignment Operators :
Although we have just one assignment operator i-e ( = ) But php provides the facility of using it with Arithmetic Operators and Concatenation Operator,
For Example :
<?php
$y = 10;
$y = $y + 5; // $y now equals 15
print $y;
?>
We can also write above program as
<?php
$y = 10;
$y += 5; /* Assignment operator used with Arithmetic Operator . $y now equals 15*/
print $y;
?>
The output of both above programs will be same. But you can see we have combined Assignment operator = with Arithmetic Operator +. It means that the value of left operand ($y) is added is added to right operand (5) and the result is stored in left operand ($y). We can also used other arithmetic operators like (-,*,/,%) in place of + with assignment operator.
Types Of Combined Assignment Operators :
- Operator Symbol : +=
Example :
$y += 5
is equivalent and same to
$y = $y + 5
- Operator Symbol : -=
Example :
$y -= 5
is equivalent and same to
$y = $y - 5
- Operator Symbol : *=
Example :
$y *= 5
is equivalent and same to
$y = $y * 5
- Operator Symbol : /=
Example :
$y /= 5
is equivalent and same to
$y = $y / 5
- Operator Symbol : %=
Example :
$y %= 5
is equivalent and same to
$y = $y % 5
- Operator Symbol : .=
Example :
$y .= "Test"
is equivalent and same to
$y = $y . "Test"
Comparison Operators :
Comparison operators compares the operands with each other and in case of success , gives Boolean value true or false. These operands are mostly used in control structures and loops (ifelse statement, while loop , for loop )
For example :
If we want to check that 5 is smaller or not than the value stored in $X.
We will simple use this statement as
$x < 5;
If the value of $x is 4 then the above statement will return true and If the value of $x is 8 then the above statement will return false.
Types Of Comparison Operators :
- Operator Symbol : ==
Operator Name : Equivalence Or Equals
Operator Function : Checks the equality of operands
Example :200 == 200
Result : True
Because 200 is equal to 200. If operands are not equal then the output will be false.
- Operator Symbol : !=
Operator Name : Non-Equivalence or Not Equals To
Operator Function : Checks the inequality of operands
Example :200 != 100
Result : True
Because 200 is not equal to 100. If operands are equal then the output will be false.
- Operator Symbol : >
Operator Name : Greater than
Operator Function : Checks whether the left operand is greater than the right operand or not
Example :200 > 300
Result : False
Because 200 is not greater than 300. If left operand is greaer than right operand (for example 200 > 100)then the output will be true.
- Operator Symbol : <
Operator Name : Less than
Operator Function : Checks whether the left operand is less than the right operand or not
Example :200 < 300
Result : true
Because 200 is less than 300. If left operand is less than right operand (for example 200 < 100) then the output will be false.
- Operator Symbol : >=
Operator Name : Greater than or Equals to
Operator Function : Checks whether the left operand is greater than or equal to the right operand or not
Example :5 >= 15
Result : False
Because 5 is not greater than 15 and also 5 is not equal to 15. If left operand is greater than right operand or equal to the right operand (for example 200 >= 100) or (for example 200 >= 200) then the output will be true.
- Operator Symbol : <=
Operator Name : Less than or Equals to
Operaor Function : Checks whether the left operand is less than or equal to the right operand or not
Example :5 <= 15
Result : true
Because 5 is less than 15 . If left operand is greaer than right operand or equal to the right operand (for example 200 >= 100) or (for example 200 >= 200) then the output will be true.
- Operator Symbol : ==
Operaor Function : Checks whether the left operand VALUE AND DATAYPE is same as the right operand or not
Example :5 == "5"
Result : falseBecause 5 is an integer while "5" is string.
Logical Operators (and, or, not) :
These operators check the conditions and return boolean value true or false.
OR logical Operator :
This operator checks two conditions and gives true If any of the two values is true. Symbol Of OR Logical Operator is || or OR
Example
($age == 27) || ($age == 30 )
You can see that i am checking two conditions in above example. It means that I want value of age 27 or 30 . If the value of age is 27 then the ($age == 27) statement will return true and the output of OR || operator will be true. It means that OR Operator output will be true if any of the operands is true. If both operands conditions are false then only the output of OR operator will be false.
Here is the truth table of OR Logical Operator .
We can see in the truth table that the only false output occurs when both or all operands are false . If any of the operand is true then the output will be true. Working Of OR operator can be just like the circuit below. In this circuit the bulb will glow If any of the switches are closed.
AND Logical Operator checks two or more than two condiotions and the output is only true when all conditions are true. If any of the conditon is false then the output of AND operator will be false.
Example:
($age == 27) && ($country == "Pakistan")
The above condition which return true only when the age is 27 and country is Pakistan. Means the user is of 27 age and also a citizen of pakistan country, only then it will return true. You will understand AND operation more clearly with the help of this Truth Table.
Another comparison example For working of AND Logical Operator.
For example.
If we add the ++ after the name of variable then it is called Post Increment and It we add ++ before name of variable then it is called Pre Increment. If we add the -- after the name of variable then it is called Post decrement and It we add -- before name of variable then it is called Pre decrement.
For example ;
It is very important to understand the difference between Postfix and Prefix. When we perform post increment or post decrement Like in using $a++ , first value of variable is returned and then it is incremented or decremented in case of $a--. While in Pre Increment or Pre decrement , the value is incremented or decremented first and then it is returned with the incremented or decremented value.
If you have still a problem then lets make a program to understand.
You can see that i am checking two conditions in above example. It means that I want value of age 27 or 30 . If the value of age is 27 then the ($age == 27) statement will return true and the output of OR || operator will be true. It means that OR Operator output will be true if any of the operands is true. If both operands conditions are false then only the output of OR operator will be false.
Here is the truth table of OR Logical Operator .
And Logical Operator |
We can see in the truth table that the only false output occurs when both or all operands are false . If any of the operand is true then the output will be true. Working Of OR operator can be just like the circuit below. In this circuit the bulb will glow If any of the switches are closed.
And Logical Operator |
AND Logical Operator ( && ) :
AND Logical Operator checks two or more than two condiotions and the output is only true when all conditions are true. If any of the conditon is false then the output of AND operator will be false.
Example:
($age == 27) && ($country == "Pakistan")
The above condition which return true only when the age is 27 and country is Pakistan. Means the user is of 27 age and also a citizen of pakistan country, only then it will return true. You will understand AND operation more clearly with the help of this Truth Table.
OR Logical Operator |
Another comparison example For working of AND Logical Operator.
OR Logical Operator |
Not Logical Operator (!) :
Not Operator Just checks the coditon and if the condition is true then after applying NOT operation , the output will be false and if condion is false then the output will be true. In simple words NOT reverses the truth value. NOT is only applied on a single operand.Increment ( ++ ) And Decrement ( -- ) Operators :
These Operators can increase or decrease the value of a variable by 1. These operators are mostly used is loops. When we add two plus signs ++ after the name of a variable ( like $i++ )then it is called incrementing and if we add two minus signs -- after the name of a variable ( like $i++ ) then it is called decrementing. ++ is increment operaor and -- is called decrement operator.For example.
$a = 5 ;
$a++ ; /* Value of $a is incremented By 1 */
$a-- ; /* Value of $a is decremented By 1 */
If we add the ++ after the name of variable then it is called Post Increment and It we add ++ before name of variable then it is called Pre Increment. If we add the -- after the name of variable then it is called Post decrement and It we add -- before name of variable then it is called Pre decrement.
For example ;
$a = 5 ;
++$a ; /* $a is incremented */
--$a ; /* $a is decremented */
Difference Between Postfix And Prefix :
It is very important to understand the difference between Postfix and Prefix. When we perform post increment or post decrement Like in using $a++ , first value of variable is returned and then it is incremented or decremented in case of $a--. While in Pre Increment or Pre decrement , the value is incremented or decremented first and then it is returned with the incremented or decremented value.
If you have still a problem then lets make a program to understand.
<?php
/*using post increment */
$test_var = 5;
print " Post Increment ";
print "The value of the test_var is :".$test_var."<br>;
print " By Using Post Increment the value of test_var is :".$test_var++;
print "<br>; /* for next line */
print " On next line, the value is now : ".$test_var;
print "<br>; /* for next line */
/*using Post Decrement */
$test_var = 5;
print " Post Decrement ";
print "The value of the test_var is :".$test_var."<br>;
print "By Using Post Decrement the value of test_var is :".$test_var--;
print "<br>; /* for next line */
print " On next line, the value is now : ".$test_var;
print "<br>; /* for next line */
/*using Pre Increment */
$test_var = 5;
print " Pre Increment ";
print "The value of the test_var is :".$test_var."<br>;
print " By Using Pre Increment the value of test_var is :".++$test_var;
print "<br>; /* for next line */
print " On next line, the value is now : ".$test_var;
print "<br>; /* for next line */
/*using Pre Decrement */
$test_var = 5;
print " Pre Decrement ";
print "The value of the test_var is :".$test_var."<br>;
print " By Using Pre Decrement the value of test_var is :".--$test_var;
print "<br>; /* for next line */
print " On next line, the value is now : ".$test_var;
print "<br>; /* for next line */
?>
Output :
Post Increment
The value of test_var is : 5
By using Post Increment the value of test_var is : 5
On next line, the value is now : 6
Post Decrement
The value of test_var is : 5
By using Post Decrement the value of test_var is : 5
On next line, the value is now : 4
Pre Increment
The value of test_var is : 5
By using Pre Increment the value of test_var is : 6
On next line, the value is now : 6
Pre Decrement
The value of test_var is : 5
By using Pre Decrement the value of test_var is : 4
On next line, the value is now : 4
Php operators program |
In the above program we have declared a $test_var variable. Then by different ways we have Increment and Decrement the value of the variable. You can see that in Post Increment ANd Post Decrement , the value of the variable is increased or decreased by 1 in next line while in pre icrement or decrement the value of variable in increased or decreased by 1 in the same line and then its value is printed.
Your Turn Now :
I have tried my best and really worked hard for writing this brief and exclusive tutorial to help yo to understand all aspects of Php operators. I hope that It will really help you. If you have any problem or want to ask any question, then feel free to ask questions to me . I will surely help you. If you want to give any feedback or want to say thanks, then give it via comments. It will be a great honour for us. Subscribe to email and on social networks to get free future posts. As this post is a comprehensive and complete post so make sure to share it with others . Keep sharing. Thanks.
Share It To Others To Spread Knowledge : )
Author: Mohammad
Mohammad is the founder of STC Network which offers Web Services and Online Business Solutions to clients around the globe. Read More →
Related Posts:
PHP Php Operators Tricks and Tutorials Web Development
Subscribe to:
Post Comments (Atom)
0 comments: