Assignment and increment decrement operators php

Assignment and increment decrement operators php

In PHP programming, assignment and increment/decrement operators are used to manage and control the default values ​​of declared variables within a program. The primary use of assignment and increment/decrement operators in PHP is to assign a numeric value to a variable, update the value of an existing variable, and increment or decrement the variable value.

Assignment and increment decrement operators php

Assignment Operators in PHP

In PHP programming, the assignment operator is used to assign the value of one variable to another. The assignment operator is represented by the equal sign (=). However, PHP also provides several compound assignment operators that help developers perform multiple variable assignments with an operation.

Basic Assignment Operator Syntax

= – This is used to assign a value to a variable in PHP

Example of Assignment Operator

<?php

$p = 8; // Here we assign 8 to the variable $p

$q = $p; // Here it assigns the value of $p (8) to $q

$r = $q;

echo $q; // Result – 8

echo $r; // Result – 8

?>

Compound Assignment Operators in PHP

In PHP, compound assignment operators perform both the operation and the assignment of the value to the variable in a single step.

Operator nameCompound Assignment DescriptionExample
+=It used to Adds and assigns the result to the define variable$p += $q; this is Equivalent to $p = $p + $q; expression
-=It used to Subtracts and assigns the result to the define variable$p -= $q; this is Equivalent to $p = $p – $q; expression
*=It used to Multiplies and assigns the result to the define variable$p *= $q; this is Equivalent to $p = $p * $q; expression
/=It used to Divides and assigns the result to the define variable$p /= $q; this is Equivalent to $p = $p / $q; expression
%=It used to Modulus and assigns the result to the define variable$p %= $q; this is Equivalent to $p = $p % $q; expression
.=It used to Concatenates and assigns the result to the define variable$p .= $q; this is Equivalent to $p = $p . $q; expression

Example of Compound Assignment Operators

<?php

$p = 10;

$q = 5;

$p += $q; // Adds $q to $p: $p = $p + $q => $p = 15

echo $p; // Result – 15

echo “\n”;

$p -= $q; // Subtracts $q from $p: $p = $p – $q => $p = 10

echo $p; // Result – 10

echo “\n”;

$p *= $q; // Multiplies $p by $q: $p = $p * $q => $p = 50

echo $p; // Result – 50

echo “\n”;

$p /= $q; // Divides $p by $q: $p = $p / $q => $p = 10

echo $p; // Result – 10

echo “\n”;

$p %= $q; // Calculates the modulus: $p = $p % $q => $p = 0

echo $p; // Result – 0

echo “\n”;

$p = “vcahelpsu”;

$q = ” programming”;

$p .= $q; // Concatenates $p and $q: $p = “vcahelpsu programming”

echo $p; // Result – vcahelpsu programming

?>

Increment and Decrement Operators in PHP

In PHP programming, increment and decrement operators are used to increase or decrease the value of a declared program variable by 1. Increment and decrement operators can be used to update the variable value in prefix or postfix order, depending on when the increment or decrement operator is applied in the current program expression.

Increment Operator (++) Explanation

  • Pre-increment (++$p) expression – This pre-increments the value of $p before using it in a PHP program expression.
  • Post-increment ($p++) expression – Uses the current variable value of $p in the PHP program expression and then post-increments it.

Decrement Operator (–) Explanation

  • Pre-decrement (–$p) expression – This decrements the value of the $p variable before using it in a PHP program expression.
  • post-decrement ($p–) expression – This uses the current value of the variable $p in the PHP program expression and then decrements it.

Example of Increment and Decrement Operators

<?php

$p = 4;

$q = 8;

// Here we use the Pre-increment operator

echo ++$p; // It increments $p to 5, Result – 5

echo “\n”;

// Here we use the post-increment operator

echo $q++; // Result – 8 (uses the value before incrementing; $q then becomes 9)

echo “\n”;

// Here we use the Pre-decrement operator

echo –$p; // It decrements $p to 4, then outputs 4

echo “\n”;

// Here we use the post-decrement operator

echo $q–; // Result – 9 (uses the value before decrementing; $q then becomes 8)

echo “\n”;

?>

Detailed Behavior of increment and decrement operators

Pre-increment (++$a) expression

In PHP programming, the value of the variable $p is first incremented, and then the updated new value of the variable can be used in any programming expression.

$p = 9;

echo ++$p; // Result – 10

Post-increment ($a++) expression

Here, in the PHP program, the current value of the variable $p is used first, and then the value of the variable $p is incremented.

$a = 7;

echo $p++; // Result – 7 (value of $p before increment)

echo $p; // Result – 8 (value of $p after increment)

Pre-decrement (–$a) expression

Here, in the PHP program, the value of $p is first decremented, and then the updated value is displayed in the expression.

$a = 9;

echo –$p; // Result – 8

Post-decrement ($p–) expression

Here, in the PHP program, the current value of $p is used first, and then the value of $p is decremented.

$p = 3;

echo $p–; // Result – 3 (Value of variable $p before decrement)

echo $p; // Result – 2 (Value of variable $p after decrement)

Combining Assignment and Increment/Decrement Operators in PHP

In PHP programming, developers can combine the assignment operator with the increment/decrement operators for concise code. For example, a programmer can increment the value of a variable and assign or allocate it to another variable simultaneously.

<?php

$p = 3;

$q = 8;

// Here it increments $p and assigns it to the $q variable

$q = ++$p; // Here $p is incremented to 4, then $q is assigned the value of 4

echo $p; // Result – 4

echo “\n”;

echo $q; // Result – 4

echo “\n”;

// Here it decrements $p and assigns it to the $q variable

$q = –$p; // Here $p is decremented to 3, then $q is assigned the value of 3

echo $p; // Result – 3

echo “\n”;

echo $q; // Result – 3

?>

Detail about Assignment and Increment/Decrement Operators in php

Operator nameOperator DescriptionExample
=Equal operator used to Basic assignment, assigns p value to p php program variable$p = 4;
+=It used to Add and assign value to the other variable$p += $q;
-=It used to Subtract and assign value to the other program variable$p -= $q;
*=It used to Multiply and assign program value to the other variable$p *= $q;
/=It used to Divide and assign program variable value to the other variable$p /= $q;
%=It used to Modulus and assign program variable value to the other variable$p %= $q;
.=It used to Concatenate and assign program variable to other value$p .= $q;
++ (pre/post)It used to Increment (increase by 1) current php variable++$p, $p++
— (pre/post)It used to Decrement (decrease by 1) current php program variable value–$p, $p–
  • Pre-increment (++$p) expression – This increments the value of the variable $p first, then updates it with the new value in the current PHP expression.
  • Post-increment ($p++) expression – This provides the value of the variable $p in the current PHP expression, then increments it.
  • Pre-decrement (–$p) expression – This decrements the value of the variable $p first, then provides the new value in the current PHP expression.
  • Post-decrement ($p–) expression – This provides the value of the variable $p in the current PHP expression, then decrements it.

Leave a Reply