Lynob Lynob - 6 months ago 34
PHP Question

How to echo php array in html5 audio and video players?

I have two php arrays, array a contain strings representing paths for mp3 files on server. Array b contains strings representing paths for mp4 files.

Lets take this simple example

$test = 'a.mp3';
$var = json_encode($test);
echo '<audio controls>';
echo '<source src=<?php $var ?> type="audio/mpeg">';
echo 'Your browser does not support the audio element.';
echo '</audio>';


this didn't work. This didn't work either

$test = 'a.mp3';
$var = json_encode($test);
echo '<audio controls>';
echo '<source src=$var type="audio/mpeg">';
echo 'Your browser does not support the audio element.';
echo '</audio>';


With quotes, without quotes, nothing works. I even tried without
json_encode
and didn't work obviously. so how to echo variables into html5 players? I will then be able to loop through the array, generating a playlist.

Answer
 $test = 'a.mp3';
 $var = json_encode($test);
echo '<audio controls>';
echo "<source src=$var type='audio/mpeg'>";
echo 'Your browser does not support the audio element.';
echo '</audio>';

Use double quote when you want to print the variable

Comments