Math: Operators, Properties, and Methods

Math: Operators, Properties, and Methods
udemy2 placeholder image

The PHP language makes working with math very easy. Not only does it offer the arithmetic operators you would expect from a programming language, but it also offers mathematical properties and functions so we do not have to implement them ourselves. This lets you focus on writing the logic of your program and not reinventing the wheel.

Arithmetic Operators

Arithmetic operators are what let you manipulate numbers in PHP. Here are ones it offers.

Addition

	
    <?php
        // adding numbers
        $cats = 4;
        $dogs = 3;

        $pets = $cats + $dogs;
        echo('I have ' . $pets . ' pets');
    ?>
	
	
    I have 7 pets
	

Here is a shorthand way to add a 1 to the value of a variable:

	
    <?php
        $cats = 4;
        $cats++; // adds 1

        echo('I have ' . $cats . ' cats');
    ?>
	
	
    I have 5 cats
	

Here is the shorthand way to add an arbitrary number:

	
    <?php
        $cats = 4;
        $cats += 2; // adds 2 cats

        echo('I have ' . $cats . ' cats');
    ?>
	
	
    I have 6 cats
	

Subtraction

	
    <?php
        // subtracting numbers
        $candy = 12;
        $eaten = 3;

        $left = $candy - $eaten;

        echo('I have ' . $left . ' candies left');
    ?>
	
	
    I have 9 candies left
	

Here's the shorthand way to subtract 1:

	
    <?php
        $candy = 12;
        $candy--; // subtracts 1

        echo('I have ' . $candy . ' candies');
    ?>
	
	
    I have 11 candies
	

Here's the shorthand way to subtract an arbitrary number:

	
    <?php
        $candy = 12;
        $candy -= 7; // subtracts 7 candies

        echo('I have ' . $candy . ' candies');
    ?>
	
	
    I have 5 candies
	

Multiplication

	
    <?php
        // multiplying numbers
        $tacos = 2;
        $guests = 20;
        $needed = $guests * $tacos;

        echo('I need ' . $needed . ' tacos');
    ?>
	
	
    I need 40 tacos
	

Here is the shorthand way to multiple a variable:

	
    <?php
        $tacos = 2;
        $tacos *= 20; // multiplies the value by 20

        echo('I need ' . $tacos . ' tacos');
    ?>
	
	
    I need 40 tacos
	

Division

	
    <?php
        // dividing numbers
        $burgers = 12;
        $guests = 6;
        $each = $burgers / $guests;

        echo('Each guest can eat ' . $each . ' burgers');
    ?>
	
	
    Each guest can eat 2 burgers
	

Here's the shorthand way to divide:

	
    <?php
        $burgers = 12;
        $burgers /= 6; // divides the value by 6

        echo('Each guest can eat ' . $burgers . ' burgers');
    ?>
	
	
    Each guest can eat 2 burgers
	

Modulus

	
    <?php
        // modulus operator
        $bottles = 14;
        $people = 4;
        $extra = $bottles % $people;

        echo('There are ' . $extra . ' extra bottles');
    ?>
	
	
    There are 2 extra bottles
	

Here is the shorthand way to perform modulus:

	
    <?php
        $bottles = 14;
        $bottles %= 4; // does modulus 4

        echo('There are ' . $bottles . ' extra bottles');
    ?>
	
	
    There are 2 extra bottles
	

Order of Operations

Order of Operations, sometimes known as PEMDAS, is respected in PHP. Here's an example of it in action:

	
    <?php
        $x = (5 * 3) + 10 / 2;

        ('x is ' . $x;
    ?>
	
	
    x is 20
	

Math Properties

To avoid making people define it themselves, PHP provides many useful mathematical constants that we can use whenever applicable.

Euler's Number

Euler's number is provided to us via the M_E constant.

	
    <?php
        echo(M_E);
    ?>
	
	
    2.718281828459
	

Pi

The much known and beloved pi is provided via the M_PI constant.

	
    <?php
        echo(M_PI);
    ?>
	
	
    3.1415926535898
	

Euler's Constant

Euler's constant is provided to us via the M_EULER constant.

	
    <?php
        echo(M_EULER);
    ?>
	
	
    0.57721566490153
	
namecheap placeholder image

Math Functions

In addition to mathemetical properties, PHP also implements some commonly used mathematical functions that we can use whenever we want.

Absolute Number

Get the absolute number by passing in a number to abs() function:

	
    <?php
        $number1 = 3;
        $number2 = -6;

        echo(abs($number1) . ' ' . abs($number2));
    ?>
	
	
    3 6
	

Floor

Round any number down to the nearest whole number by calling the floor() function on it:

	
    <?php
        $number = 3.1415;

        echo(floor($number));
    ?>
	
	
    3
	

Ceiling

Round any number up to the nearest whole number by calling the ceil() function on it:

	
    <?php
        $number = 3.1415;

        echo(ceil($number));
    ?>
	
	
    4
	

Logarithm

Get the natural logarithm of a number by using the log() function:

	
    <?php
        $number = 3.1415;

        echo(log($number));
    ?>
	
	
    1.1447003928609
	

Maxiumum

Get the highest number from a set of numbers by using the max() function:

	
    <?php
        echo(max(35, 23, 52, 34, 74, 43));
    ?>
	
	
    74
	

Minimum

Get the lowest number from a set of numbers by using the min() function:

	
    <?php
        echo(min(35, 23, 52, 34, 74, 43));
    ?>
	
	
    23
	

Power

Calculate the power of a number by using the pow() function. The first parameter is the base and the second parameter is the exponent.

	
    <?php
        echo(pow(5, 3));
    ?>
	
	
    125
	

Random

Return a random number between a range that you define by using the rand() function:

	
    <?php
        $roll = rand(1, 6);
        echo('I just rolled a ' . $roll);
    ?>
	
	
    I just rolled a 6
	

Round

Round a number to the nearest integer by using the round() function on it:

	
    <?php
        $number1 = 13.37;
        $number2 = 82.65;

        echo(round($number1) . ' ' . round($number2));
    ?>
	
	
    13 83
	

Square Root

Calculate the square root of a number by using the sqrt() function:

	
    <?php
        echo(sqrt(1337));
    ?>
	
	
    36.565010597564
	

That's the end of it for Math! 🤓🤓🤓