PHP
PHP Operators - Arithmetic operator, Assignment Operator, Increment-decrement Operator
- PHP Operators
- Types of Operators
¶PHP Operators
To work with PHP variables and make any operations, PHP operators are used. Operators take one or more values (Operands/Variables) and yields another value. After making, we can call that an expression.
¶Example of Operator and Operands
<?php
$summation = 10 + 30;
echo $summation; // 40
?>
In the above example -
- 10 = Operand
- 20 = Operand
-
+
= Operator
Or, this could be also done with Variable -
<?php
$number1 = 10;
$number2 = 30;
$summation = $number2 / $number1; // Here, / is the Division operator
echo $summation; // 3
?>
In this line -
$summation = $number2 / $number1;
$number1
and $number2
are variables and /
is the Division operator.
¶Types of Operators
According to operator values it takes, we can distinguish them below types -
- Arithmetic Operator
- Assignment Operator
- Increment-Decrement Operator
- String Operator
- Logical Operator
- Comparison Operator
- Conditional Operator
- Bitwise Operator
- Array Operator
- Type Operator
- Error Control Operator
- Execution Operator.
We’ll discuss only the First three operators in this class. Others will be discussed later, on specific part.
¶Arithmetic Operator
Arithmetic operators are the basic mathematic arithmetics actually. Let’s get the list of arithmetic operators -
Operator | Name | Example | Result |
---|---|---|---|
+ | Addition |
$a + $b ==> $sum = 10 + 20; // 30
|
Summation of $a and $b |
- | Subtraction |
$a - $b ==> $sub = 20 - 10; // 10
|
Subtraction of $a and $b |
* | Multiplication |
$a * $b ==> $mul = 10 * 20; // 200
|
Multiplication of $a and $b |
/ | Division |
$b / $a ==> $div = 20 / 10; // 2
|
Division of $b by $a |
% | Modulo |
$a % $b ==> $mod = 11 % 5; // 1
|
Reminder of $a divided by $b |
** | Exponentiation |
$a ** $b ==> $exp = 5**2; // 25
|
Exponentiation of $a to the power $b |
¶Examples of Arithmetic Operator
¶Problem 1: Summation of Two number: Sum of 10 and 20
$number1 = 10;
$number2 = 20;
echo "Summation = " . $number1 + $number2;
Output:
¶Problem 2: Division of Two number: 20 divide by 5
$number1 = 20;
$number2 = 5;
echo "Division = " . $number1 / $number2;
Output:
¶Problem 3: Exponentiation of Two number: 5 to the power 3
$number = 5;
$power = 3;
echo "Exponentiation = " . $number ** $power;
Output:
¶Assignment Operator
Assignment operators are used to assign value on variable.
**Example: **
<?php
$name = "Jhon"; // here = is the assignment operator.
?>
The basic assignment operator is =
(equal to). It actually means that the left operand ($name
) gets set to the value of the expression on the right (Jhon
).
All other assignment operators are actually made from this concept. Let’s get all assignment operators:
Operator | Name | Example | Result |
---|---|---|---|
= | Equal to or Assign |
$a = 5;
|
Assign 5 on $a variable. |
+= | Plus Equal (Add then Assign) |
$a = 10; $a += 5;
|
Same as $a = $a + 5; ==> 15 |
-= | Minus Equal (Subtract then Assign) |
$a = 10; $a -= 5;
|
Same as $a = $a - 5; ==> 5 |
*= | Multiplication Equal (Multiplication then Assign) |
$a = 10; $a *= 5;
|
Same as $a = $a * 5; ==> 50 |
/= | Divide Equal (Divide then Assign) |
$a = 10; $a /= 5;
|
Same as $a = $a / 5; ==> 2 |
%= | Modulo Equal (Modulus then Assign) |
$a = 11; $a %= 5;
|
Same as $a = $a % 5; ==> 1 |
¶Problem 4: Summation and assign
$a = 10;
$a += 5;
echo "a = " . $a;
Output:
¶Increment/Decrement Operator
Increment or Decrement operators are another kind of PHP operators which used to increase or decrease PHP variables value.
There are two types increment and decrement operator actually.
Operator | Name | Example | Result |
---|---|---|---|
++ | Increment |
$a++
|
Return the value and then increment. |
++$a
|
Increment the value and Return. | ||
-- | Decrement |
$a--
|
Return the value and then decrement. |
--$a
|
Decrement the value and Return. |
Example:
<?php
$a = 5;
echo $a++; // 5
echo $a; // 6
$b = 20;
echo ++$b; // 21
echo $b; // 21
?>
¶String Operator
To manipulate strings or perform any operations on strings, String operator is used.
There are two types string operator actually.
Operator | Name | Example | Result |
---|---|---|---|
. | Concatenation |
echo "Hello " . $name
|
Concat and append the string with old string. |
.= | Concatenation and Assign |
$a = "Hello "; $a .= $name
|
Concat and assign and append the string with old string. |
Example:
<?php
$name = "Jhon";
echo "Hello " . $name; // Hello Jhon
$welcome_text = "Hello ";
$name = "Jhon Doe";
$welcome_text .= $name;
echo $welcome_text; // Hello Jhon Doe
?>
Echo, PHP Statement, Print, Concat and Comments in PHP
PHP Operators Part 2 - Comparison Operator, Logical Operator, Conditional Operator
All Tutorials in this playlist
Popular Tutorials
Categories
-
Artificial Intelligence (AI)
11
-
Bash Scripting
1
-
Bootstrap CSS
0
-
C Programming
14
-
C#
0
-
ChatGPT
1
-
Code Editor
2
-
Computer Engineering
3
-
CSS
28
-
Data Structure and Algorithm
18
-
Design Pattern in PHP
2
-
Design Patterns - Clean Code
1
-
E-Book
1
-
Git Commands
1
-
HTML
19
-
Interview Prepration
2
-
Java Programming
0
-
JavaScript
12
-
Laravel PHP Framework
37
-
Mysql
1
-
Node JS
1
-
Online Business
0
-
PHP
28
-
Programming
8
-
Python
12
-
React Js
19
-
React Native
1
-
Redux
2
-
Rust Programming
15
-
SEO - Search Engine Optimization
1
-
Tailwind CSS
1
-
Typescript
10
-
Uncategorized
0
-
Vue JS
1
-
Windows Operating system
1
-
Woocommerce
1
-
WordPress Development
2
Tags
- Artificial Intelligence (AI)
- Bash Scripting
- Business
- C
- C Programming
- C-sharp programming
- C++
- Code Editor
- Computer Engineering
- CSS
- Data Structure and Algorithm
- Database
- Design pattern
- Express JS
- git
- Git Commands
- github
- HTML
- Java
- JavaScript
- Laravel
- Mathematics
- MongoDB
- Mysql
- Node JS
- PHP
- Programming
- Python
- React Js
- Redux
- Rust Programming Language
- SEO
- TypeScript
- Vue JS
- Windows terminal
- Woocommerce
- WordPress
- WordPress Plugin Development