Home

Introduction to Operations

Fundamentals of Operations

Introduction

An operation is a group of values, called operands, and symbols, called operators that, formed as an expression, produce a new value. Operations are performed in numbers, characters, or strings. The operators and the operands play different roles in an operator. For example, an operator can be applied to an operand to change the value of that operand. An operator can be applied to two operand to produce a new value.

Unary Operators

An operator is referred to as unary if acts on only on operand. An example is - as in:

-24.75

In this case, - causes 24.75 to a negative number.

Binary Operators

A binary operator is one that requires, and acts on, 2 operands, usually to produce a new value.

Ternary Operations

A ternary operation is one that requires 3 operands and usually 2 operators. It is usually made to perform comparisons.

Arithmetic Operations

The Addition

The addition, represented with +, is binary operation used to add one value to another, and produces a new value. Here is an example:

<?php
    $nbr1 = 48;
    $nbr2 = 42;

    $total = $nbr1 + $nbr2;

echo "$nbr1 + $nbr2 = $total"
?>

This would produce:

Arithmetic Operations

The Subtraction

The subtraction, represented as -, is a binary operation used to subtract one numeric value from another. Here are examples:

<?php
    $nbr1 = 48;
    $nbr2 = 42;

    $result = $nbr1 + $nbr2;

echo "$nbr1 - $nbr2 = $result"
?>

This would produce:

Arithmetic Operations

The Multiplication

The multiplication is a binary operation used to multiply one number to another. Here is an example:

<?php
    $nbr1 = 48;
    $nbr2 = 42;

    $result = $nbr1 * $nbr2;

echo "$nbr1 * $nbr2 = $result"
?>

This would produce:

Arithmetic Operations

The Division

The division is a binary operation used to divide one number by another. The number to divide is called the numerator. The number that divides is called denominator. The denominator should never be 0. Here is an example:

<?php
    $nbr1 = 48;
    $nbr2 = 42;

    $result = $nbr1 / $nbr2;

echo "$nbr1 / $nbr2 = $result"
?>

This would produce:

Arithmetic Operations

The Remainder

The remainder is a binary operation that is used to get the integral value that remains when a division is mande. The operation is performed using the modulus operator %. Here is an example:

<?php
    $nbr1 = 48;
    $nbr2 = 42;

    $result = $nbr1 % $nbr2;

echo "$nbr1 % $nbr2 = $result"
?>

This would produce:

Arithmetic Operations

The Exponentiation

The  exponentiation is a binary operation used to raise one number to the power of another. The operation is performed with **.

Incrementing/Decrementing a Variable

Incrementing the Value of a Variable

Sometimes you want to increase the value of a variable without having to use another variable. If you want to increase the value of a variable by 1, PHP (like many languages, including all C-based languages) provides the ++ operator. To apply it, simply apply this operator to the name of the variable. Here is an example:

<?php
    $number = 48;
    echo "Number: $number <br>";

    $number++;
    echo "Number: $number";
?>

This would produce:

Incrementing the Value of a Variable

There are two ways this operator is used. If you want to increase the value before the variable is used, this is referred to as pre-increment. In this case, use the ++ operator on the left side of the variable, as in ++$number.

The post-increment consists of applying the ++ operator after the variable has been accessed. In this case, the ++ operator is written on the right side of the variable, as in $number++.

As an alternative, assign the variable to itself and add 1. This can be done as follows:

<?php
    $number = 48;
    echo "Number: $number <br>";

    $number = $number + 1;
    echo "Number: $number";
?>

This would produce:

Incrementing the Value of a Variable

You can increase the value of a variable by any number of your choice. To do this, assign the variable to itself and add the desired value. Here is an example:

<?php
    $number = 48;
    echo "Number: $number <br>";

    $number = $number + 26;
    echo "Number: $number";
?>

This would produce:

Incrementing the Value of a Variable

As an alternative, PHP (again like all C-based languages) provides a special operator as +=. To use it, put it after the name of the variable and follow it by the value you want to add to the variable. Here is an example:

<?php
$time_worked = 8.00;

echo "Time worked on first day: ";
echo $time_worked;
echo "<br>";

$time_worked += 9.50;

echo "Time worked so far: ";
echo $time_worked;
echo "<br>";

$time_worked += 8.00;

echo "Time worked so far: ";
echo $time_worked;
echo "<br>";

$time_worked += 7.00;

echo "Time worked so far: ";
echo $time_worked;
echo "<br>";

$time_worked += 9.00;

echo "Time worked this week: ";
echo $time_worked;
echo "<br>";
?>

This would produce:

Incrementing the Value of a Variable

Decrementing the Value of a Variable

If you want to decrease the value of a variable without using another variable, assign the variable to itself and subtract the desired value. This can be done as follows.

<?php
    $number = 48;
    echo "Number: $number <br>";

    $number = $number - 26;
    echo "Number: $number";
?>

This would produce:

Incrementing the Value of a Variable

If you want to decrease by 1, PHP provides the -- operator.

<?php
    $number = 48;
    echo "Number: $number <br>";

    $number--;
    echo "Number: $number";
?>

This would produce:

Incrementing the Value of a Variable

The decrement operator can be used before the variable, as in --$number, if you want to decrement before the variable is accessed. Use the operator on the right side of the variable, as in $number-- if you want to decrement after the variable has been accessed.


Previous Copyright © 2015-2022, FunctionX Next