# PHP Operators

There are three types of operators. Firstly there is the unary operator which operates on only one value, for example ! (the negation operator) or ++ (the increment operator). The second group are termed binary operators; this group contains most of the operators that PHP supports.The third group is the ternary operator: ?:. It should be used to select between two expressions depending on a third one, rather than to select two sentences or paths of execution. Surrounding ternary expressions with parentheses is a very good idea.

The most common PHP operators are assignment operators, arithmetic operators, combined operators, comparison operators, and logical operators. Each type is discussed separately below.

Arithmetic Operators

The basic assignment operator in PHP is “=”. This means that the operand to the left of “=” gets set to the value to the right of “=”.

Arithmetic Operators

 Operator Description Example Result + Addition \$x=2+2 4 – Subtraction x=2 5-x 3 * Multiplication x=4 x*5 20 / Division 15/5 5/2 3 2.5 % Modulus (division remainder) 5%2 10%8 10%2 1 2 0

Assignment Operators

 Operator Example Is The Same As = x=y x=y += x+=y x=x+y -= x-=y x=x-y *= x*=y x=x*y /= x/=y x=x/y .= x.=y x=x.y %= x%=y x=x%y

Comparison Operators

 Operator Description Example == is equal to 5==8 returns false != is not equal 5!=8 returns true > is greater than 5>8 returns false < is less than 5<8 returns true >= is greater than or equal to 5>=8 returns false <= is less than or equal to 5<=8 returns true

Logical Operators

 Operator Description Example && and x=6 y=3 (x < 10 && y > 1) returns true || or x=6 y=3 (x==5 || y==5) returns false ! not x=6 y=3 !(x==y) returns true

PHP Unary Operators

The following table outlines the various forms of pre and post increment and decrement operators, together with examples that show how the equivalent task would need to be performed without the increment and decrement operators.

 Operator Type Description Equivalent ++\$var Preincrement Increments the variable value before it is used in rest of expression \$var = 10; \$var2 = \$var + 1; –\$var Predecrement Decrements the variable value before it is used in rest of expression \$var = 10; \$var2 = \$var – 1; \$var++ Postincrement Increments the variable value after it is used in rest of expression \$var = 10; \$var2 = \$var; \$var = \$var + 1; \$var– Postdecrement Decrements the variable value after it is used in rest of expression \$var = 10; \$var2 = \$var; \$var = \$var – 1;

PHP String Concatenation Operator

Working Example

Given below is a PHP script that demonstrates use of some basic PHP operators:

```<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<TITLE> PHP Operators </TITLE>

<BODY>
<h2>PHP Operators </h2>
<HR>
<FIELDSET>
<LEGEND>
<span style="background-color: #3399CC; color:
#F7F7F7;font-weight: bold">
Arithmetic Operators Example</span></LEGEND>
<pre>
<?php
#\$x,\$y,\$result;
\$x=10;
\$y=20;
\$result=\$x+\$y;
//substraction operator
echo "\$x + \$y =\$result";
\$result=\$x-\$y;
echo "<br>\$x - \$y =\$result";
//Multiplication operator
\$result=\$x*\$y;
echo "<br>\$x * \$y =\$result";
//division operator
\$result=\$x/\$y;
echo "<br>\$x / \$y =\$result";
\$result=\$x%\$y;
echo "<br>\$x % \$y =\$result";

?>
</pre>
</FIELDSET><BR>
<FIELDSET>
<LEGEND><span style="background-color: #3399CC;

color: #F7F7F7;font-weight: bold">A
ssignment Operator EXAMPLE </span></LEGEND>
<pre>
<?php
#\$x,\$y,\$result;
\$x=10;
\$y=20;

echo "\$x +=\$y:   ";
\$x+=\$y;
echo "\$x";

//substraction operator
echo "<br>\$x -=\$y:   ";
\$x-=\$y;
echo "\$x";
//Multiplication operator

echo "<br>\$x *=\$y   :";

echo "<br>\$x /=\$y: ". (\$x/=\$y);
//division operator
#\$result=\$x/\$y;

?>
</pre>
</FIELDSET></BODY>
</HTML>
```

Type the above code in the editor and save it as operator.php and now open the PHP script in the browser to view the output: Try the other operators yourself.

Scroll to Top