The PhoneNumber Object
ThePhoneNumber class is the core data structure in libphonenumber for PHP. It represents a parsed phone number with all its components and metadata.
Object Structure
APhoneNumber object contains the following key properties:
| Property | Type | Description |
|---|---|---|
countryCode | int | The country calling code (e.g., 1 for US/Canada, 44 for UK) |
nationalNumber | string | The national significant number without formatting |
extension | string | Optional phone extension (up to 40 digits) |
italianLeadingZero | bool | Whether the number has a leading zero (for certain countries) |
numberOfLeadingZeros | int | Count of leading zeros (default: 1) |
rawInput | string | The original input string before canonicalization |
countryCodeSource | CountryCodeSource | How the country code was derived |
preferredDomesticCarrierCode | string | Carrier selection code for domestic calls |
Example PhoneNumber Object
When you parse a UK phone number:Accessing Phone Number Properties
ThePhoneNumber class provides getter methods for all properties:
Phone Number Extensions
Phone extensions are stored as strings to accommodate leading zeros:Extensions can be up to 40 digits long, though most are much shorter in practice.
Italian Leading Zeros
Some countries (notably Italy) have numbers where leading zeros are significant when dialing internationally:Phone Number Types
The library categorizes phone numbers into different types defined by thePhoneNumberType enum:
Available Types
- Common Types
- Special Types
- Other Types
| Type | Value | Description |
|---|---|---|
FIXED_LINE | 0 | Traditional landline numbers |
MOBILE | 1 | Mobile/cellular numbers |
FIXED_LINE_OR_MOBILE | 2 | Cannot distinguish between fixed-line and mobile |
TOLL_FREE | 3 | Freephone lines (e.g., 1-800 numbers) |
Determining Number Type
UsegetNumberType() to identify a phone number’s type:
FIXED_LINE_OR_MOBILE Type
In some regions (notably the USA and Canada), it’s impossible to distinguish between fixed-line and mobile numbers by examining the number itself:The
FIXED_LINE_OR_MOBILE type is never returned by getSupportedTypesForRegion() as it’s a convenience type representing ambiguity.Checking Supported Types by Region
Find out which phone number types are available in a specific region:Geographical vs Non-Geographical Numbers
Some phone numbers have a geographical association:Which number types are geographical?
Which number types are geographical?
FIXED_LINE- Always geographicalMOBILE- Geographical in some countries (e.g., Argentina, Brazil, China, Mexico)FIXED_LINE_OR_MOBILE- Considered possibly geographical- All other types are non-geographical
Working with Different Types
Getting Example Numbers
Retrieve example numbers for testing:Type-Specific Validation
Check if a number is possible for a specific type:Best Practices
The phone number type is determined by pattern matching against metadata. It does not verify if the number is currently in use or assigned to a specific carrier.
Comparing Phone Numbers
Use theequals() method to compare two PhoneNumber objects:
String Representation
Convert aPhoneNumber to a readable string for debugging: