What are some best practices for automatically embedding videos on a website using PHP?

When embedding videos on a website using PHP, it's important to ensure that the videos are responsive and load efficiently. One best practice is to use a video hosting service like YouTube or Vimeo, which provide embed codes that automatically handle responsive design and video playback. Another best practice is to use PHP to dynamically generate the embed code based on the video ID or URL provided.

<?php
function embedVideo($videoUrl) {
    // Extract video ID from URL
    $videoId = ''; // Code to extract video ID from URL

    // Generate embed code
    $embedCode = '<iframe width="560" height="315" src="https://www.youtube.com/embed/' . $videoId . '" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>';

    return $embedCode;
}

// Usage
$videoUrl = 'https://www.youtube.com/watch?v=VIDEO_ID';
echo embedVideo($videoUrl);
?>