Web Designing Tutorials

PHP Operators: What Are The Different Types?

Pinterest LinkedIn Tumblr
Since, you have learned about different PHP variables and their types in the previous post . You will learn here about PHP operators along with the different types of operators used in PHP. Let’s start with defining an operator, it is a symbol that specifies a particular action in an expression. You are already well known about some arithmetic operators such as +, -, *, / and %. In this tutorial, you will get more detail on all of the types of PHP operators. There is also another thing you should know that PHP’s automatic type conversion will convert data type based on the type of operator placed between two operators.

Types of PHP Operators

Following are the different types of PHP operators that are mostly used on the PHP expressions.

Arithmetic Operators

The most known operators are the arithmetic operators that includes addition, subtraction, concatenation, division, multiplication and modulus operator. These operators are most frequently and easily used. PHP also includes vast list of mathematical functions that can perform base conversion and calculating logarithms, square roots, geometric values and more.

Assignment Operators

The main function of the assignment operators is to assign data value to the variable. The simplest assignment operator is equal sign (=) that just assigns some value. While the other assignment operators are shortcut assignment operators that make other operations before assignment operators are addition assignment (+=), multiplication assignment (*=), division assignment (/=) and concatenation assignment (.=) operators.

<?php
$num=10;
$new_num=$num;
echo “Simple Assignment Operator $new_num <br/>”;
$num+=5;
echo “Addition Assignment Operator $num <br/>”;
$num*=2;
echo “Multiplication Assignment Operator $num <br/>”;
$num/=3;
echo “Division Assignment Operator $num <br/>”;
$num.=5;
echo “Concatenation Assignment Operator $num <br/>”;
?>

Output:

Simple Assignment Operator 10
Addition Assignment Operator 15
Multiplication Assignment Operator 30
Division Assignment Operator 10
Concatenation Assignment Operator 105

String Operators

String operators in PHP provides two operators for concatenating the strings. Since, the first operator (.) called concatenation operator that concatenates two strings together. Whereas, the second operator (.=) called concatenation assignment operator that assigns the value after concatenating.

Here is an example showing the implementation of concatenation operator in order to concatenate two stings in PHP.

<?php

$lang1=”JavaScript”;

$lang2=”PHP”;

$lang=$lang1.” & “.$lang2;

echo $lang;

$out.=”are programming languages”;

echo $out;

?>

Output:

JavaScript & PHP

Increment and Decrement Operators

There are another sets of operators that can increase or decrease the value of the variable called increment (++) and decrement operators (–). The increment operator(++) increments the value by 1 whereas the decrement(–) operator decrements the value by 1.

In PHP, you can place these increment and decrement operators either side of the variables. It will provides slightly different effect while assigning the value with increment or decrement operators.

In the following example a number is assigned to an integer variable and incremented the value after assigning to the first variable and the value of the variable assigned to the second variable after decreasing the value of an integer.

<?php

$int=45; // Assigns the integer value 45 to \$int

$int1=++$int;

$int2=$int–;

echo “The value of first integer is $int1<br/>”;

echo “The value of second integer is $int2<br/>”;

?>

Output:

The value of first integer is 46
The value of second integer is 46

Logical Operators

Since the logical operators provides the way to make decisions based on the values of multiple variables. These operators are most useful while crating PHP applications. So, it is possible to direct the flow of program with logical operators. Logical operators are mostly used with control structures such as if condition, while and for loop. Following are the list of logical operators in PHP and their uses.

  • AND, && → And operators → True if both of the variables are True.
  • OR, || → OR operators → True if at least one variable is True.
  • NOT, ! → Not operator → True if the variable is not True.
  • XOR → Exclusive OR → True if only one of the variable is True.

Following is an example that shows the uses of different logical operators used in PHP programming.

<?php

$a=5;

$b=6;

$c=7;

if (($a<$b) && ($b<$c)) {

echo “$c is the greatest number<br/>”;

}

if ($a=5 || $b=5) {

echo “Either a of b is equal to 5<br/>”;

}

if ($a!==$b) {

echo “a and b are not equal<br/>”;

}

?>

Output:

7 is the greatest number
Either a or b is equal to 5
a and b are not equal

Equality Operators

Equality operators compare two values or the value of the variables for testing their equivalency. The most commonly used equality operators that are used on PHP are as follows.

  • Is equal to (= =) operator
  • Is not equal to (!=) operator
  • Is identical to (= = =) operator

The difference between (= =) and (= = =) operators is that (= = =) needs to have the variables same type in addition with equality.

Following example shows the difference between “is equal to” and “is identical to” operators.

<?php

$a=50;

$b=”50″;

if ($a==$b){

echo “a and b are equal<br/>”;

}

else {

echo “a and b are not equal<br/>”;

}

if ($a===$b) {

echo “a and b are equal<br/>”;

}

else {

echo “a and b are not equal<br/>”;

}

?>

Output:

a and b are equal
a and b are not equal

Comparison Operators

Since, logical operator provides the way to make decisions, comparison operator provides a method to direct program flow. Comparison operator compares the two or more values of the variables. Some of the most commonly used comparison operators are less than (<), greater than (>), less than or equal to (<=), greater than or equal to (>=) and ternary operator (? :).

Here is an example showing how comparison operators are used in PHP.

<?php

$x=19;

$y=21;

($x<=$y) ? $z=$y-$x : $z=$x-$y;

echo “The value of z is $z”;

?>

Output:

The value of z is 2

Bitwise Operators

Unlike to the other types of PHP operators, Bitwise operators examine and manipulate integer values in the level of individual bits that make up the integer values. Since these types of operators converts integer numbers to binary numbers and performs logical operations then finally display them in integer numbers, thus the name bitwise operators. AND(&), OR(|), XOR(^), NOT(~), Shift left(<<) and Shift right(>>) are the most commonly used bitwise operators.

Here is an example that shows the uses of PHP bitwise AND(&) operator which displays the output of bitwise AND operation of two variables $a and $b.

<?php

$a=66;

$b=88;

$c=$a&$b;

echo “Bitwise AND on a and b is $c”;

?>

Output:

Bitwise AND on a and b is 64

Read Next:What Conditional Statements Used In PHP

Article Sponsor: online slots Canada

Please follow and like us:
RSS
Follow by Email
X (Twitter)
Visit Us
Follow Me
YouTube
YouTube
LinkedIn
linkedIn | PHP Operators: What Are The Different Types?
Share

Author

Shuseel Baral is a web programmer and the founder of InfoTechSite has over 8 years of experience in software development, internet, SEO, blogging and marketing digital products and services is passionate about exceeding your expectations.

Comments are closed.

RSS
Follow by Email
X (Twitter)
Visit Us
Follow Me
YouTube
YouTube
LinkedIn
Share