Thursday, September 4, 2025
HomeLanguagesPHP mb_http_output() Function

PHP mb_http_output() Function

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;
  
?>


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) {
  
    // 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

RELATED ARTICLES

Most Popular

Dominic
32261 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS