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

OperatorDescriptionExampleEquivalent to
+=Addition assignment
-=Subtraction assignment
*=Multiplication assignment
/=Division assignment
%=Modulus assignment

Java, C++, C#

OperatorDescriptionExampleEquivalent to
+=Addition assignment
-=Subtraction assignment
*=Multiplication assignment
/=Division assignment
%=Modulus assignment

Visual Basic

OperatorDescriptionExampleEquivalent to
+=Addition assignment
-=Subtraction assignment
*=Multiplication assignment
/=Division assignment
\=Integer division assignment
^=Exponentiation assignment

Python

OperatorDescriptionExampleEquivalent 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:

OperatorDescriptionExampleResult
AdditionSum of $x and $y
SubtractionDifference of $x and $y.
MultiplicationProduct of $x and $y.
DivisionQuotient of $x and $y
ModulusRemainder 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.

OperatorDescriptionExampleIs 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.

OperatorNameExampleResult
EqualTrue if $x is equal to $y
IdenticalTrue if $x is equal to $y, and they are of the same type
Not equalTrue if $x is not equal to $y
Not equalTrue if $x is not equal to $y
Not identicalTrue if $x is not equal to $y, or they are not of the same type
Less thanTrue if $x is less than $y
Greater thanTrue if $x is greater than $y
Greater than or equal toTrue if $x is greater than or equal to $y
Less than or equal toTrue 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.

OperatorNameEffect
Pre-incrementIncrements $x by one, then returns $x
Post-incrementReturns $x, then increments $x by one
Pre-decrementDecrements $x by one, then returns $x
Post-decrementReturns $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.

OperatorNameExampleResult
AndTrue if both $x and $y are true
OrTrue if either $x or $y is true
XorTrue if either $x or $y is true, but not both
AndTrue if both $x and $y are true
OrTrue if either $$x or $y is true
NotTrue 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.

OperatorDescriptionExampleResult
ConcatenationConcatenation of $str1 and $str2
Concatenation assignmentAppends 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:

OperatorNameExampleResult
UnionUnion of $x and $y
EqualityTrue if $x and $y have the same key/value pairs
IdentityTrue if $x and $y have the same key/value pairs in the same order and of the same types
InequalityTrue if $x is not equal to $y
InequalityTrue if $x is not equal to $y
Non-identityTrue 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";
  • ?>
Categories: 1

0 Replies to “Compound Assignment Operators Php Scripts”

Leave a comment

L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *