When working with PHP, you may need to get the Unicode code point of a character. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.
mb_ord: The function reads the first Unicode character in the supplied encoding.
What Is mb_ord in PHP?
Get the Unicode code point of a character. The function reads the first Unicode character in the supplied encoding.
Syntax:
mb_ord(string $string, ?string $encoding = null): int|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: Invalid bytes or an unsupported encoding can yield false; check the return type before integer operations.
Example 1: Basic Usage
This example introduces the operation with a short and predictable input.
<?php
echo mb_ord("A", "UTF-8");
The code focuses on the main operation. The function reads the first Unicode character in the supplied encoding.
Example 2: Another Case
Now compare a different input, locale, date, or option to see another part of the behavior.
<?php
echo mb_ord("€", "UTF-8");
What changes? Invalid bytes or an unsupported encoding can yield false; check the return type before integer operations.
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
$characters = ["A", "Ω", "€"];
foreach ($characters as $character) {
echo $character . " = U+" . strtoupper(dechex(mb_ord($character, "UTF-8"))) . PHP_EOL;
}
The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. The function reads the first Unicode character in the supplied encoding.
Important Behavior and Edge Cases
- Core behavior: The function reads the first Unicode character in the supplied encoding.
- Inputs and results: Invalid bytes or an unsupported encoding can yield false; check the return type before integer operations.
- Alternatives: mb_chr() converts a Unicode code point into encoded text.
- Compatibility: mbstring is required for mb_* functions.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
mb_ord | Get the unicode code point of a character. |
| Related PHP approach | mb_chr() converts a Unicode code point into encoded text. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using an incorrect input or option | Invalid bytes or an unsupported encoding can yield false; check the return type before integer operations. |
| Confusing this API with a related operation | mb_chr() converts a Unicode code point into encoded text. |
| Assuming output is identical across environments | Check return values and required extensions or versions. mbstring is required for mb_* functions. |
Key Takeaways
- mb_ord helps you get the Unicode code point of a character.
- The function reads the first Unicode character in the supplied encoding.
- Invalid bytes or an unsupported encoding can yield false; check the return type before integer operations.
Use mb_ord when you need to get the Unicode code point of a character. 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.