Operator and Expression play an important role in PHP programming. So, this article is designed to explain PHP Operator and Expression from very beginning. In last article, PHP variable and data types were also discussed from very beginning. If you are new here, feel free to study that article which will help you to learn PHP Operator and Expression so easily.
Operators and Expressions in PHP
Operators in PHP
Operators are used to make an expression which provides a meaningful result to process any algorithm in PHP programming. See below simple example.
Here, 6 and 4 are operands and plus sign (+) is operator. All are making an expression together and providing a result which can be used further algorithm processing.
Kinds of Operator in PHP
There are seven kinds of operator in PHP language. These are…
- Arithmetic Operator
- Increment and Decrement Operator
- Assignment Operator
- Comparison or Relational Operator
- Concatenation Operator
- Logical Operator
- Bitwise Operator
Generally five types of arithmetic operator are used in PHP programming. I think, you have enough experience about these operators in practical life or study life. So I’ll give a short description about these.
- Addition (+): This operator is used to add two numeric values. Example: 5+5=10.
- Subtraction (-): This operator is used to subtract two numeric values. Example: 6-1=5.
- Division (/): This operator is used to divide one numeric value by another numeric value. Example: 5/5=1.
- Multiplication (*): This operator is used to multiply multiple numeric values. Example: 5*5*2=50.
- Modulus (%): This operator is used to find the remainder from division operation. Example: 6%5=1.
Increment and Decrement Operator
These types of operator are used to increase or decrease an integer value gradually. These are specially used in loop statement and conditional statement of any programming language.
Increment Operator (++)
Double plus sign (++) is used to represent increment operator. It increases previous value by one. Such as…
Now the echo function will show 6 because the value of variable $a is increased by one.
Decrement Operator (–)
Double minus sign (–) is used to represent decrement operator. It decreases previous value by one. Such as…
Now, the result will be 4 because the value of variable $a is decreased by one.
Again, increment and decrement operators are also divided into two categories…
- Pre- increment or Pre-decrement
- Post-increment or Post-decrement
Pre increment or decrement
In this logic, the value of a variable is increased or decreased first and then the variable is used for next algorithm. See the below simple example.
In this example, the first echo function will show 6 and the second echo function will show 11 because the variable value is increased or decreased first and then the variable is used to process regular functionality.
Post increment or decrement
In contrast with pre-increment or pre-decrement, post–increment or post-decrement logic uses regular functionality first and then increases or decreases the value of a variable. See another simple example.
Here, the first echo function will show 5 and the second echo function will show 6 because the compiler will first show the normal functionality and then increase the value. The third and fourth echo function will do the same functionality but value will be decreased now.
In this article, I have explained the arithmetic operator and the increment and decrement operator from very beginning. If you have a question, feel free to contact with me from Contact us page. I’ll try my best to stay with you. In next article, I’ll discuss the remaining operators so deeply. Hope you will keep tuning with me.