2 1

We have learned in the last tutorial, about Reference Variable in PHP. Now, we will discuss operators in PHP.

# PHP Operators:

Simple answer can be given using expression 2+ 5 is equal to 7. Here 2 and 5 are called operands and + is called operator. PHP language supports following type of operators.

### Types of Operators:

• Arithmetic Operators
• Comparison Operators
• Logical (or Relational) Operators
• Assignment Operators
• Incrementing and Decrementing Operators
• Conditional (or ternary) Operators

## Arithmetic Operators:

The arithmetic operators are used to perform common arithmetical operations, such as addition, subtraction, multiplication etc. Here’s a complete list of PHP’s arithmetic operators.

Operator
##### Result
+Addition\$x + \$ySum of \$x and \$y.
Subtraction\$x – \$yDifference of \$x and \$y.
*Multiplication\$x * \$yProduct of \$x and \$y.
/Division\$x / \$yQuotient of \$x and \$y.
%Modulus\$x % \$yRemainder of \$x divided by \$y.

## Example:

`<?php`
` \$x = 10;`
` \$y = 4;`
` echo(\$x + \$y);  // 0utputs: 14`
` echo(\$x - \$y);   // 0utputs: 6`
` echo(\$x * \$y);  // 0utputs: 40`
` echo(\$x / \$y);   // 0utputs: 2.5`
` echo(\$x % \$y);   // 0utputs: 2`
` ?>`

## Comparison Operators:

The comparison operators are used to compare two values in a Boolean form.

 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 < Less than \$x < \$y True if \$x is less than \$y > Greater than \$x > \$y True if \$x is greater 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:

`<?php`
` \$x = 25;`
` \$y = 35;`
` \$z = "25";`
` var_dump(\$x == \$z);        // Outputs: boolean true`
` var_dump(\$x === \$z);     // Outputs: boolean false`
` var_dump(\$x != \$y);        // Outputs: boolean true`
` var_dump(\$x !== \$z);     // Outputs: boolean true`
` var_dump(\$x < \$y);        // Outputs: boolean true`
` var_dump(\$x > \$y);       // Outputs: boolean false`
` var_dump(\$x <= \$y);   // Outputs: boolean true`
` var_dump(\$x >= \$y);   // Outputs: boolean false`
` ?>`

| Also Read | Introduction To PHP

## Logical Operators:

The logical operators are typically used to combine conditional statements.

 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

## Example

`<?php`
` \$year = 2014;`
` // Leap years are divisible by 400 or by 4 but not 100`
` if((\$year % 400 == 0) || ((\$year % 100 != 0) && (\$year % 4 == 0))){`
` echo "\$year is a leap year.";`
` } else{`
` echo "\$year is not a leap year.";`
` }`
` ?>`

## Assignment Operators:

The assignment operators are used to assign values to variables.

 Operator Description Example Is The Same As = Assign \$x = \$y \$x = \$y += Add and assign \$x += \$y \$x = \$x + \$y -= Subtract and assign \$x -= \$y \$x = \$x – \$y *= Multiply and assign \$x *= \$y \$x = \$x * \$y /= Divide and assign quotient \$x /= \$y \$x = \$x / \$y %= Divide and assign modulus \$x %= \$y \$x = \$x % \$y

## Example:

`<?php`
` \$x = 15;`
` echo \$x;  // Outputs: 15`

`\$x = 15;`
` \$x += 30;`
` echo \$x;  // Outputs: 45`

`\$x = 30;`
` \$x -= 20;`
` echo \$x;  // Outputs: 10`

`\$x = 5;`
` \$x *= 20;`
` echo \$x;  // Outputs: 100`

`\$x = 100;`
` \$x /= 20;`
` echo \$x;  // Outputs: 5`

`\$x = 100;`
` \$x %= 15;`
` echo \$x;  // Outputs: 10`
` ?>`

## Incrementing and Decrementing Operators:

 Operator Name Effect ++\$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

`<?php`
` \$x = 5;`
` echo ++\$x; // Outputs: 6`
` echo \$x; // Outputs: 6`

`\$x = 10;`
` echo \$x++; // Outputs: 10`
` echo \$x; // Outputs: 11`

`\$x = 15;`
` echo --\$x; // Outputs: 14`
` echo \$x; // Outputs: 14`

`\$x = 10;`
` echo \$x--; // Outputs: 10`
` echo \$x; // Outputs: 9`
` ?>`

## Conditional Operator:

There is one more operator called conditional operator. This first evaluates an expression for a true or false value and then execute one of the two given statements depending upon the result of the evaluation.

 Operator Description Example ? : Conditional Expression If Condition is true ? Then value X : Otherwise value Y

## Example

`<?php`
` \$a = 10;`
` \$b = 20;`

`/* If condition is true then assign a to result otheriwse b */`
` \$result = (\$a > \$b ) ? \$a :\$b;`

`echo "TEST1 : Value of result is \$result<br/>";`

`/* If condition is true then assign a to result otheriwse b */`
` \$result = (\$a < \$b ) ? \$a :\$b;`

`echo "TEST2 : Value of result is \$result<br/>";`
` ?>`

| Also Read | PHP Data Types

So this is for today, in our next tutorial we will learn about Control Statements(Decison Making).Stay connected with us for more tutorials,  keep visiting for more coming in this series . Feel free to like, comment ,share and give your opinions in comments below. Your valuable comments help us in giving you more relevant content . Be a part of Tekraze family, have a nice day.