Broad Network


Unary Operators in PHP

PHP Operators – Part 5

Forward: In this part of the series we look at Unary Operators in PHP.

By: Chrysanthus Date Published: 10 Aug 2012

Introduction

This is part 5 of my series, PHP Operators. A PHP unary operator needs only one operand to work with. In this part of the series we look at Unary Operators in PHP.

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.

The increment Operator
The Increment Operator is, ++. The operand has to be a number. When it is placed in front (prefix) of the operand, it behaves in one way. When it is placed after (postfix) the operand it behaves in another way.

Prefix: When it is prefix, it adds 1 to the operand and returns the incremented operand value. Read and try the following code:

<?php

        $id1 = 10;
        
        $id2 = ++$id1;
        
        echo $id2;

?>

In the code, initially, 10 is assigned to the variable, $id1. Then we have a new statement. In the statement you have a new variable, $id2, the assignment operator and then “++$id1”. What interest us here is “++$id1”, where the increment operator is in front of the variable. The value the increment operator returns is assigned to the variable, $id2. If you have tried the code, you would have noticed that the value of $id2 is 11. This means, if used prefix, it increments the operand and then returns the incremented content of the operand.

Postfix: When it is postfix, it returns the operand value before adding 1 to it. The returned value is the original value of the operand. The increased value is the new value of the operand, which is not returned. Read and try the following code.

<?php

        $id1 = 10;
        
        $id2 = $id1++;
        
        echo $id2 . "<br />";
        echo $id1;

?>

If you have tried the above code, you would have noticed that the value for $id2 is 10 and the final value for $id1 is 11, confirming that the incrementing took place after the value was returned. Always remember that when it is placed postfix, the value of the operand is returned before it is incremented.

The Decrement Operator
The decrement operator is --. It decreases its number by 1. It can be used prefix or postfix, as with ++.

The & Operator

Region in Memory
A region in memory is a consecutive set of cells in the computer’s memory. A memory region can hold a datum, e.g. an integer or a floating-point number or string. A region can also hold an instantiated object. Consider the following statement:

        $myVar = 56;

In this statement 56 is an integer, which is in a memory region. At the moment this 56 is identified by the variable, $myVar.

Make another Variable Identify the Same Region
You can make another variable identify the memory region already identified for 56 in the above statement. The following code segment does this:

        $myVar = 56;
        $herVar = &$myVar;

There are two statements here. The first one initializes a variable in the normal way, assigning 56 to $myVar. The second statement also initializes a new variable. The right operand of this second statement is the previous variable preceded by the ampersand, &. The $myVar variable already identifies the region in memory that has 56. By preceding it with & in the second statement and assigning the result to the new variable, $herVar, you are making the new variable identify the same memory region that has 56. Now, $myVar and $herVar identify the same region in memory that has, 56. At this point, you can obtain 56 using either $myVar or $herVar.

What is a Reference in PHP?
This is what the PHP specification says about reference in PHP: “PHP references allow you to make two variables to refer to the same content”. In this quotation, “content” means, the value in the region. In the above code segment repeated below, 56 is the value in a region.

        $myVar = 56;
        $herVar = &$myVar;

In this code segment, we can say, &$myVar is a reference. We can say that if you precede a variable with &, you get a reference. The & operator is used to create a reference.

The new Operator
The new operator is used to instantiate an object from a class. I assume that you have read the series titled, Object Oriented Programming in PHP. The explanation of the new operator is there, so I will not explain it here.

PHP does not have the phrase “Unary Operators”; I have borrowed it from C++.

Let us take a break here and continue 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