Operators in PHP
PHP & MySQL Course:Operators in PHP
In the previous lecture we learnt about the basics of PHP and used variables in different types.
Lecture 4 (Part-A):Operators in PHP
In this lecture we will learn the following topics. And you should practice todays lecture by your
heart because these are one of the most important topics.
- Use of Operators in PHP
- Use of Conditional Statements in PHP
Operators in PHP:
We use same operators in PHP & MySQL, which we used in C++ and Java Programming Languages.
But in PHP we will use them in a little bit change method. Because there are some operators are look
different from the C++ and Java Programming Language operators. Which you had been used. Let’s
we see the list of operators and their use.
Use of Arithmetic Operators in PHP & MySQL:
We use Arithmetic Operators for simple calculation in PHP programming. And these are very common
to use in other different type of languages. By the help of Arithmetic Operators in PHP programming it
becomes so simple now to create a web page for any economic institute. Check listing 1 for details how
we can use the Arithmetic Operators in PHP & MySQL.
Table 1 Arithmetic Operators in PHP & MySQL
Name of Operator |
Symbol |
Description |
Use In PHP |
Addition |
+ |
Sum two variables |
$a + $b |
Subtraction |
– |
Subtract two variables |
$a – $b |
Multiplication |
* |
Multiply two variables |
$a * $b |
Division |
/ |
Divide two variables |
$a / $b |
Modulus |
% |
Take remainder of any two variables |
$a % $b |
Use of Assignment Operators in PHP & MySQL:
We use Assignment Operators for comparison of values in PHP programming. By using them we can
compare anything in PHP webpages. These all assignment operators are same like which you had been
used in C++ and Java Programming languages. So don’t get worried about them. These are fully easy to
use them. And see the Listing 2, for details how you can use the Assignment operators in PHP & MySQL.
Table 2 Assignment Operators in PHP & MySQL
Name of Operator |
Symbol |
Description |
Use in PHP |
Equal |
= |
Compare two variable for equal |
$a = $b |
Add & Equal (Addition) |
+= |
Add and then compare two variables for equal |
$a += $b |
Subtract & Equal (Subtract) |
-= |
Subtract and then compare two variables for equal |
$ -= $b |
Multiply & Equal (Multiplication) |
*= |
Multiply and then compare two variables |
$a *= $b |
Divide & Equal (Division) |
/= |
Divide and then compare two variables |
$a /= $b |
Modulus & Equal (Remainder) |
%= |
Take remainder and then compare two variables |
$a %= $b |
Difference between Assignment & Arithmetic Operators in PHP & MySQL:
If we check the assignment and arithmetic operators in PHP & MySQL. So just their using methods is
change from each other. But their result will be same. Check the table three for comparison of Assignment
and Arithmetic Operators in PHP and MySQL and see the Listing 3 for their use and same results.
Table 3 Note their results will be same
Assignment Operators Symbol |
Arithmetic Operators Symbol |
Use of Arithmetic Operators |
Use of Assignment Operators |
+= |
+ |
$a + $b |
$a += $b |
-= |
– |
$a – $b |
$a -= $b |
*= |
* |
$a * $b |
$a *= $b |
/= |
/ |
$a / $b |
$a /= $b |
%= |
% |
$a % $b |
$a %= $b |
Listing 1: Using Arithmetic Operators in PHP
<? //using arithmetic operators in php & mysql $a = 2; $b = 2.5; $sum = $a + $b; $divide = $a / $b; $modulus = $a % $b; $multiply = $a * $b; $subtract = $a - $b; echo "<br>Addition of two values are: ".$sum; echo "<br>Division of two values are: ".$divide; echo "<br>Remainder of two values are: ".$modulus; echo "<br>Multiplication of two values are: ".$multiply; echo "<br>Subtraction of two values are: ".$subtract; ?>
Figure 1 Table 4 Result of Arithmetic Operators in PHP & MySQL
Listing 2: Using Assignment Operators in PHP
<? //using assignment operators in php & mysql $a = 2; $b = 2.5; echo "<br>Addition of two values are: ".$a += $b; echo "<br>Division of two values are: ".$a /= $b; echo "<br>Remainder of two values are: ".$a %= $b; echo "<br>Multiplication of two values are: ".$a *= $b; echo "<br>Subtraction of two values are: ".$a -= $b; ?>
Figure 2 Result of Assignment Operators in PHP & MySQL
Listing 3: Using Arithmetic & Assignment Operators in PHP
<? //using arithmetic operators in php & mysql $a = 2; $b = 2.5; $sum = $a + $b; $divide = $a / $b; $modulus = $a % $b; $multiply = $a * $b; $subtract = $a - $b; print "<br><h3>ARITHMETIC OPERATORS IN PHP & MYSQL</h3><br>"; echo "<br>Addition of two values are: ".$sum; echo "<br>Division of two values are: ".$divide; echo "<br>Remainder of two values are: ".$modulus; echo "<br>Multiplication of two values are: ".$multiply; echo "<br>Subtraction of two values are: ".$subtract; //using assignment operators in php & mysql $a = 2; $b = 3; print "<br><h3>ARITHMETIC OPERATORS IN PHP & MYSQL</h3><br>"; echo "<br>Addition of two values are: ".$a += $b; echo "<br>Division of two values are: ".$a /= $b; echo "<br>Remainder of two values are: ".$a %= $b; echo "<br>Multiplication of two values are: ".$a *= $b; echo "<br>Subtraction of two values are: ".$a -= $b; ?>
Figure 3 Result of Arithmetic & Assignment Operators in PHP & MySQL
Use of Increment & Decrement Operators in PHP & MySQL:
We use these two types of operators in PHP & MySQL for increase a variable value by 1 or
decrease by 1. And these operators are being used commonly in all programming languages as
now we will use them in PHP programming. Read the listing 4 for details and check the table 4 for
their full detailed information.
Table 4 Increment & Decrement Operators in PHP & MySQL
Name of Operator |
Symbol |
Description |
Use in PHP |
Post-Decrement |
— |
Returns variable, then decrease value of variable by 1. |
$a– |
Post-Increment |
++ |
Returns variable, then increase value of variable by 1. |
$a++ |
Pre-Decrement |
— |
Decrease value of variable by 1, and then return the variable |
–$a |
Pre-Increment |
++ |
Increase value of variable by 1, and then return the variable |
++$a |
Listing 4: Increment & Decrement Operators in PHP & MySQL
<? //using Increment Operators $a = 0; //result will be same as 0, and then increment by 1 so $a value will be 1 echo "Increased by one ".$a++; echo "<br>Increased by one but first add by one then increased ".++$a; //result will be 2, because $a = 1 + $a //using Decrement Operators $b = 0; //result will be same as 0, and then decrement by 1 so $b value will be -1 echo "<br>Decreased by one ".$b--; echo "<br>Decreased by one but first add by one then increased ".--$b; //result will be -2, because $b = -1 -$b ?>
Figure 4 Result of Increment & Decrement in PHP & MySQL