How can the session ID be securely outputted in PHP?

When outputting the session ID in PHP, it is important to ensure that it is done securely to prevent potential security risks. One way to securely output the session ID is to use the session_regenerate_id() function to generate a new session ID and then output it using htmlspecialchars() function to sanitize any potentially malicious characters.

<?php
session_start();

// Regenerate session ID
session_regenerate_id();

// Output the session ID securely
echo htmlspecialchars(session_id());
?>