What role does the character encoding play in ensuring proper display of special characters in PHP output?

Character encoding plays a crucial role in ensuring proper display of special characters in PHP output. If the encoding is not set correctly, special characters may appear as gibberish or squares on the screen. To solve this issue, it is important to set the correct character encoding in both the PHP file and the HTML output.

<?php
header('Content-Type: text/html; charset=utf-8');
echo '<html><head><meta charset="utf-8"></head><body>';
echo 'Special characters like é, ü, and ñ will display correctly.';
echo '</body></html>';
?>