Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Bcmath Compat

Bcmath Compat Laravel Package

phpseclib/bcmath_compat

BCMath compatibility layer for phpseclib, providing pure-PHP replacements for bcmath functions when the ext-bcmath extension isn’t available. Helps ensure consistent big integer math support across environments for cryptography and related operations.

Deep Wiki
Context7

phpseclib/bcmath_compat is a lightweight compatibility layer that provides BCMath-style arbitrary-precision math functions in pure PHP. It’s designed for environments where the BCMath extension isn’t available, helping crypto- and math-heavy libraries keep working consistently across hosting setups.

Drop it in as a dependency to get familiar bc* operations backed by PHP implementations, improving portability without changing calling code.

  • Pure PHP BCMath function replacements (e.g., bcadd, bcsub, bcmul, bcdiv)
  • Helps libraries run when the bcmath extension is missing
  • Predictable big-number behavior for cryptography and large integer math
  • Simple Composer install; minimal integration effort
Frequently asked questions about Bcmath Compat
How do I install phpseclib/bcmath_compat in a Laravel project?
Run `composer require phpseclib/bcmath_compat` in your project root. The package auto-loads via Composer’s PSR-4 standard, so no additional configuration is needed unless you’re using a custom autoloader. For Laravel, you can optionally bind it to the service container for centralized control.
Will this break existing code that uses bcmath functions like bcadd() or bcdiv()?
No, the package is designed as a drop-in replacement. If the bcmath extension is unavailable, it transparently intercepts calls to bc* functions and routes them to pure-PHP implementations. Test critical paths to confirm precision matches your expectations, especially for financial or cryptographic operations.
Does phpseclib/bcmath_compat work with Laravel 8/9/10 and PHP 8.x?
Yes, the package supports PHP 7.4+ and is compatible with Laravel 8, 9, and 10. It leverages modern PHP features like namespaced functions and type hints. Always check the package’s Composer constraints for the latest supported versions, as they may evolve with updates.
How does the performance compare to native bcmath?
Pure-PHP implementations are significantly slower—expect 10x to 100x slower operations depending on the function and input size. For cryptography or bulk operations, benchmark critical paths and consider caching results (e.g., using Laravel’s cache) or enabling the bcmath extension if possible.
Can I use this alongside the bcmath extension? Will it cause conflicts?
Yes, the package is safe to use even when bcmath is enabled. It acts as a fallback only when the extension is missing. To dynamically switch between native and pure-PHP implementations, register a service provider that checks `extension_loaded('bcmath')` and resolves the appropriate backend.
What libraries or Laravel features rely on bcmath, and will this package help?
Libraries like phpseclib (for cryptography), paragonie/random_compat (for secure randomness), and custom financial/math logic often depend on bcmath. This package ensures those libraries work in environments without the extension, such as shared hosting or Docker containers with minimal PHP extensions.
How can I test if the package is working correctly in my Laravel app?
Write unit tests comparing outputs of bc* functions against known values (e.g., `bcdiv('1', '3', 10)` should return `0.3333333333`). Use PHPUnit’s assertions or a helper function to validate precision. For integration tests, mock the bcmath extension using tools like `php -dextension=bcmath.so` or `php -dextension=none` to simulate missing extensions.
Are there alternatives to phpseclib/bcmath_compat for BCMath compatibility?
Yes, you could use the GMP extension (faster than bcmath) as a primary backend with this package as a fallback. Alternatively, libraries like `bmath` or `mathphp` offer similar functionality but may not be as widely adopted. Evaluate tradeoffs like performance, precision, and Laravel ecosystem integration before choosing.
How do I handle precision errors or rounding differences between native bcmath and this package?
Validate outputs against native bcmath for edge cases (e.g., division rounding, large integers). Use a tolerance threshold in tests or log warnings when discrepancies exceed acceptable limits. For financial applications, consider using a dedicated decimal library like `briannesbitt/decimal` alongside this package.
Should I use this package in production if my app only uses bcmath for non-critical operations?
If bcmath is only used for non-performance-critical paths (e.g., logging, non-financial calculations), the package is a low-risk solution. However, benchmark the pure-PHP implementation to ensure it meets your latency requirements. For high-throughput systems, prioritize enabling the bcmath extension or optimizing critical paths.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport