PHP Math Functions

PHP Math Functions

Web Development in PHP:

Web Development in PHPPHP is a server side scripting language use for website development mainly. Most web development companies prefer PHP for website development mainly because of its easy syntax and its compatibility to run with Linux based server. PHP is an object oriented programming language and like all other object oriented programming languages it also do possess methods, functions, procedure and classes. Below we will discuss about some major mathematical functions of PHP. Before discussing, we are pointing variables with their values that we will use to describe syntax.

  • First variable we will use is x, it will be our first integer & its assigned value is 6.
  • Second variable we will use is y, it will be our second integer & its assigned value is 4.
  • Third variable will be z, it will be third integer that will be used and it will be use to assign range in it. We are setting its value 2.
  • Fourth variable will be result & resultant will be assigned in it.

In programming perspective the above mentioned syntax are like following;

<?php $x=6;       $y=4;     $z=2;     ?>

PHP Mathematical FunctionsPHP Maths Functions

Number format in PHP:

To output a result in php with certain decimal places number_format function is use.

Syntax:

<?php echo “Result is ”. number_fomat(10,$z);?>

Output: Result is 10.00

Addition in PHP:

For addition in PHP, function called bcadd is used.

Syntax:

<?php $result=bcadd($x,$y,$z); ?>

On echo it will show following result,

<?php echo “Result is ”. number_format($result,$z); ?>

Output: Result is 10.00

Subtraction in PHP:

For subtraction in PHP, function called bcsub is used.

Syntax:

<?php $result=bcsub($x,$y,$z); ?>

On echo it will show following result,

<?php echo “Result is ”. number_format($result,$z); ?>

Output: Result is 2.00

Multiplication in PHP:

For multiplication in PHP, function called bcmul is used.

Syntax:

<?php $result=bcmul($x,$y,$z); ?>

On echo it will show following result,

<?php echo “Result is ”. number_format($result,$z); ?>

Output: Result is 24.00

Division in PHP:

For addition in PHP, function called bcdiv is used.

Syntax:

<?php $result=bcdiv($x,$y,$z); ?>

On echo it will show following result,

<?php echo “Result is ”. number_format($result,$z); ?>

Output: Result is 1.50

Square Root in PHP:

To find square root of any integer value function of sqrt is use.

Syntax:

<?php $result= sqrt($y); ?>

On echo it will show following result,

<?php echo “Result is ”. number_format($result,$z); ?>

Output: Result is 2.00

Other Major PHP Math Functions:

Other PHP math functions that are commonly use are provided below with syntax and examples,

Absolute Value:

To find absolute value of any number abs function is use.

Syntax:

<?php $result=abc(-4.2) ?>

On echo it will show following result,

<?php echo “Result is ”. number_format($result,$z); ?>

Output: Result is 4.20

Number Comparison:

To compare numbers according to their values or to find lowest or highest value among an array following functions are use.

Syntax for minimum value:

<?php echo min(2, 3, 1, 6, 7); ?>

Output: 1

Syntax for Maximum value:

<?php echo max(2, 3, 1, 6, 7);?>

Output: 7

Count Function:

To count all elements in an array or object count function is use in PHP.

Syntax:

<?php   $a

[0] = 2;             $a[1] = 3;             $a[2] = 9;             echo “Total Count is ”. $result = count($a); ?>

Output: Total Count is 3

Round Function:

Round function is used to provide rounded value of an integer.

Syntax & Output:

<?php

echo round(7.4);              Output: 7

echo round(7.5);              Output: 8

echo round(7.6);              Output: 8

echo round(7.6, 0);         Output: 8

echo round(7.9558, 2);  Output: 7.96

?>

For further details with round, round half up, down, even and odd are also use

Syntax & Output:

<?php

echo round(7.5, 0, PHP_ROUND_HALF_UP);        Output: 8

echo round(7.5, 0, PHP_ROUND_HALF_DOWN);Output:  7

echo round(7.5, 0, PHP_ROUND_HALF_EVEN);  Output:  8

echo round(7.5, 0, PHP_ROUND_HALF_ODD);   Output:  7

?>

To round fractions up ceil function is also use.

Syntax & Output:

<?php

echo ceil(7.3);                    Output: 8

echo ceil(7.999);              Output: 8

echo ceil(-7.14);                Output: -7

?>

To round fractions down floor function is use

Syntax & Output

<?php

echo floor(7.3);                 Output: 7

echo floor(7.999);           Output: 7

echo floor(-6.14);            Output: -7

?>

For more knowledge and interesting content related website development in PHP keep visiting our web development company blog or visit PHP website (http://php.net/manual/en/ref.math.php) for all mathematics function.

About the Author:

Value Technologies Pakistan is peerless Web Design Company and Web Hosting Company pledge to provide unsurpassed web designing and web hosting being a leading entity.

Vtech Live Chat

)
    is typing...
    Fill in the form below to send us a message.
    Send Message Cancel