When working with PHP, you may need to read a localized currency amount and ISO currency code. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.
NumberFormatter::parseCurrency: The currency code is written into a referenced variable and the amount is returned.
What Is NumberFormatter::parseCurrency in PHP?
Read a localized currency amount and ISO currency code. The currency code is written into a referenced variable and the amount is returned.
Syntax:
$formatter->parseCurrency(string $string, string &$currency, ?int &$offset = null): float|false
Parameters and return value: The signature above identifies the arguments and return type where the API has a single call. For topic guides, follow the sequence of calls in the examples. Always distinguish a returned value from an object or variable modified by reference.
Important: A parsed currency string is not proof that a payment is valid. Do not confuse parsing with conversion between currencies.
Example 1: Basic Usage
This example introduces the operation with a short and predictable input.
<?php
$formatter = new NumberFormatter("en_US", NumberFormatter::CURRENCY);
$currency = "";
var_dump($formatter->parseCurrency("$12.50", $currency), $currency);
The code focuses on the main operation. The currency code is written into a referenced variable and the amount is returned.
Example 2: Another Case
Now compare a different input, locale, date, or option to see another part of the behavior.
<?php
$formatter = new NumberFormatter("en_US", NumberFormatter::CURRENCY);
$currency = "";
var_dump($formatter->parseCurrency("$99.95", $currency), $currency);
What changes? A parsed currency string is not proof that a payment is valid. Do not confuse parsing with conversion between currencies.
Practical Example: Applying It in an Application
Use the following separate example as a starting point for a small application task. Check any extension or environment requirements below before running it.
<?php
$formatter = new NumberFormatter("en_US", NumberFormatter::CURRENCY);
$rawPrice = "$42.00";
$currencyCode = "";
$amount = $formatter->parseCurrency($rawPrice, $currencyCode);
if ($amount !== false) {
echo $currencyCode . ": " . $amount;
}
The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. The currency code is written into a referenced variable and the amount is returned.
Important Behavior and Edge Cases
- Core behavior: The currency code is written into a referenced variable and the amount is returned.
- Inputs and results: A parsed currency string is not proof that a payment is valid. Do not confuse parsing with conversion between currencies.
- Alternatives: NumberFormatter::formatCurrency() formats an amount and a currency code.
- Compatibility: The intl extension is required.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
NumberFormatter::parseCurrency | Read a localized currency amount and iso currency code. |
| Related PHP approach | NumberFormatter::formatCurrency() formats an amount and a currency code. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using an incorrect input or option | A parsed currency string is not proof that a payment is valid. Do not confuse parsing with conversion between currencies. |
| Confusing this API with a related operation | NumberFormatter::formatCurrency() formats an amount and a currency code. |
| Assuming output is identical across environments | Check return values and required extensions or versions. The intl extension is required. |
Key Takeaways
- NumberFormatter::parseCurrency helps you read a localized currency amount and ISO currency code.
- The currency code is written into a referenced variable and the amount is returned.
- A parsed currency string is not proof that a payment is valid. Do not confuse parsing with conversion between currencies.
Use NumberFormatter::parseCurrency when you need to read a localized currency amount and ISO currency code. Start with the first example, compare the second, and adapt the practical example to your inputs. Confirm the return value and environment requirements before adding it to production code.