PHP Operators

PHP Operators perform operations by taking one or more values, 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. The following are the operators in PHP:

  • Arithmetic Operators
  • Assignment Operators
  • Relational Operators
  • Logical Operators
  • Increment/ Decrement Operators
  • String Operators

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 the 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,

The following are the string operators:

OperatorNameExample
.Concatenation$str1 . $str2
.=Concatenation assignment$str1 .= $str2
PHP Constants
PHP Loops
Studyopedia Editorial Staff
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading