Saturday, December 28, 2024
Google search engine
HomeLanguagesPHP | bccomp() Function

PHP | bccomp() Function

The bccomp() function in PHP is an inbuilt function and is used to compare two arbitrary precision numbers. This function accepts two arbitrary precision numbers as strings and returns the result of comparison of the two numbers after comparing them upto a specified precision.

Syntax:

int bccomp ( $num_str1, $num_str2, $scaleVal)

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

  • $num_str1: This parameter is of string type and represents the left operand or one of the two numbers among which we want to perform the comparison. This parameter is mandatory.
  • $num_str2: This parameter is of string type and represents the right operand or one of the two numbers among which we want to perform the comparison. This parameter is mandatory.
  • $scaleVal: This parameter is of int type and is optional. This parameter tells the number of digits after decimal places that will be used in comparison. The default value of this parameter is zero.

Return Value: This function returns an integral value based on the comparison of the two numbers $num_str1 and $num_str2. If both of the numbers are equal then this function returns zero. If $num_str1 is greater than $num_str2 this function returns 1 and if $num_str1 is less than $num_str2 this function returns -1.

Examples:

Input:  $num_str1 = 3.22, $num_str2 = 3
Output: 0
Explanation: Since the parameter $scaleVal is not 
specified so no digits after decimal is used in 
comparison. So, the value of first parameter which 
is 3.22 will be treated as 3 and hence both 
parameters are equal.

Input:  $num_str1 = 3.222, $num_str2 = 3, $scaleVal = 2
Output: 1

Input:  $num_str1 = 3, $num_str2 = 3.222, $scaleVal = 2
Output: -1

Below programs illustrate the bccomp() function in PHP :

Program 1:




<?php
// PHP program to illustrate bccomp() function
   
// input numbers
$num_str1 = "3.12";  
$num_str2 = "3";  
  
// calculates the comparison of the two 
// numbers when $scaleVal is not specified
$res = bccomp($num_str1, $num_str2, 2);
  
// both parameters are equal
echo $res;
   
?>


Output:

0

Program 2:




<?php
// PHP program to illustrate bccomp() function
   
// input numbers
$num_str1 = "3.12";  
$num_str2 = "3";  
  
// scale value
$scaleVal = 2;
  
// calculates the comparison of the two 
// numbers when $scaleVal is specified
$res = bccomp($num_str1, $num_str2, $scaleVal);
  
// first parameter is greater than second
echo $res;
   
?>


Output:

1

Program 3:




<?php
// PHP program to illustrate bccomp() function
   
// input numbers
$num_str1 = "3";  
$num_str2 = "3.12";  
  
// scale value
$scaleVal = 2;
  
// calculates the comparison of the two 
// numbers when $scaleVal is specified
$res = bccomp($num_str1, $num_str2, $scaleVal);
  
// first parameter is smaller than second
echo $res;
   
?>


Output:

-1

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

Last Updated :
19 Apr, 2018
Like Article
Save Article

<!–

–>

Similar Reads
Related Tutorials
RELATED ARTICLES

Most Popular

Recent Comments