PHP Operators

王林
Release: 2024-08-29 12:38:34
Original
1129 people have browsed it

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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

Syntax:

$x === $y
Copy after login

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

Syntax:

$x !== $y
Copy after login

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

Syntax:

$x <> $y
Copy after login

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

Syntax:

$x != $y
Copy after login

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

Syntax:

$x < $y
Copy after login

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

Syntax:

$x > $y
Copy after login

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

Syntax:

$x <= $y
Copy after login

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

Syntax:

$x >= $y
Copy after login

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
Copy after login

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

Syntax:

--$x
Copy after login

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

Syntax:

$x++
Copy after login

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

Syntax:

$x—
Copy after login

5. String PHP Operators

String Operators are implemented over strings.

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

Syntax:

$text1.$text2
Copy after login
Copy after login

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

Syntax:

$text1.$text2
Copy after login
Copy after login

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
Copy after login

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

Syntax:

$x or $y
Copy after login

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
Copy after login

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

Syntax:

$x && $y
Copy after login

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

Syntax:

!$x
Copy after login

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!

Related labels:
php
source:php
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!