i

PHP Tutorial

PHP Operators

Operators are the symbols that take values called operands and yield a result or another value. In Php, operators perform certain operations on values and variables and are classified into below categories.

1. Arithmetic operators – These operators perform mathematical calculations such as addition, subtraction, multiplication etc. In PHP we have +, -, *, /, %, ** for addition, subtraction, multiplication, division, modulus and exponentiation respectively.

2. Assignment operators – These operators write a value to any variable, and the most common assignment operator is “=” that assigns the right value of the expression to left operand. In Php, we have =, +=, -=, *=, /=, %= that assigns the value after the arithmetic operation written before “=”.

3. Increment/Decrement operators – In PHP, increment operators are used to increment the value of a variable and decrement operator is used to decrement the variable value. We have four operators ++$x for pre-increment, $x++ for post-increment, --$x for pre-decrement, and $x-- for post-decrement.

4. Comparison operators - These operators are used to compare two values and we have == for equal, != and <> for not equal, === for identical, !== for non-identical, > for greater than, < for less than, >= for greater than equal to, <= for less than equal to, and < = > for spaceship that returns a value less than, equal to or greater than 0.

5. Logical operators – These operators help to combine conditional statements in Php, and we have and, or, xor, &&, ||, ! for and, Or, Xor, and, or, not operations respectively.

6. Arrays operators – To compare arrays, we use array operators in PHP. In php, + is for union, == for equality, === for identity, != and <> for inequality, and !== for non-identity array operations.

7. String operators – For strings, php has two special operators ‘.’ and ‘.=’. ‘.’ mean concatenation and ‘.=’ is for concatenation assignment that appends right value to left value. 

8. Conditional assignment operators – These operators are used to set any value in php based on some condition. In Php we have two operators ?: for ternary e.g. $x= 20>2?”Y”:” N” returns Y if the expression is true and N if false. Another operator is ?? for null coalescing. e.g. $y= $y??” no value”; if y has a value, it will show the value else no value.