Home  >  Article  >  Backend Development  >  PHP Operators

PHP Operators

王林
王林Original
2024-08-29 12:38:34962browse

Operators are symbols for mathematical calculations like addition, subtraction, and multiplication. PHP supports various operators to perform simple mathematical operations and logical operations like AND, OR, NOT, comparison operations like more significant than, less than, and many more. Operators are nothing that takes one or more values and yields another value.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Top 6 Types of PHP Operators

The different operators used in PHP are as follows:

1. Arithmetic PHP Operators

Like every programming language, PHP also supports Arithmetic operators, which perform simple arithmetical operations, such as addition, subtraction, division, multiplication, etc.

i) Addition Operator( + ): This operator is used to add two Values. Suppose X and Y are two Values; these plus operators will add up these two Values X + Y.

Syntax:

$x + $y

ii) Subtraction Operator( – ): This operator is used to subtracting two Values. Suppose X and Y are two Values; then this minus operator will subtract the value of the second value from the first value.

Syntax:

$x - $y

iii) Multiplication Operator( * ): This operator is used to multiply two Values. Suppose X and Y are two Values; then this multiplication operator will multiply X with Y.

Syntax:

$x * $y

iv) Division Operator( / ): This operator is used to numerator by the denominator. Suppose X and Y are two Values; this division operator divides the numerator by the denominator.

Syntax:

$x / $y

v) Modulus Operator( % ): This operator gives the remainder of the division. Suppose X and Y are two Values; this modulus operator divides the numerator by the denominator and gives the remainder.

Syntax:

$x % $y

vi) Exponentiation( ** ): This operator is used to raise one quantity to the power of another value. Suppose X and Y are two Values; then this exponentiation operator raises the value of X to the power Y.

Syntax:

$x ** $y

2. Assignment PHP Operators

We assign a value to a variable using assignment operators with numeric values. The basic assignment operator in PHP is =, which sets the value of the assignment expression on the right to the left value. Below is the list of Assignment operators used in PHP

  • Simple Assignment Operator( = ): This operator Assigns values from the right Values to the left value.
  • Add AND operator ( += ): This operator adds the right value to the left value and assigns the output to the left value.
  • Subtract AND operator ( -= ): This operator subtracts the right value from the left value and assigns the result to the left value.
  • Multiply AND Operator( *= ): This operator multiplies the right value with the left value and assigns the result to the left value.
  • Divide AND operator ( /= ): This operator divides the left value with the right value and assigns the result to the left value.
  • Modulus AND operator ( %= ): This operator takes modulus using two Values and assigns the result to the left value.

3. Comparison of PHP Operators

The PHP comparison operators are used to compare two values; those values can be numbers or strings.

i) Equal to( == ): This operator returns True if both the operands are equal.

Syntax:

$x == $y

ii) Identical( === ): This operator returns True if both the operands are equal and are of the same type.

Syntax:

$x === $y

iii) Not Identical( !== ): This operator returns True if both the operands are not equal and are of different types.

Syntax:

$x !== $y

iv) Not Equal( <> ): This operator returns True if both the operands are unequal.

Syntax:

$x <> $y

v) Not Equal( != ): This operator returns True if both the operands are unequal.

Syntax:

$x != $y

vi) Less Than( < ): This operator returns True if $x is less than $y.

Syntax:

$x < $y

vii) Greater Than( > ): This operator returns True if $x is greater than $y.

Syntax:

$x > $y

viii) Less Than or Equal To( <= ): This operator returns True if $x is less than or equal to $y.

Syntax:

$x <= $y

ix) Greater Than or Equal To( >= ):  This operator returns True if $x is greater than or equal to $y.

Syntax:

$x >= $y

4. Increment/Decrement PHP Operators

These are called the unary operators as it operates on single operands. These operators are used to increment or decrement values.

i) Pre-Increment( ++ ): This operator initially increments $x by one, then return $x.

Syntax:

++$x

ii) Pre-Decrement( — ): This operator initially decrements $x by one, then return $x.

Syntax:

--$x

iii) Post-Increment( ++ ): This operator First returns $x, then increments it by one.

Syntax:

$x++

iv) Pre-Decrement( — ): This operator first returns $x, then decrement it by one.

Syntax:

$x—

5. String PHP Operators

String Operators are implemented over strings.

i) Concatenation( . ): This operator Concatenates Two strings.

Syntax:

$text1.$text2

ii) Concatenation and assignment( .= ): This operator Appends two strings.

Syntax:

$text1.$text2

6. Logical PHP Operators

Logical operators are used to combine conditional statements.

i) AND: This operator returns true if both the operands are true; else returns false.

Syntax:

$x and $y

ii) OR: This operator returns true if either of the operands is true; else returns false.

Syntax:

$x or $y

iii) XOR: This operator returns true if either of the operands is true, and if both are true, then I will return false.

Syntax:

$x xor $y

iv) &&: This operator returns true if both the operands are true; else returns false.

Syntax:

$x && $y

v) NOT: This operator returns True if $x is false.

Syntax:

!$x

Conclusion

It plays a vital role in PHP when it comes to mathematical calculations. It also supports various operators like logical operators, string operators, etc.

The above is the detailed content of PHP Operators. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP BooleansNext article:PHP Booleans