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 // Get the current HTTP output character encoding $encoding = mb_http_output(); echo "Current HTTP output encoding: " . $encoding ; ?> |
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 ) { // Validate the encoding parameter if (!in_array( $encoding , mb_list_encodings())) { return false; // Invalid encoding } // Conditionally set the character encoding // based on a condition if ( $condition ) { mb_http_output( $encoding ); return true; // Encoding set successfully } else { return false; // Encoding not set } } 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