PHP iconv_get_encoding() Function

Definition
In PHP, iconv_get_encoding() returns the current iconv encoding settings: input, output and internal.
Call it with a type name to get one value, or with no argument to get all three in an array.
Syntax
Call iconv_get_encoding() with the values you need. Start with this line.
$enc = iconv_get_encoding("internal_encoding");
| Call | What it does |
|---|
iconv_get_encoding("internal_encoding") | Returns the internal encoding, for example "UTF-8". |
iconv_get_encoding() | Returns all three settings as an array. |
Since PHP 5.6 the defaults come from the default_charset setting, which is UTF-8 in most installs.
Library declarations
iconv_get_encoding(string $type = "all"): string|array|false
Parameters
| Parameter | Description |
|---|
| type | Optional. "all" (default), "input_encoding", "output_encoding" or "internal_encoding". |
Return Value
| Return Value | Description |
|---|
| string | The value of one setting. |
| array | An array with the keys input_encoding, output_encoding and internal_encoding when the type is "all". |
| false | Returned on failure. |
PHP Examples
Example 1 — Read the Internal Encoding
Print one setting. The output is typical for a UTF-8 server.
<?php
echo iconv_get_encoding("internal_encoding"), PHP_EOL;
How It Works
- Pass the type name.
internal_encoding is the encoding PHP uses inside iconv. - Get a string. It is usually
UTF-8. - Print it.
echo shows the name. - Expect other values on other servers. They depend on
php.ini.
Example 2 — Read All Settings
Call it with no argument to get an array. The output is typical.
<?php
print_r(iconv_get_encoding());
Array
(
[input_encoding] => UTF-8
[output_encoding] => UTF-8
[internal_encoding] => UTF-8
)How It Works
- Leave out the argument. The default is
all. - Get an array. It has three keys.
- Read each key. They tell you how PHP converts input, output and internal text.
- Use
print_r() to see the whole array.
Frequently Asked Questions
iconv_get_encoding() returns the current iconv encoding settings for input, output and internal text.
iconv_get_encoding(string $type = "all"): string|array|false. Pass a type name or nothing at all.
You can pass all, input_encoding, output_encoding or internal_encoding.
Since PHP 5.6 they come from the default_charset setting, which is UTF-8 by default.
iconv_set_encoding() can change them, but it is deprecated. Set default_charset in php.ini instead.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful