PHP mb_http_output() Function
Last Updated :
04 Oct, 2023
The mb_http_output() function is an inbuilt function in PHP that is used to set and retrieve the character encoding. This function allows you to specify a character encoding for HTTP response.
Syntax:
mb_http_output($encoding )
Parameters: This function accepts only one parameter which is described below.
- $encoding: This is the optional parameter that specifies the character encoding.
Return Value: This function returns the boolean value if the function successfully executes, it will return “true” otherwise it will return false. If the $encoding removes the function, it will be used by default encoding.
Program 1: The following program demonstrates the mb_http_output() function.
PHP
<?php
$encoding = mb_http_output();
echo "Current HTTP output encoding: " . $encoding ;
?>
|
Output
Current HTTP output encoding: UTF-8
Program 2: The following program demonstrates the mb_http_output() function.
PHP
<?php
$encoding = "UTF-8" ;
$condition = true;
function custom_mb_http_output( $encoding , $condition ) {
if (!in_array( $encoding , mb_list_encodings())) {
return false;
}
if ( $condition ) {
mb_http_output( $encoding );
return true;
} else {
return false;
}
}
if (custom_mb_http_output( $encoding , $condition )) {
echo "Character encoding set to $encoding for HTTP output." ;
} else {
echo "Failed to set character encoding for HTTP output." ;
}
?>
|
Output:
Character encoding set to UTF-8 for HTTP output.
Reference: https://www.php.net/manual/en/function.mb-http-output.php
Share your thoughts in the comments
Please Login to comment...