How to Play a Sound File Using PHP

By Kristen Waters

It is possible to embed a sound file in a web page using a combination of PHP and HTML. PHP is a server-side language, which means that all the processing it performs takes place on the web server hosting the website. In order to get the sound file to play on the client side--the visitor's computer--you must insert a reference to the audio file in the HTML code for the web page. The HTML generated by the PHP code will prompt the web browser to activate an audio application, which then plays the audio file through the computer's speakers.

Things You'll Need

  • Computer
  • Text editor

Step 1

Open a text editor, such as Microsoft Windows Notepad.

Step 2

Start your PHP code by typing "

Step 3

To store the name of your sound file in a variable, type the line... $file='example.mp3';

Step 4

Type the line ...echo "";... to insert a reference in the web page that will prompt the web browser to play the sound file once the page loads. The "" tag is actually HTML code that will be embedded by the PHP script.

Step 5

To conclude the PHP script, type the characters "?>". The entire script should read:";?>

Step 6

Save and close the file.

Tips & Warnings

  • You can also place a link to the music file in the web page by adding this code to your PHP script:
  • echo "Play Music";