Php – Operators – POFTUT

# Php – Operators

[rps-include post=6522]

We have almost learned the variables and related types. But just variables do not enough to create useful applications. We generally need to operate over these variables and data. Operators are a way to operate on variable and data. For example if we need to sum two integer variables we should use sum operator. Let’s start looking available operators. We groups operators with the following group names.

• Arithmetic Operators
• Assignment Operators
• Comparison Operators
• Increment Operators
• Logical Operators
• String Operators
• Array Operators

# Arithmetic Operators

Arithmetic or Mathematical operations can be done with arithmetic operators. Here list of arithmetic operators.

## + Sum Operator

Sum operator is used to sum two variables or values. In this example `\$result` will be 7.

```\$x = 5; \$y = 2;

\$result = \$x + \$y;```

## – Subtract Operator

Subtract operator is used to subtract two variables or values. In this example `\$result` will be 3 .

```\$x = 5; \$y = 2;

\$result = \$x - \$y;```

## * Multiply Operator

Multiply operator is used to multiply two variables or values. In this example `\$result` will be 10 .

```\$x = 5; \$y = 2;

\$result = \$x * \$y;```

## / Divide Operator

Divide operator is used to divide two variables or values. In this example `\$result` will be 2.5 . The `\$result` type will be a float number.

```\$x = 5; \$y = 2;

\$result = \$x / \$y;```

## % Module Operator

Module operator is used to get remainder two variables or values. In this example `\$result` will be 1 .

```\$x = 5; \$y = 2;

\$result = \$x % \$y;```

# Assignment Operators

Assignment operators generally provide same functionality of Arithmetic operators with a bit different way. Assignment operators uses first value or variable to provide value and store result.

## = Assign

Assignment will set variables value to the given.

```\$a = "This is string";

\$b = 5;```

Add and assign operator will add left and right variables or values and set the result to the left variable. In this example `\$x` will be 7 .

```\$x = 5; \$y = 2;

\$x += \$y;```

## -= Subtract and Assign

Subtract and assign operator will subtract left and right variables or values and set the result to the left variable. In this example `\$x` will be 3 .

```\$x = 5; \$y = 2;

\$x -= \$y;```

## *= Multiply and Assign

Multiply and assign operator will multiply left and right variables or values and set the result to the left variable. In this example `\$x` will be 10 .

```\$x = 5; \$y = 2;

\$x *= \$y;```

## /= Divide and Assign

Divide and assign operator will divide left and right variables or values and set the result to the left variable. In this example `\$x` will be 7 .

```\$x = 5; \$y = 2;

\$x /= \$y;```

## %= Divide and Assign Modulus

Divide and assign operator will divide left and right variables or values and set the remainder to the left variable. In this example `\$x` will be 7 .

```\$x = 5; \$y = 2;

\$x %= \$y;```

# Comparison Operators

Comparison operators generally used to compare two values or variables. These operators generally used to return some boolean result like `true` and `false`  and used generally with conditional keywords like `if` .

## == Equal

Equal is used simply check whether given two values or variables are equal. If they are same this will return true if not this will return false.

```\$x = 5;
\$y = 10;
\$z = "5";

(\$x == \$y); //Returns false
(\$x == \$z); //Returns true
```

## === Identical

Equal is used simply check whether given two values or variables are identical. If they are identical this will return true if not this will return false.

```\$x = 5;
\$y = 10;
\$z = "5";

(\$x === \$y); //Returns false
(\$x === \$z); //Returns false```

## != or <> Not Equal

Not equal is used simply check whether given two values or variables are not equal. If they are same this will return false if not this will return true.

```\$x = 5;
\$y = 10;
\$z = "5";

(\$x <> \$y); //Returns false
(\$x != \$z); //Returns true```

## !== Not Identical

Not identical is used simply check whether given two values or variables are identical. If they are identical this will return false if not this will return true.

```\$x = 5;
\$y = 10;
\$z = "5";

(\$x !== \$y); //Returns true
(\$x !== \$z); //Returns true```

## < Less Than

Less than is used simply check whether first variables or values is lesser than second one.  If first variables is lesser than second one this will return true if not false.

```\$x = 5;
\$y = 10;

(\$x < \$y); //Returns true
(\$y < \$x); //Returns false```

## > Greater Than

Greater than is used simply check whether first variables or values is greater than second one.  If first variables is greater than second one this will return true if not false.

```\$x = 5;
\$y = 10;

(\$x > \$y); //Returns false
(\$y > \$x); //Returns true```

## >= Greater Than Or Equal To

Greater than  or equal to is used simply check whether first variables or values is greater or equal to than second one.  If first variables is greater or equal to than second one this will return true if not false.

```\$x = 5;
\$y = 10;

(\$x >= \$y); //Returns false
(\$y >= \$x); //Returns true```

## <= Less Than or Equal To

Less than  or equal to is used simply check whether first variables or values is less or equal to than second one.  If first variables is less or equal to than second one this will return true if not false.

```\$x = 5;
\$y = 10;

(\$x <= \$y); //Returns true
(\$y <= \$x); //Returns false```

# Increment Operators

Increment operators generally used as mathematical operations.

## ++\$x Pre-increment

We can use pre-increment operator to increment single step the variable value before using this variable. In this example `\$x` before  value is 6 and after value is 6 .

```\$x=5;

echo \$x; //Output is 5

echo ++\$x;//Output is 6

echo \$x; //Output 6```

## \$x++ Post-increment

We can use post-increment operator to increment single step the variable value after using this variable. In this example `\$x` before  value is 5 and after value is 6 .

```\$x=5;

echo \$x; //Output is 5

echo \$x++;//Output is 5

echo \$x; //Output 6```

## –\$x Pre-decrement

We can use pre-decrement operator to decrement single step the variable value before using this variable. In this example `\$x` before  value is 4 and after value is 4 .

```\$x=5;

echo \$x; //Output is 5

echo --\$x;//Output is 4

echo \$x; //Output 4```

## \$x– Post-decrement

We can use post-decrement operator to decrement single step the variable value after using this variable. In this example `\$x` before  value is 5 and after value is 64.

```\$x=5;

echo \$x; //Output is 5

echo \$x--;//Output is 5

echo \$x; //Output 4```

# Logical Operators

Local operators generally used with boolean values and variables.

## and , && Operator

`and` operator is used like below. If two values or variables are true this will return true if not it will return false. `&&` can be used as and operator too.

```\$x = true; \$y =true; \$z = false

(\$x && \$y); //Returns true

(\$x and \$z); //Returns false```

## or , || Operator

`or` operator is used like below. If two values or variables are false this will return false if not it will return true. || can be used as and operator too.

```\$x = true; \$y =true; \$z = false

(\$x || \$y); //Returns true

(\$x or \$z); //Returns true```

## xor  Operator

`xor` operator is used like below. If two values or variables are same boolean value this will return true if not will return false.

```\$x = true; \$y =true; \$z = false

(\$x xor \$y); //Returns true

(\$x xor \$z); //Returns false

(\$z xor \$z); //Returns true```

## ! Not Operator

Not operator simply return the reverse boolean value of the given variable or value. If given variable of value is true result will be false. If the given value is false result will be true.

```\$x = true; \$y =true; \$z = false

!\$x; //Returns false
!\$z; //Returns true```

# String Operators

String operators mainly used to concatenate given string variables or values.

## . Concatenate

We can concatenate two string variable of value with `.` like below. In this example we will concatenate two string variable named `\$a` and `\$b` and result will be assigned to the `\$result` .

```\$a="This is";

\$b="string";

\$result = \$a . \$b; //Result will be "This is string"```

## .= Append

We can concatenate two string variable of value with `.` like below. In this example we will concatenate two string variable named `\$a` and `\$b` and result will be assigned to the `\$a` .

```\$a="This is";

\$b="string";

\$a .= \$b; //\$a will be "This is string"```

[rps-include post=6522]