# PHP Operators

- In programming and logic, an operator symbol is used to perform an operation on some value such as addition, substraction, and so on
- Operators assign values, works with strings and numbers and can control program flow

## Types of Operators

- Assignment
- Math (Same as when you learned math in grade school)
- String
- Comparison
- Logical

## Assignment Operators

The Assignment Operators ( **=** ) sets the value to the left of the operand to the value on the right

You can also combine it with other operators

```
<?php
//Combined Operators //Same as
$a=$b; $a=$b; //The left operand gets set to the value of the expression on the right
$a+=$b; $a=$a+$b; //Addition
$a-=$b; $a=$a-$b; //Substraction
$a*=$b; $a=$a*$b; //Multiplication
$a/=$b; $a=$a/$b; //Division
$a%=$b; $a=$a%$b; //Modulus
$a.=$b; $a=$a.$b; //Concatenate
?>
```

#### Concatenate Operator is Often used to create output

TIP: Do not think of = as “equal” … think of it as “assignment”

TIP: Think of == as “equal”

## Math Operators

The PHP math/arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc.

Examples | Name | Result |
---|---|---|

-$a | Negative | Negative Value of $a |

$a + $b | Addition | Sum of $a and $b |

$a – $b | Subtraction | Difference of $a and $b |

$a * $b | Multiplication | Product of $a and $b |

$a / $b | Division | Quotient of $a and $b |

$a % $b | Modulus | Remainder of $a divided by $b |

$a ** $b | Exponentiation | Result of raising $a to the $b’th power |

**TIP**: The division operator(“/”) return a float value anytime, even if the two operands are integers (or strings that get converted to integers)

There are also increment/decrement operators ++ and —**Note**: $a++ is equal to $a+1

## String Operators

PHP has two operators that are specially designed for strings.

The concatenation operator (.) allows you to combine two strings together into a single string

Examples | Name | Result |
---|---|---|

$txt1 . $txt2 | Concatenation | Concatenation of $txt1 and $txt2 |

$txt1 .= $txt2 | Concatenation assignment | Appends $txt2 to $txt1 |

for example, both these lines of code have the same result

```
<?php
echo "Hello "."World";
echo "Hello World";
?>
```

## Output:

```
Hello World
Hello World
```

## Comparison Operators

If you compare an integer with a string, the string is converted to a number

If you compare two numerical strings, they are compared as integers

Examples | Name |
---|---|

$a==$b | equal |

$a===$b | Identical (same value and type) |

$a!=$b | Not equal |

$a<>$b | Not equal (numerical) |

$a!==$b | Not Identical (values and types not the same) |

$a<$b | Less than |

$a>$b | Greater than |

$a<=$b | Less than or equal to |

$a>=$b | Greater than or equal to |

$a <=> $b | Spaceship(Returns an integer less than, equal to, or greater than zero, depending on if $a is less than, equal to, or greater than $b. Introduced in PHP 7.) |

**Note**: “Not identical” values and types are not the same

## Logical Operators

**You are already be femiliar with some of these operators**

- and (“and” and && symbols are both supported)
- or (“or” and || symbols are both supported)
- xor (“xor” and ^ symbols are both supported)
- not (“not” and ! symbols are both supported)

**Why are two symbols that do same thing?**

- Order Precedence -it depends upon which symbol you use
- For Example- and or && – the entire statement may evaluate to a different result
- The reason for the two difference variation of “and” and “or” operators is that they operate at different precedence
- Be sure to define xor: $a xor $b if either is true but not both

Hi my friend! I wish to say that this post is awesome, nice written and include approximately all significant infos. I would like to see more posts like this.

I’m extremely impressed along with your writing talents and also with the structure to your blog. Is that this a paid subject or did you modify it your self? Either way keep up the nice quality writing, it is uncommon to peer a great blog like this one today..

Good write-up, I?¦m regular visitor of one?¦s blog, maintain up the nice operate, and It is going to be a regular visitor for a long time.

Heya i’m for the first time here. I came across this board and I in finding It truly helpful & it helped me out a lot. I hope to offer something again and help others like you helped me.