PHP

# PHP Operators

PHP Supports following Operators:

Each Operators are Explained below:

## PHP Arithmetic Operators

Operator Name Example Result
+ Addition \$x + \$y Sum of \$x and \$y
- Subtraction \$x - \$y Difference of \$x and \$y
* Multiplication \$x * \$y Product of \$x and \$y
/ Division \$x / \$y Quotient of \$x and \$y
% Modulus \$x % \$y Remainder of \$x divided by \$y

### Example:

The following example shows the different results of using the different arithmetic operators:

``````<?php
\$n1 = 10;
\$n2 = 5;

\$sub = \$n1 - \$n2;
echo "\$n1 - \$n2 = \$sub Subtraction<br />";

\$mul = \$n1 * \$n2;
echo "\$n1 * \$n2 = \$mul Multiplication<br />";

\$div = \$n1 / \$n2;
echo "\$n1 / \$n2 = \$div Division<br />";

\$mod = \$n1 % \$n2;
echo "\$n1 % \$n2 = \$mod Remainder of Division";
?>``````

### Output:

Tutorialik.com
10 + 5 = 15 Addition
10 - 5 = 5 Subtraction
10 * 5 = 50 Multiplication
10 / 5 = 2 Division
10 % 5 = 0 Remainder of Division

## PHP Logical Operators

Operator Name Example Result
and And \$x and \$y True if both \$x and \$y are true
or Or \$x or \$y True if either \$x or \$y is true
xor Xor \$x xor \$y True if either \$x or \$y is true, but not both
&& And \$x && \$y True if both \$x and \$y are true
|| Or \$x || \$y True if either \$x or \$y is true
! Not !\$x True if \$x is not true

## PHP Bitwise Operators

The bitwise operators enable you to treat an integer as the series of bits used to represent it.
You probably will not find a lot of use for the bitwise operators in PHP, but a summary is shown in following Table

Operator Name Example Result
& Bitwise AND \$a & \$b Bits set in \$a and \$b are set in result.
| Bitwise OP \$a | \$b Bits set in \$a or \$a are set in the result.
~ Bitwise NOT ~\$a Bits set in \$a are not set in the result and vice versa.
^ Bitwise Ex-OR (XOR) \$a ^ \$b Bits set in \$a or \$b but not in both are set in the result.
<< Left Shift \$a << \$b Shifts \$a left \$b bits.
>> Right Shift \$a >> \$b Sifts \$a right \$b bits.

## PHP Assignment Operators

The PHP assignment operators is used to write a value to a variable.

The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the assignment expression on the right.

 Assignment Short Hand Description \$x = \$y \$x = \$y The left operand gets set to the value of the expression on the right \$x = \$x + \$y \$x += \$y Addition \$x = \$x - \$y \$x -= \$y Subtraction \$x = \$x * \$y \$x *= \$y Multiplication \$x = \$x / \$y \$x /= \$y Division \$x = \$x % \$y \$x %= \$y Modulus

### Example:

The following example shows  the different results of using the different assignment operators:

``````<?php
\$x = 10;
\$y = 5;

echo \$x.' Initial Value \$x<br />';
echo \$y.' Initial Value \$y<br />';

/*Assign Value of \$y to \$x*/
\$x = \$y;
echo 'Value of \$x is '.\$x.' After Assigning \$y<br />';

/*Add Value of \$y to Existing Value of \$x, and New Value is Assign to \$x*/
\$x += \$y;
echo 'Value of \$x is '.\$x.' After \$x += \$y;<br />';

/*Subtract Value of \$y from Existing Value of \$x, and New Value is Assign to \$x*/
\$x -= \$y;
echo 'Value of \$x is '.\$x.' After \$x -= \$y;<br />';

/*Multiply Value of \$y by Existing Value of \$x, and New Value is Assign to \$x*/
\$x *= \$y;
echo 'Value of \$x is '.\$x.' After \$x *= \$y;<br />';

/*Divide Value of \$y from Existing Value of \$x, and New Value is Assign to \$x*/
\$x /= \$y;
echo 'Value of \$x is '.\$x.' After \$x /= \$y;<br />';

/*Divide Value of \$y from Existing Value of \$x, and Reminder is Assign to \$x*/
\$x %= \$y;
echo 'Value of \$x is '.\$x.' After \$x %= \$y;<br />';
?>``````

### Output:

Tutorialik.com
10 Initial Value \$x
5 Initial Value \$y
Value of \$x is 5 After Assigning \$y
Value of \$x is 10 After \$x += \$y;
Value of \$x is 5 After \$x -= \$y;
Value of \$x is 25 After \$x *= \$y;
Value of \$x is 5 After \$x /= \$y;
Value of \$x is 0 After \$x %= \$y;

## PHP String Operators

There are two types of 'String Operators': The Concatenating Operator (`.`) and the Concatenating Assignment Operator (`.=`).

Operator Name Example Result
`.` Concatenation \$txt1 = "Hello"
\$txt2 = \$txt1 `.` " PHP"
Now \$txt2 contains "Hello PHP"
`.=` Concatenation assignment \$txt1 = "Hello"
\$txt1 `.=` " PHP"
Now \$txt1 contains "Hello PHP"

### Example:

The following example shows the results of using the string operators:

``````<?php
\$str1 = "Hello";
\$str2 = \$str1 . " PHP"; //Concatenation using .
echo \$str2;
echo "<br />";
\$str3 = "Hello";
\$str3 .= " PHP";        //Concatenation using .=
echo \$str3;
?>``````

Tutorialik.com
Hello PHP
Hello PHP

## PHP Increment / Decrement Operators

Operator Name Description
++\$x Pre-increment Increments \$x by one, then returns \$x
\$x++ Post-increment Returns \$x, then increments \$x by one
--\$x Pre-decrement Decrements \$x by one, then returns \$x
\$x-- Post-decrement Returns \$x, then decrements \$x by one

### Example:

The following example shows the different results of using the different increment/decrement operators:

``````<?php
\$x = 5;
//Increment \$x by 1, Returns \$x which will 6
echo ++\$x.' Pre-Increment<br />';

\$y = 5;
//Returns \$x which is 5, then increments \$x by one
echo \$y++. ' Post-Increment<br />';

\$x = 7;
//Decrements \$x by one, then Returns \$x which will 6
echo --\$x.' Pre-Decrement<br />';

\$y = 7;
//Returns \$x which is 7, then decrements \$x by one
echo \$y--.' Post-Decrement';
?>``````

Tutorialik.com
6 Pre-Increment
5 Post-Increment
6 Pre-Decrement
7 Post-Decrement

## PHP Comparison Operators

The PHP comparison operators are used to compare two values (number or string):

Operator Name Example Result
== Equal \$x == \$y True if \$x is equal to \$y
=== Identical \$x === \$y True if \$x is equal to \$y, and they are of the same type
!= Not equal \$x != \$y True if \$x is not equal to \$y
<> Not equal \$x <> \$y True if \$x is not equal to \$y
!== Not identical \$x !== \$y True if \$x is not equal to \$y, or they are not of the same type
> Greater than \$x > \$y True if \$x is greater than \$y
< Less than \$x < \$y True if \$x is less than \$y
>= Greater than or equal to \$x >= \$y True if \$x is greater than or equal to \$y
<= Less than or equal to \$x <= \$y True if \$x is less than or equal to \$y

### Example:

The following example shows the different results of using some of the comparison operators:

``````<?php
\$x = 50;
\$y = "50";
echo '\$x is ';
echo var_dump(\$x).'<br />';
echo '\$y is ';
echo var_dump(\$y).'<br /><br />';

var_dump(\$x == \$y);
echo ' \$x == \$y <br />';

var_dump(\$x === \$y);
echo ' \$x === \$y<br />';

var_dump(\$x != \$y);
echo ' \$x != \$y<br />';
var_dump(\$x !== \$y);
echo ' \$x !== \$y<br />';
echo '<br />';

\$a = 50;
\$b = 70;
echo '\$a is ';
echo var_dump(\$a).'<br />';
echo '\$b is ';
echo var_dump(\$b).'<br /><br />';

var_dump(\$a > \$b);
echo ' \$a > \$b <br />';
var_dump(\$a < \$b);
echo ' \$a < \$b <br />';
?>``````

### Output:

Tutorialik.com
\$x is int(50)
\$y is string(2) "50"

bool(true) \$x == \$y
bool(false) \$x === \$y
bool(false) \$x != \$y
bool(true) \$x !== \$y

\$a is int(50)
\$b is int(70)

bool(false) \$a > \$b
bool(true) \$a < \$b