How can UTF-8 encoding be effectively utilized in PHP to handle special characters like Turkish Umlauts in database operations?

When handling special characters like Turkish Umlauts in database operations in PHP, it is important to ensure that the encoding is set to UTF-8 to properly store and retrieve these characters. This can be achieved by setting the character set to UTF-8 in the database connection and using functions like utf8_encode() and utf8_decode() to handle the encoding and decoding of special characters.

// Set UTF-8 encoding for database connection
$dsn = 'mysql:host=localhost;dbname=my_database;charset=utf8';
$username = 'username';
$password = 'password';
$options = array(
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);

try {
    $pdo = new PDO($dsn, $username, $password, $options);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

// Example of encoding and decoding special characters
$text = 'İstanbul'; // Turkish word with Umlaut
$encoded_text = utf8_encode($text);
$decoded_text = utf8_decode($encoded_text);

echo $decoded_text; // Output: İstanbul