如何将YouTube链接嵌入PHP

Keep in mind I am fairly new to PHP..

So what I am currently trying to do is pull information from a database (AirTable) and display it on a page (which so far I have succeeded in.) I need to embed a video alongside the numerical data, and it would be a different video for each set of data displayed. Not an issue, I have a field in the database that holds the video ID and a variable that calls that ID for each entry. What I am having trouble doing is embedding the YouTube video into the PHP code.

echo "<br>". $e["Test #"]."<br>". $e["RPM"]."<br>". $e["Axial Cut"]."<br>";

So the line of code I am using to embed videos is

<iframe width="250" height="140" src="http://www.youtube.com/embed/IDHERE?rel=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>

And this works outside of PHP, but when I try to add it to the echo, it comments out the code after the "http://" so what do I need to fix? It does need to go into the PHP, because I am replacing the part of the URL with the video ID variable so that the video pulls the proper one from the database for each entry.

Be careful to only use single quotes in your HTML tag when you use double quotes in the PHP code for echoing - or vice versa (otherwise the quotes for the HTML attributes will close the echoed string earlier than you want it to):

echo '<iframe width="250" height="140" src="http://www.youtube.com/embed/IDHERE?rel=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>';

EDIT/ADDITION after comment:

Together with your variable, that should be

echo '<iframe width="250" height="140" src="http://www.youtube.com/embed/'.$vidID.'?rel=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>';

(i.e. interrupt the echoed string with a single quote, add a dot, the variable name, another dot and continue the string again with another single quote) To answer

Try this:

<?php

$videoid = "ZKFwQFBwQFU"; // Your Video ID

echo "<iframe width=\"250\" height=\"140\" src=\"http://www.youtube.com/embed/" . $videoid . "?rel=0\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen></iframe>";

If you don't use \" around HTML attribute values, " will be interpreted as the end of string. See https://stackoverflow.com/a/11036433/6523409 for details.