What are the best practices for handling character encoding in PHP scripts, particularly when dealing with form submissions?

When dealing with form submissions in PHP scripts, it is important to ensure that character encoding is handled correctly to prevent issues with special characters and data corruption. One best practice is to always set the correct character encoding in both the HTML form and the PHP script. This can be done by setting the "accept-charset" attribute in the HTML form to "UTF-8" and using the mb_internal_encoding() function in the PHP script to set the internal character encoding to UTF-8.

// Set the character encoding in the HTML form
<form action="submit.php" method="post" accept-charset="UTF-8">
    <!-- Form fields here -->
</form>

// Set the character encoding in the PHP script
<?php
mb_internal_encoding("UTF-8");

// Process form submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Handle form data here
}
?>