PHP Operators

PHP Operators

PHP Operators perform operations by taking one or more value, to give another value. These operations are performed on variables and values.

For example:  $a + $b,  $a – $b, $a = $b

The types of operators are explained here with an example, where $a = 5, $b =10. Here are the operators in PHP:

PHP Arithmetic operators

Arithmetic operators perform arithmetical operations, such as addition, subtraction, division, multiplication etc.

OperatorNameExample
+Addition$a + $b
-Subtraction$a - $b
*Multiplication$a * $b
/Division$a / $b
%Modulus$a % $b
**Exponentiation$a ** $b

PHP Assignment operators

Use the Assignment Operator when you need to assign a value to a variable. Assignment means assigning the value of right operand(s) to the left operand.

OperatorNameExample
=Assignment$a = $b
+=Add AND assignment $a += $b is same as $a = $a + $b
-=Subtract AND assignment$a -= $b is same as $a = $a - $b
*=Multiply AND assignment$a *= $b is same as $a = $a * $b
/=Divide AND assignment$a /= $b is same as $a = $a / $b
%=Modulus AND assignment$a % $b is same as $a = $a % $b

PHP Relational operators

Compare two values with PHP relational operators.

OperatorNameExample
==Equal$a == $b isn’t true
!=Not Equal$a != $b returns true
Greater Than$a > $b isn’t true
Less Than$a < $b is true
>=Greater than or equal to$a >= $b isn’t true
<=Less than or equal to$a <= $b is true

PHP Logical operators

Logical operators combine conditional statements.

OperatorNameExample
AndLogical AND operator$a and $b is true if both $a and $b are true
OrLogical OR operator$a or $b is true either $a or $b is true
XorLogical XOR operator$a xor $b is true if either $a or $b is true (not both)
&&Logical AND operator$a && $b is true if $a and $b are both true
||Logical OR Operator$a || $b is true if either $a or $b is true
!Logical NOT Operator!$a is true if $a is not true

PHP Increment/ Decrement operators

Increment and decrement a variable’s value with Increment and Decrement operators respectively.

OperatorNameExample
++$aIncrement (Pre)$a increments, then returns
$a++Increment (Post)Returns $a, then increments
--$aDecrement (Pre)$a decrements, then returns
$a--Decrement (Post)Returns $a, then decrements

PHP String operators

Here are the operators for strings. Let’s say, we have the following two strings,

OperatorNameExample
.Concatenation$str1 . $str2
.=Concatenation assignment$str1 .= $str2

PHP Constants
PHP Loops
Studyopedia Editorial Staff
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create free tutorials for all.

No Comments

Post A Comment