HTML embed video using PHP variable doesn't work

how to display php variable in html form
pass php variable to html form
access php variable in html
how to use html variable in php
php variable in string
assign html value to php variable
convert html variable to php
embed youtube video in php

I'm trying to display all videos a webpage using the following code, I've gotten as far as being able to iterate over the files, printing the file names and embedding a video. However the videos are greyed out and don't work, I suspect I did something wrong with using $filename in the code.

$dir = new DirectoryIterator(dirname(__FILE__));
foreach ($dir as $filename) {
    if (!$filename->isDot()) {

        if ($filename != "index.php" and $filename != "error_log") {

            echo $filename, "<br>"; 

            echo '<video width="400" controls="controls" preload="metadata">
            <source src=$filename type="video/mp4"></video>';

            echo "<br><br>";


This is how it shows up:


You are echo $filename like a string, not a PHP variable.

Remember, if echo with single quotes ' all inside it will echo like string and echo with double quotes " will echo PHP variables reading their value. Informations about strings in PHP are nicley explained e.g. here .

You can change your piece of code witch echo HTML video to:

echo '<video width="400" controls="controls" preload="metadata"><source src="' . $filename . '" type="video/mp4"></video>';

Note that there were also missing double quotes for HTML video tag src property and I added them in code above.


insert variable into video player via php, @garyjohnson. What I want is for when the user clicks on one of their many video hyperlinks to have them redirected to the browseplayer.html  The controls attribute adds video controls, like play, pause, and volume. It is a good idea to always include width and height attributes. If height and width are not set, the page might flicker while the video loads. The <source> element allows you to specify alternative video files which the browser may choose from. The browser will use the first recognized format.


<source src=$filename type="video/mp4"></video>';


<source src="'.$filename.'" type="video/mp4"></video>';

PHP and HTML - Manual, How can I pass a variable from Javascript to PHP? In order to specify a random string, you must include it in double quotes, and technology, and since HTTP is a "stateless" protocol, the two languages cannot directly share variables. The .htaccess file must be in the same directory with your html file. If you can't found the .htaccess file although you already turning on the show hidden file option, you can create new .htaccess file and include AddHandler mentioned above in the file.

Your php variable is inside single quotes and (from the doc) variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.

As an alternative you could keep your string between single quotes and make use of sprintf which will return a formatted string and use for example %s to specify that the argument should be handled as string type.

echo sprintf('<video width="400" controls="controls" preload="metadata">
<source src="%s" type="video/mp4"></video>',

YouTube HTML5 Embed from URL with PHP, Just a quick reference post for how to embed a YouTube video from a URL string with PHP. <iframe id="ytplayer" type="text/html" width="<?php echo $width ?>  As you can see, you can use any HTML you want without doing anything special or extra in your PHP file, as long as it's outside and separate from the PHP tags. In other words, if you want to insert PHP code into an HTML file, just write the PHP anywhere you want (so long as they're inside the PHP tags).

using php variables in html, With PHP, there are two basic ways to get output: echo and print . output text with the echo command (notice that the text can contain HTML markup): The following example shows how to output text and variables with the echo statement: and examples are constantly reviewed to avoid errors, but we cannot warrant full  The rules of adding a PHP variable inside of any MySQL statement are plain and simple: Any variable that represents an SQL data literal , (or, to put it simply - an SQL string, or a number) must be added through a prepared statement .

PHP Echo and Print Statements, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  In your code in the page load event, or which ever event you want to display the variable: test.Text = variable.ToString(); This is C# code, but you should be able to figure out vb.

CSS Variables - var() Function, The <var> tag also supports the Event Attributes in HTML. Related Pages. HTML tutorial: HTML Text Formatting. HTML DOM reference: Variable Object. Default  embedded variable in HTML. PHP Forums on Bytes. I discovered that I could embed php variables in html using this But I think this doesn't work with php