Broad Network


Assignment Operators in PHP

PHP Operators with Security Considerations - Part 1

Foreword: In this part of the series, I explain PHP assignment operators.

By: Chrysanthus Date Published: 17 Oct 2018

Introduction

This is part 1 of my series, PHP Operators with Security Considerations. In this part of the series, I explain PHP assignment operators. You should have read the previous series before coming here as this is the continuation.

Operand
An Operand is a variable or a literal (value) associated with an operator. Consider,

    $myVar = 30;  

$myVar is a left operand and 30 is a right operand to =. = is an example of an operator. It is called, the assignment operator, not the equal operator (details below).

Consider:

      $myVar && $hisVar && $herVar

There are three operands in this expression. So, you can talk of the first ($myVar), second ($hisVar) and third ($herVar) operands. &&, that is 2 ampersands, form an operator; I shall talk about that later.

Binary and Unary Operators
A binary operator needs two operands to work with: one on its left and one on its right. A unary operator needs only one operand to work with, placed on its left or on its right (see later).

Assignment Operators
Assignment operators are:

=  *=  /=  %=  +=  -= .=

We look at each of these operators in this part of the series.

Simple Assignment, =
The basic assignment operator is, =. The following statement illustrates an example of its use:

    $myInt = 35;

We say the integer, 35, is assigned to the identifier, $myInt. The left operand to = is $myInt. The right operand is 35.

For basic or simple assignment, the right operand may be another variable or even a function call that returns a value.

Compound Assignment Operators
= is actually the simple assignment operator. A compound assignment operator is made up of two operators: some other operator followed by the assignment operator.

E1 op= E2 Expressions
In the following generalized statement, E1 is a variable, op is an operator and E2 is another variable; = is the simple assignment operator. This statement is used to explain the coding of the compound operators.

    E1 op= E2

This generalized statement is equivalent to,

    E1 = E1 op E2

The rest of the assignment operators in the list above follow this generalized rule. The purposes of the assignment operators are explained below. These compound assignment operators are each made up of the simple assignment operator and an operator you might already know.

The *= Operator
Consider the following statement:

    $int1 = $int1 * $int2;

Here, $int1 and $int2 are variables. Assume that a value has already been assigned to $int1 previously, before this statement. Note that $int1 is found in both the left operand and the right operand ($int1 * $int2), of the simple assignment operator. We also have the multiplication operator, *. In the above statement the result of multiplying $int1 by $int2 is assigned to $int1. The statement can be re-written as:

    $int1 *= $int2;

Also, $int2 can be replaced by a number as in,

    $int1 *=3;

The /= Operator
Consider the following statement:

    $int1 = $int1 / $int2;

Here, $int1 and $int2 are variables. Assume that a value has already been assigned to $int1 previously, before this statement. Note that $int1 is found in both the left operand and the right operand of =. We also have the division operator, /. In the above statement the result of dividing $int1 by $int2 is assigned to $int1. The statement can be re-written as:

    $int1 /= $int2;

Also, $int2 can be replaced by a number as in,

    $int1 /=4;

The following program illustrates this:

<?PHP

        $int1 = 20;
        $int2 = 4;

        $int1 /= $int2;
        echo($int1);

?>

The %= Operator
Consider the following statement:

     $int1 = $int1 % $int2;

Here, $int1 and $int2 are variables. Note that $int1 is found in both the left operand and right operand. We also have the modulus operator, %. In the above statement the remainder of dividing $int1 by $int2 is assigned to $int1. The statement can be re-written as:

    $int1 %= $int2;

The += Operator
Consider the following statement:

    $int1 = $int1 + $int2;

Here, $int1 and $int2 are variables. Note that $int1 is found in both the left operand and the right operand of the simple assignment operator. We also have the addition operator, +. In the above statement the result of adding $int1 to $int2 is assigned to $int1. The statement can be re-written as:

    $int1 += $int2;

The -= Operator
Consider the following statement:

    $int1 = $int1 - $int2;

Here, $int1 and $int2 are variables. Note that $int1 is found in both the left operand and the right operand of the simple assignment operator. We also have the subtraction operator, -. In the above statement the result of subtracting $int2 from $int1, is assigned to $int1. The statement can be re-written as:

    $int1 -= $int2;

String Operators
The string operator is the dot, that is

    .

To concatenate two strings as variables or as literals, use the dot operator. Try this example:

     <?php

         $str = "one " . "two";
         echo($str);

     ?>

In the script code, the dot (.) operator concatenates two strings.
Hey, you can use the E1 op= E2 compound operation with the string . operator.
Example:

    <?php

        $str = "one ";
        $str .="two";
        echo($str);

     ?>

Try the code.  

With that we come to the end of Assignment Operators. See you in the next part of the series.

Chrys


Related Links

Basics of PHP with Security Considerations
White Space in PHP
PHP Data Types with Security Considerations
PHP Variables with Security Considerations
PHP Operators with Security Considerations
PHP Control Structures with Security Considerations
PHP String with Security Considerations
PHP Arrays with Security Considerations
PHP Functions with Security Considerations
PHP Return Statement
Exception Handling in PHP
Variable Scope in PHP
Constant in PHP
PHP Classes and Objects
Reference in PHP
PHP Regular Expressions with Security Considerations
Date and Time in PHP with Security Considerations
Files and Directories with Security Considerations in PHP
Writing a PHP Command Line Tool
PHP Core Number Basics and Testing
Validating Input in PHP
PHP Eval Function and Security Risks
PHP Multi-Dimensional Array with Security Consideration
Mathematics Functions for Everybody in PHP
PHP Cheat Sheet and Prevention Explained
More Related Links

Cousins

NEXT

Comments