Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
PHP – Get or set the HTTP output character encoding with mb_http_output()
The mb_http_output() function in PHP is used to get or set the HTTP output character encoding. An output, after this function is called, will be converted from the set internal encoding to the specified encoding.
Syntax
string|bool mb_http_output(string $encoding = null)
Parameters
mb_http_output() accepts only a single parameter −
-
$encoding − It is used to set the HTTP output character encoding to the encoding. If the encoding is omitted, then mb_http_output() will return the current HTTP output character encoding.
Return Values
If the encoding is omitted, then the mb_http_output() function will return the current HTTP output character encoding. Otherwise, it returns True on success and False on failure.
Example 1: Getting Current HTTP Output Encoding
This example shows how to retrieve the current HTTP output character encoding ?
<?php // It will return the output character encoding $string = mb_http_output(); var_dump($string); ?>
string(5) "UTF-8"
Example 2: Setting HTTP Output Encoding
This example demonstrates how to set a specific HTTP output character encoding ?
<?php
// Set HTTP output encoding to ISO-8859-1
$result = mb_http_output("ISO-8859-1");
var_dump($result);
// Check the current encoding
echo "Current encoding: " . mb_http_output();
?>
bool(true) Current encoding: ISO-8859-1
Example 3: Handling Invalid Encoding
This example shows what happens when an invalid encoding is provided ?
<?php
// Try to set an invalid encoding
$result = mb_http_output("INVALID-ENCODING");
var_dump($result);
// Check if encoding changed
echo "Current encoding: " . mb_http_output();
?>
bool(false) Current encoding: UTF-8
Conclusion
The mb_http_output() function is useful for managing character encoding in web applications. Use it without parameters to get the current encoding, or with an encoding parameter to set a new one.
