Broad Network


Assignment Operators in PHP

PHP Operators – Part 1

Forward: In this part of the series, we look at assignment operators in PHP.

By: Chrysanthus Date Published: 10 Aug 2012

Introduction

This is part 1 of my series, PHP Operators. Addition and subtraction symbols are examples of operators in PHP. PHP has many other operators that do not have similarities with mathematics. In this part of the series, we look at assignment operators in PHP. Everything said in this series is applicable to PHP 5. I explain most of the operators, not all.

If you are an old programmer, you can read the series in any order or you can read just the part, which is related to a problem you currently have at your job side. If you are new to programming or you studied programming and have not been practicing it, then you should read the whole series.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Prerequisite
This series is easy to understand in itself. However, to appreciate its application, you need to be conversant with the following topics:

- Getting started with PHP
- OOP Basics in PHP
- Some Scalar Data Types in PHP

A computer language builds up. There are certain things you have to learn first and then use them to learn higher things. Each of the above titles is either a tutorial or the first tutorial in a series. If it is the first part of a series, then you should have read the whole series. If it is a tutorial standing alone, then you should have read the tutorial. To reach any of the articles, just type the title of the article and my name Chrys in the Search Box of this page and click Search.

In this part of the series, we look at assignment operators in PHP. An example of such an operator is =.

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 called, the assignment operator, not the equal operator (details below).

Consider:

      $myVar && $hisVar && $herVar

There are three operands in the above expression. So, you can talk of the first (myVar), second (hisVar) and third operands (herVar). &&, that is 2 ampersands form another operator; we 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:

    $myInt = 35;

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

For all assignment operators the operation is from right to left; that is, from the right operand to the left operand. If you are trying to make similarity with mathematics, then the previous sentence (right to left) may not make sense; avoid making similarity with mathematics. We shall see many examples of right to left and left to right operations as we go along in the series.

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

E1 op= E2 Expressions
In the following generalized statement, E1 is a variable, op is an operator and E2 is another variable; = is the basic assignment operator:

    E1 op= E2

This generalized statement that involves the simple assignment operator is equivalent to,

    E1 = E1 op E2

The rest of the assignment operators in the list above follow this generalized rule as explained below. The rest of the assignment operators are each made up of an operator you might already know, and the basic assignment operator.

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

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

    int 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 basic 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 basic 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 PHP string has two operators, which are,

    . and .=

To concatenate two strings as variables or as literals, use the dot operator. Example:

<?php

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

?>

Hey, you can use the E1 op= E2 generalized statement with the string dot operator. Example:

<?php

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

?>

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

Chrys

Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message