# Compound Assignment Operators Php Scripts

Many modern computer languages offers a special set of operators known as compound assignment operators, which can help you write code faster.

## PHP

Operator | Description | Example | Equivalent to |

+= | Addition assignment | ||

-= | Subtraction assignment | ||

*= | Multiplication assignment | ||

/= | Division assignment | ||

%= | Modulus assignment |

## Java, C++, C#

Operator | Description | Example | Equivalent to |

+= | Addition assignment | ||

-= | Subtraction assignment | ||

*= | Multiplication assignment | ||

/= | Division assignment | ||

%= | Modulus assignment |

## Visual Basic

Operator | Description | Example | Equivalent to |

+= | Addition assignment | ||

-= | Subtraction assignment | ||

*= | Multiplication assignment | ||

/= | Division assignment | ||

\= | Integer division assignment | ||

^= | Exponentiation assignment |

## Python

Operator | Description | Example | Equivalent to |

+= | Addition assignment | ||

-= | Subtraction assignment | ||

*= | Multiplication assignment | ||

/= | Division assignment | ||

//= | Integer division assignment | ||

%= | Modulus Assignment | ||

^= | Exponentiation assignment |

Looking at the *“Equivalent to”* column, it becomes clear that same result can be achieved by just using the classic assignment ( = ) operator. So the question that arises here is *why do these operators exist?*

The answer is simple: It’s a matter of convenience. Once you start using them, your life finds a different meaning!

Notice: Please keep in mind that flowcharts are a loose method to represent an algorithm. Although the use of compound assignment operators is allowed in flowcharts, this website uses only the commonly accepted operators shown in the “Equivalent to” column. For example, the Java statement is represented in a flowchart as

## PHP Operators

In this tutorial you will learn how to manipulate or perform the operations on variables and values using operators in PHP.

## What is Operators in PHP

Operators are symbols that tell the PHP processor to perform certain actions. For example, the addition () symbol is an operator that tells PHP to add two variables or values, while the greater-than () symbol is an operator that tells PHP to compare two values.

The following lists describe the different operators used in PHP.

## PHP Arithmetic Operators

The arithmetic operators are used to perform common arithmetical operations, such as addition, subtraction, multiplication etc. Here's a complete list of PHP's arithmetic operators:

Operator | Description | Example | Result |
---|---|---|---|

Addition | Sum of $x and $y | ||

Subtraction | Difference of $x and $y. | ||

Multiplication | Product of $x and $y. | ||

Division | Quotient of $x and $y | ||

Modulus | Remainder of $x divided by $y |

The following example will show you these arithmetic operators in action:

## PHP Assignment Operators

The assignment operators are used to assign values to variables.

Operator | Description | Example | Is The Same As |
---|---|---|---|

Assign | |||

Add and assign | |||

Subtract and assign | |||

Multiply and assign | |||

Divide and assign quotient | |||

Divide and assign modulus |

The following example will show you these assignment operators in action:

## PHP Comparison Operators

The comparison operators are used to compare two values in a Boolean fashion.

Operator | Name | Example | Result |
---|---|---|---|

Equal | True if $x is equal to $y | ||

Identical | True if $x is equal to $y, and they are of the same type | ||

Not equal | True if $x is not equal to $y | ||

Not equal | True if $x is not equal to $y | ||

Not identical | True if $x is not equal to $y, or they are not of the same type | ||

Less than | True if $x is less than $y | ||

Greater than | True if $x is greater than $y | ||

Greater than or equal to | True if $x is greater than or equal to $y | ||

Less than or equal to | True if $x is less than or equal to $y |

The following example will show you these comparison operators in action:

## PHP Incrementing and Decrementing Operators

The increment/decrement operators are used to increment/decrement a variable's value.

Operator | Name | Effect |
---|---|---|

Pre-increment | Increments $x by one, then returns $x | |

Post-increment | Returns $x, then increments $x by one | |

Pre-decrement | Decrements $x by one, then returns $x | |

Post-decrement | Returns $x, then decrements $x by one |

The following example will show you these increment and decrement operators in action:

## PHP Logical Operators

The logical operators are typically used to combine conditional statements.

Operator | Name | Example | Result |
---|---|---|---|

And | True if both $x and $y are true | ||

Or | True if either $x or $y is true | ||

Xor | True if either $x or $y is true, but not both | ||

And | True if both $x and $y are true | ||

Or | True if either $$x or $y is true | ||

Not | True if $x is not true |

The following example will show you these logical operators in action:

## PHP String Operators

There are two operators which are specifically designed for strings.

Operator | Description | Example | Result |
---|---|---|---|

Concatenation | Concatenation of $str1 and $str2 | ||

Concatenation assignment | Appends the $str2 to the $str1 |

The following example will show you these string operators in action:

## PHP Array Operators

The array operators are used to compare arrays:

Operator | Name | Example | Result |
---|---|---|---|

Union | Union of $x and $y | ||

Equality | True if $x and $y have the same key/value pairs | ||

Identity | True if $x and $y have the same key/value pairs in the same order and of the same types | ||

Inequality | True if $x is not equal to $y | ||

Inequality | True if $x is not equal to $y | ||

Non-identity | True if $x is not identical to $y |

The following example will show you these array operators in action:

## PHP Spaceship Operator PHP 7

PHP 7 introduces a new spaceship operator () which can be used for comparing two expressions. It is also known as combined comparison operator.

The spaceship operator returns if both operands are equal, if the left is greater, and if the right is greater. It basically provides three-way comparison as shown in the following table:

Operator | Equivalent |
---|---|

The following example will show you how spaceship operator actually works:

#### Example

Run this code »- <?php
- $x=10;
- $y=4;
- echo($x+$y);
- echo($x-$y);
- echo($x*$y);
- echo($x/$y);
- echo($x%$y);
- ?>

#### Example

Run this code »- <?php
- $x=25;
- $y=35;
- $z="25";
- var_dump($x==$z);
- var_dump($x===$z);
- var_dump($x!=$y);
- var_dump($x!==$z);
- var_dump($x<$y);
- var_dump($x>$y);
- var_dump($x<=$y);
- var_dump($x>=$y);
- ?>

#### Example

Run this code »- <?php
- $x=10;
- echo++$x;
- echo$x;
- $x=10;
- echo$x++;
- echo$x;
- $x=10;
- echo--$x;
- echo$x;
- $x=10;
- echo$x--;
- echo$x;
- ?>

#### Example

Run this code »- <?php
- $year=2014;
- if(($year%400==0) || (($year%100!=0) && ($year%4==0))){
- echo"$year is a leap year.";
- } else{
- echo"$year is not a leap year.";
- }
- ?>

#### Example

Run this code »- <?php
- $x="Hello";
- $y=" World!";
- echo$x . $y;
- $x .=$y;
- echo$x;
- ?>

#### Example

Run this code »- <?php
- $x=array("a"=>"Red", "b"=>"Green", "c"=>"Blue");
- $y=array("u"=>"Yellow", "v"=>"Orange", "w"=>"Pink");
- $z=$x+$y;
- var_dump($z);
- var_dump($x==$y);
- var_dump($x===$y);
- var_dump($x!=$y);
- var_dump($x<>$y);
- var_dump($x!==$y);
- ?>

#### Example

Run this code »- <?php
- echo1<=>1;
- echo1<=>2;
- echo2<=>1;
- echo1.5<=>1.5;
- echo1.5<=>2.5;
- echo2.5<=>1.5;
- echo"x"<=>"x";
- echo"x"<=>"y";
- echo"y"<=>"x";
- ?>

## 0 Replies to “Compound Assignment Operators Php Scripts”