Operator and operand collectively makes expression. Basically no PHP program is possible without operator and expression. So, a newbie PHP programmer should have proper knowledge on PHP operators and expression. The more we be expert on PHP operators, the more we be smart on PHP programming. So, there is no alternative to have strong knowledge on PHP operators if we want to be a smart PHP programmer. In my previous article I explained PHP data types and variable. In this article I will discuss PHP Operators and Expression elaborately.
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 on PHP operator.
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 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 Operator
- Increment and Decrement Operator
- Assignment Operator
- Comparison or Relational Operator
- Concatenation Operator
- Logical Operator
- Bitwise Operator and
- Ternary Operator
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 Operator
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 or Pre-decrement
- Post-increment or Post-decrement
Pre-increment or Pre-decrement
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 operator.
<?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 or Post-decrement
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 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 Operator
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 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.
Left Expression Right Expression Result 0 0 0 0 1 0 1 0 0 1 1 1
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.
Left Expression Right Expression Result 0 0 0 0 1 1 1 0 1 1 1 1
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.
Left Expression Right Expression Result 0 0 1 0 1 0 1 0 0 1 1 1
- 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.
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.
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 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 about 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 us page. I will try my best to stay with you.