Broad Network


Arithmetic Operators in PHP

PHP Operators with Security Considerations - Part 2

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

By: Chrysanthus Date Published: 17 Oct 2018

Introduction

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

Additive Operators
Additive Operators are addition and subtraction operators.

Addition Operator
Read and try the following code. The explanation is given below.

    <?php

        $id1 = 20;
        $id2 = 30;
        
        $id3 = $id2 + $id1;
        echo($id3);

     ?>

20 is kept in a region in memory identified by $id1. 30 is kept in the region identified by $id2. In the third statement, PHP takes the content of $id2 and adds it to the content of $id1, then it puts the result as content for the region of the newly declared identifier (variable), $id3. This addition is done without affecting or changing the contents of $id2 and $id1.

Subtraction Operator
Try the following code:

    <?php

        $id1 = 20;
        $id2 = 30;
        
        $id3 = $id2 - $id1;
        echo($id3);

     ?>

The explanation is similar to the previous case, but this time, subtraction is done.

Multiplicative Operators
Multiplicative operators are the *, / and % operators. See explanations below:

Multiplication Operator
Try the following code.

    <?php

        $id1 = 20;
        $id2 = 30;
        
        $id3 = $id2 * $id1;
        echo($id3);

     ?>

Note that the multiplication operator is * and not X. * multiplies two numbers.

Division Operator
Try the following code. The explanation is given below.

    <?php

        $id1 = 3;
        $id2 = 15;
        
        $id3 = $id2 / $id1;
        echo($id3);

     ?>

Note that the division operator is, / . / divides one number by another.

Modulus Operator
The modulus operator divides the first operand by the second operand and returns the remainder. Try the following code:

    <?php

        $id1 = 17;
        $id2 = 12;
        
        $id3 = $id1 % $id2;
        echo($id3);

     ?>

The Modulus operator is the percentage sign.

The - Operator
This is the negation operator. If the value of the operand is a positive number, it changes it to a negative number. If it is a negative number, it changes it to a positive number. This same symbol is used for subtraction; however here it is the negation operator. The following example illustrates this:

    <?php

        $int1 = +5;
        $int2 = -6;

        $intA = -$int1;
        $intB = -$int2;    

        echo($intA), '<br>';
        echo($intB);

     ?>

If you try the above code, the value of $intA will be -5 and the value of $intB will be +6 (same as 6). Remember, in mathematics, +5 and 5 mean the same thing, but -5 and 5 are two different things.

So there are 6 arithmetic operators that are:

+  - * / % negation

That is it for arithmetic operators. We take a break here and continue 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

BACK NEXT

Comments