What is bcadd in PHP?

bcadd is a function in PHP that adds two arbitrary precision numbers.

Syntax

bcadd(string $num1, string $num2, int $scale = null): string

Parameters

  • num1 - variable of type string.
  • num2 - variable of type string.
  • scale - variable of type int. This is an optional parameter. scale is used to set the number of digits after the decimal place in the result. The default value of scale is 0.

Return value

The bcadd function returns the sum of num1 and num2. The value returned is of type string.

Code

<?php
$num1 = '1.34';
$num2 = '4';
echo bcadd($num1, $num2);
echo bcadd($num1, $num2, 2);
?>

Output

5
5.34
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources