1

I am a noob to php and coding in general so pardon my ignorance. I am trying to play a wav file from a php if statement. When I echo html for the sound I get this error.

PHP Parse error: syntax error, unexpected '" type="' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';' in /var/www/html/colums.php on line 67

line 67 being

        echo "<audio autoplay><source src="sounds/beep.wav" type="audio/wav" 
        autoplay></audio>";

Here is my code:

    <?php
    $result = $db->query("SELECT * FROM my_table WHERE sound=1");
    while($row = $result->fetch_assoc()){
    echo $row['sound'];
    }
    $sound=$row['sound'];
    if ($sound = "1"){
            echo "<audio autoplay><source src="/sounds/beep.wav" 
    type="audio/wav" autoplay></audio>";

    }
    ?>

Thanks for your help

Shadow
  • 33,525
  • 10
  • 51
  • 64
Barrett K
  • 13
  • 3

3 Answers3

1

the problem within you code lies inside of the quotes. in lots of languages(php included) you can't embed quotes inside of quotes. to reslove your problem you need to add a backslash before every quote inside the quotes that make up the echo. example: echo "";

Nick
  • 69
  • 5
0

The problem from what I can see at a quick glance is in your if statement.

 if ($sound = "1"){

In php a single "=" is assignment of value. "==" and "===" is for comparing the value. So, right now you are assigning the value of 1 to $sound technically. To fix the problem you should just need to replace it with double equals.

if ($sound == "1"){

Also refer to this question for more information.

nickzor
  • 84
  • 7
  • Thanks for the reply and helpful tip. The problem was more related to Nick's suggestion. But I needed to fix the == too. Thanks for the link. – Barrett K May 27 '18 at 06:14
0

Found several mistakes in your code ,

  1. You cannot assign $row['sound'] out side the loop. either it should be $row[0]['sound'] or $sound variable should be inside the while loop.
  2. if($sound = 1) will not return error , but logically it's incorrect , it should be ($sound == 1)
  3. you cannot use " inside a string if the string is quoted by " , ( same for ' ). you need to add slashes like \" or \'

Suppose below code will works...

        <?php
    $result = $db->query("SELECT * FROM my_table WHERE sound=1");
    while($row = $result->fetch_assoc()){
    echo $row['sound'];
    $sound=$row['sound'];
    if ($sound == 1){
            echo "<audio autoplay><source src=\"/sounds/beep.wav\" type=\"audio/wav\" autoplay></audio>";
}}
    ?>