Open In App

PHP | bcsub() Function

The bcsub() function in PHP is an inbuilt function and is used to subtract one arbitrary precision number from another. This function accepts two arbitrary precision numbers as strings and returns the subtraction of the two numbers after scaling the result to a specified precision.

Syntax:



string bcsub ( $num_str1, $num_str2, $scaleVal)

Parameters: This function accepts three parameters as shown in the above syntax and explained below:

Return Value: This function returns the subtraction of two numbers $num_str1 and $num_str2 as string.



Examples:

Input:  $num_str1 = 11.222, $num_str2 = 3
Output: 8
Since the parameter $scaleVal is not specified so
no digits after decimal is appeared in the 
result after subtraction.

Input:  $num_str1 = 11.222, $num_str2 = 3, $scaleVal = 4
Output: 8.2220

Below programs illustrate the bcsub() function in PHP :

Program 1:




<?php
// PHP program to illustrate bcsub() function
   
// input numbers with arbitrary precision
$num_str1 = "11.222";
$num_str2 = "3";
   
// calculates the subtraction of
// the two numbers when $scaleVal is
// not specified
$res = bcsub($num_str1, $num_str2);
  
echo $res;
   
?>

Output:

8

Program 2:




<?php
// PHP program to illustrate bcsub() function
   
// input numbers with arbitrary precision
$num_str1 = "11.222";
$num_str2 = "3";
  
// scale value
$scaleVal = 4;
   
// calculates the subtraction of the two
// numbers when $scaleVal is specified
$res = bcsub($num_str1, $num_str2, $scaleVal);
  
echo $res;
   
?>

Output:

8.2220

Reference:
http://php.net/manual/en/function.bcsub.php


Article Tags :