# PHP Operators and Expression Explanation

PHP operator is one of the basic terms in PHP language. Operator and operand collectively makes expression. Basically no PHP program is possible without operator and expression. So, a newbie PHP programmer should have proper understanding on PHP operators and expression. There are eight kinds of operators in PHP language.

In my previous article, I explained PHP data types and variables elaborately (which are another basic term in PHP language) and in this article I am going to discuss PHP Operators and Expression elaborately.

## PHP Operators

Operators are used to make an expression which provides a meaningful result to process any algorithm in PHP programming. The following line is a simple example of PHP operator.

6+4=10

Here, 6, 4 and 10 are operands and plus sign (+) and equal sign (=) are operators. All are making an expression together and providing a result which can be used for further algorithm processing.

### Kinds of Operator in PHP

Like other programming language, various kinds of operators are used in PHP language. The following eight kinds of operators are usually used in PHP programming language.

• Arithmetic Operators
• Increment and Decrement Operators
• Assignment Operators
• Comparison or Relational Operators
• Concatenation Operators
• Logical Operators
• Bitwise Operators and
• Ternary Operator

### Arithmetic Operators

Arithmetic operators are usually used to make mathematical expression. The following five types of arithmetic operators are used in PHP programming.

• 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 two or more 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 Operators

The increment and decrement operators are used to increase or decrease an integer value gradually. These operators are specially used in loop statement or conditional statement in PHP programming language.

#### Increment Operator (++)

Double plus sign (++) is used to represent increment operator.  It increases previous value by one. The following program is an example of increment operator.

```<?php
\$a=5;
\$a++;
echo \$a;
?>
```

Here, the value of variable \$a has been assigned to 5 and then increment operator has been applied. So, the value of \$a will be 6 and the result will be found with echo function.

#### Decrement Operator (–)

Double minus sign (–) is used to represent decrement operator. It decreases previous value by one. The following program is an example of decrement operator.

```<?php
\$a=5;
\$a--;
echo \$a;
?>
```

Here, the value of variable \$a has been assigned to 5 and then decrement operator has been applied. So, the value of \$a will be 4 and the result will be found with echo function.

The increment and decrement operators are also divided into the following two categories.

• Pre-increment and Pre-decrement
• Post-increment and Post-decrement

Pre-increment and Pre-decrement Operators

With pre-increment or pre-decrement operator, the value of a variable is increased or decreased first and then the variable is used for next algorithm. The following program is an example of pre-increment and pre-decrement operators.

```<?php
\$a=5;
\$b=10;
echo ++\$a;
echo --\$b;
?>
```

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 and Post-decrement Operators

In contrast with pre-increment or pre-decrement, post–increment or post-decrement operator first does regular functionality and then increases or decreases the value of a variable. The following program is an example of post-increment and post-decrement operator.

```<?php
\$a=5;
\$b=10;
echo \$a++;
echo \$a;
echo \$b--;
echo \$b;
?>
```

Here, the first echo function will show 5 and the second echo function will show 6 because PHP 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 in this case.

### Assignment Operators

Assignment operators are used to assign any value into a variable. These operators can be used in the following seven ways.

• Equal to (=): Simply it is used to put any value to any variable. Example: \$a=5;
• Plus then equal to (+=): This operator adds a numeric value with the previous value of a variable and then keeps the result into that variable. Such as:  \$a=5; \$a+=10; Now \$a contains 15. The expression can also be expressed as: \$a=\$a+5; this expression expresses that the numeric value 5 will be added with the previous value of \$a and then the result will be assign into \$a.
• Minus then equal to (-=): This operator subtracts a numeric value from the previous value of a variable and then keeps the result into that variable. For example: \$a=5; \$a-=3; Now \$a contains 2.
• Divide then equal to (/=): This operator divides a variable value by a numeric value and then put the result into that variable. For example: \$a=10; \$a/=2; Now \$a contains 5.
• Multiply then equal to (*=): This operator multiplies a numeric variable value with the previous value of a variable and then keeps the result into that variable. For example: \$a=5; \$a*=5; So, \$a is now 25.
• Modulus then equal to (%=): This operator divides a variable value by a numeric value and then keeps the remainder into that variable. For example: \$a=22; \$a%=7;Now \$a contains 1.
• Concatenate then equal to (. =): This operator concatenates a new string value with the previous string value of a variable and then keeps the resultant string value into that variable. For example: \$a=”My name is”; \$a. =”Tom”; Now \$a contains “My name is Tom” string value.

### Comparison or Relational Operators

Comparison or Relational operators return either true or false value. These types of operators can be divided into the following seven categories.

• Double equal (==): This operator is used to compare any two values without case sensitivity or data type. If two values are equal, it returns true otherwise it returns false.  For example: \$a=’A’; \$b=’a’; Now if \$a is compared with \$b with double equal operator, it will return true because double equal compares without case sensitivity and data type.
• Triple equal or Identical (===): This operator is used to compare any two values with case sensitivity and data types. Identical operator compares two values so hardly. Comparing values must be equal with case and data type otherwise it will return false. For example: \$a=’A’; \$b=’a’; Now if \$a is compared with \$b with identical operator, it will return false value because variable \$a and variable \$b are not exactly same. Now see below example: \$a=’A’; \$b=’A’; Now comparison will return true value because two variables contain exactly same value. Identical operator is mostly used to compare password value in PHP developing environment. So, when we will develop any site or program where password needs to be compared, we must use identical operator otherwise our development will be very poor.
• Not equal to (!= or <>): This operator returns a deep logical result. For example: \$a = 5; Now if we express \$a != 5, the result will be false because we have assigned variable \$a=5 but expression says variable \$a not equal 5. So, program will return a false value because our expression is going against our declaring variable value. Again, if we assign \$a = 4 and the expression is \$a != 5, the result will be true because \$a = 4 and expression is saying variable \$a is not equal 5. Now, our expression is true with declaring value. So, the operator will return a true value.
• Less than (<): This operator is used to compare any variable value with a given value. If the value of the variable is smaller than the given value, it will return true otherwise it will return false. Say, we have assigned \$a =5 and our expression is \$a < 10, then our program will return true because here \$a is obviously smaller than 10. But if we express \$a < 4, we will find a false value because 4 is not smaller than 5.
• Greater than (>): This operator is used to compare any variable value with a given value. If left side value is greater than right side value, a Boolean true is returned otherwise Boolean false is returned. Say, we have assigned \$a =5 and our expression is \$a > 4. Then we will get a true value. Now, if our expression is \$a > 7, you will get a false value.
• Less than or equal to (<=): This operator returns a true value if left side value is smaller or equal to the right side value. Otherwise it will return a Boolean false value. Say, \$a =5. Now, if we have declare an expression \$a <= 8, the result will be true because 5 is less than 8. Again, expression is \$a <=5. Now result will be true because \$a is equal to 5. Again, if our expression is \$a <= 4. This time the result will be false because 5 is greater than 4.
• Greater than or equal to (>=): The operation of this operator is reverse from the previous less than equal to operator (<=). If left side value is greater or equal from the right side value, the result will be Boolean true otherwise false. Say, \$a = 10 and an expression is \$a >= 5. So, we will get Boolean true because 10 is greater than 5. Now our expression is \$a >= 10. This time we will also get true value. Again, our expression is \$a >= 15. Then we will get a Boolean false because 10 is never greater or equal to 15.

### Logical Operators

Logical operator returns Boolean true or false value with the combination of two expressions. It is a frequently used operator in PHP Language. The Logical operator is divided into the following four types.

• AND operator (&&): This operator can be expressed as (&&) or string (and or AND). This operator returns a Boolean true if the left side expression and the right side expression of this operator return true value. The following truth table is showing the operation of AND operator.

Here, 0 = false and 1 = true. Left Expression represents the result of left expression of AND operator and Right Expression represents the result of right expression of AND operator. So, from the truth table we can easily understand that if both expressions of AND operator are true, we will get true value otherwise false value. We can use this logical operator to verify the username and the password of any application. If both the username and the password of any user are matched, the user can login to the application otherwise the user will be denied.

• OR operator (||): This operator can be written as (||) or string (or and OR). This operator returns a Boolean true if any expression of this operator returns true value. The truth table of OR operation is shown in the following table.

From the truth table, we can easily understand that if any side of OR operator gives true value, the result will be true otherwise false. Say, we have developed an application to give points 10 to the US and the UK people. So, we can develop the application logic like if country equal to UK OR country equal to US, give 10 points else give 5 points. This is a tiny example of OR operator.

• Exclusive OR (XOR) operator: This operator returns true value if both side of this operator return the same logical value. The truth table of XOR operation is given below.
• Logical NOT (!): The Logical NOT operator returns reverse logical value. If the logical value is true, it returns false and if the logical value is false, it returns true. Every PHP variable returns two values. Firstly, it returns assigned value and secondly it returns true if a value is assigned otherwise returns false. Say, \$a=5; If our expression is !\$a, you will get false because there is a value in variable \$a. Now we assign \$a=NULL. If our expression is !\$a, we will get Boolean true because there is no value assigned in variable \$a.

### Bitwise Operators

The Bitwise operator returns any numeric value. This operator works at bit level and rarely used in PHP programming. There are six kinds of Bitwise operators.

• Bitwise and (&)
• Bitwise or (|)
• Bitwise xor (^)
• Bitwise not (~)
• Left shift (<<)
• Right shift (>>)

You will get more details about Bitwise Operator in PHP manual page.

### Concatenate Operators

This operator is used to add two string values. The concatenate operator is denoted as dot (.). This is a frequently used operator in PHP programming. The following program is an example of concatenate operator.

```<?php
\$a="Tomas";
Echo "My name is ".\$a;
?>
```

So, the output will be “My name is Tomas”. Here, two strings are concatenated with dot (.) operator.

### Ternary Operator

Ternary operator is a special operator in PHP programming and used to make conditional statement. The structure of ternary operator is:

Expression ? true conditional statement : false conditional statement

If the expression is true, the true conditional statement will be executed otherwise the false conditional statement will be executed. More discussion on ternary operator will be on the PHP conditional statement explanation.

PHP Operators and Expression has been explained in this article. I hope, you have now a clear concept on PHP operators and expression. However, if you face any confusion to understand PHP Operators and Expression, feel free to discuss in comment or contact me from Contact page. I will try my best to stay with you. I am a system administrator and like to share knowledge that I am learning from my daily experience. I usually work on MikroTik, Redhat/CentOS Linux, Windows Server, physical server and storage, virtual technology and other system related topics. Web development is also my favorite topic and love to work with PHP, HTML, CSS, Tailwindcss, Bootstrap, JavaScript, jQuery, Vue.js, WordPress and Laravel framework. Follow Me: Facebook, Twitter and Linkedin.

## Laravel First Time Installation on Windows 10/11

Laravel is one of the best full stack frameworks used to develop modern web application simply and securely. Day by day PHP developers are absorbing Laravel. So,…

## PHP Delete Array Element with Built-in Function

Array is an important and mostly used data type in PHP programming. PHP programmer can manually add element to an array or delete element from an array…

## PHP Add Element to Array with Built-in Functions

Array plays an important role in PHP programming. It is one of the most used data structures in PHP programming. So, it is better to have a…

## PHP Array Explanation

Array is one of the most important and basic terms in PHP Language. Almost all the PHP program uses array data structure. So, those who want to…

## Function Call by Value and Call by Reference in PHP

More than thousands of built-in functions proves the power of PHP Language. Besides built-in function PHP allows to create user defined function. How to create user defined…

## PHP Variable Usage inside of Function

Besides more than thousands of built-in functions PHP provides facility to define custom/user defined function. How to define and call a user defined function was discussed in…

## This Post Has 18 Comments

1. Oakley Glasses says:

An impressive share, I simply given this onto a colleague who was doing slightly evaluation on this. And he the truth is purchased me breakfast as a result of I found it for him.. smile. So let me reword that: Thnx for the treat! But yeah Thnkx for spending the time to debate this, I feel strongly about it and love reading extra on this topic. If doable, as you turn out to be expertise, would you mind updating your blog with more details? It’s highly useful for me. Massive thumb up for this blog put up!

2. Cheap Oakley Sunglasses says:

very good put up, i actually love this website, carry on it

3. Michael Kors Outlet says:

I’m usually to blogging and i really appreciate your content. The article has actually peaks my interest. I am going to bookmark your website and preserve checking for new information.

4. Michael Kors Bags says:

A formidable share, I simply given this onto a colleague who was doing a bit evaluation on this. And he in reality bought me breakfast because I discovered it for him.. smile. So let me reword that: Thnx for the treat! But yeah Thnkx for spending the time to discuss this, I really feel strongly about it and love studying extra on this topic. If possible, as you turn into expertise, would you thoughts updating your blog with more particulars? It’s extremely helpful for me. Big thumb up for this weblog submit!

5. google plus age restriction says:

Wow that was unusual. I just wrote an very long comment
but after I clicked submit my comment didn’t show up.
Grrrr… well I’m not writing all that over again.
Anyway, just wanted to say excellent blog!

6. Cheap Oakley Sunglasses says:

The subsequent time I read a blog, I hope that it doesnt disappoint me as a lot as this one. I mean, I do know it was my option to learn, but I actually thought youd have one thing attention-grabbing to say. All I hear is a bunch of whining about one thing that you could repair if you werent too busy on the lookout for attention.

7. Michael Kors Handbags says:

WONDERFUL Post.thanks for share..more wait .. ?

8. I am really enjoying the theme/design of your web site.
Do you ever run into any web browser compatibility problems?

A small number of my blog visitors have complained about my website not
operating correctly in Explorer but looks great in Chrome.

Do you have any solutions to help fix this issue?

9. discount oakley sunglasses says:

Hello, I enjoy reading all of your article. I like to write a little comment to support you.

10. There is definately a lot to know about this subject.

I like all of the points you made.

11. google plus and facebook says:

Hi colleagues, how is the whole thing, and what you desire to
say on the topic of this paragraph, in my view its genuinely amazing in favor of me.

12. minecraft games says:

Peculiar article, exactly what I was looking for.

13. Cheap NFL Jerseys China says:

This website is really a stroll-by way of for all of the info you wished about this and didn抰 know who to ask. Glimpse right here, and also you抣l positively discover it.

14. Ratina says:

Nice start. Keep it up. It will help us to make website so easily.

15. Oakley Store says:

This actually answered my drawback, thanks!

16. Michael Kors Outlet says:

I抦 impressed, I have to say. Actually not often do I encounter a blog that抯 both educative and entertaining, and let me let you know, you may have hit the nail on the head. Your idea is excellent; the issue is something that not sufficient persons are talking intelligently about. I am very pleased that I stumbled throughout this in my seek for something relating to this.

17. Cheap NFL Jerseys says:

Aw, this was a very nice post. In concept I would like to put in writing like this moreover ?taking time and precise effort to make an excellent article?but what can I say?I procrastinate alot and not at all seem to get one thing done.

18. tadalafil 20 mg says:

Hello, I check your blog regularly. Your humoristic style is awesome, keep it
up!

This site uses Akismet to reduce spam. Learn how your comment data is processed.