rees92
rees92

Reputation: 65

having trouble breaking a youtube embed code up into variables

I'm trying to take the basic youtube embed code and bbreak it up into variables of height width and url but the code im using keeps throwing a error.

<?php
$width = "10";
$height = "20";
$vid url = "http://www.youtube.com/embed/HjgSmoilwV4";

echo '<iframe width="'$width'" height="'$height'" src="'$vid'" frameborder="0"allowfullscreen></iframe>';
?>

using this code im getting the following error

Parse error: syntax error, unexpected T_STRING in D:\webdesign\webserver\root\dynapage\scripts\admin\add_video.php on line 4

What am I doing wrong? I've Googled and found stuff on escaping but not sure what it expects me to escape.

Upvotes: 1

Views: 256

Answers (5)

vdbuilder
vdbuilder

Reputation: 12994

Line 4 $vid url , can't have that space there, it's a syntax error. so change it to:

$vid = "http://www.youtube.com/embed/HjgSmoilwV4";

and that last line should be:

echo "<iframe width='".$width."' height='".$height."' src='".$vid."' frameborder='0' allowfullscreen></iframe>";

Upvotes: 3

Chris Hutchinson
Chris Hutchinson

Reputation: 9212

<?php

$width = '10';
$height = '20';
$vid_url = 'http://www.youtube.com/embed/HjgSmoilwV4';

echo "<iframe width=\"$width\" height=\"$height\" src=\"$vid_url\" frameborder=\"0\" allowfullscreen></iframe>";

?>

Use of double-quote (") in PHP allows you to embed variable contents by name, as the scripting engine parses double-quoted strings for known patterns. I replaced the single quotes with double quotes and then escaped all of the double quotes.

The main problem with your code sample was that variables cannot include spaces, so I replaced your space with an underscore.

Upvotes: 0

Levi Morrison
Levi Morrison

Reputation: 19552

In order to use variables inside of strings, you need to use double quotes in PHP. So the following:

echo '<iframe width="'$width'" height="'$height'" src="'$vid_url'" frameborder="0"allowfullscreen></iframe>';

should be:

echo "<iframe width='$width' height='$height' src='$vid_url' frameborder='0' allowfullscreen></iframe>";

You are getting a syntax error because you used single quotes and then ended them but still have text after it. Your example code could also be written using the concatenation operator .:

echo '<iframe width="' . $width . '" height="' . $height . '" src="' . $vid_url . '" frameborder="0"allowfullscreen></iframe>';

Upvotes: 3

Vinicius Cainelli
Vinicius Cainelli

Reputation: 847

The only missing concatenate =]

<?php
    $width = "10";
    $height = "20";
    $vid url = "http://www.youtube.com/embed/HjgSmoilwV4";

    echo '<iframe width="'.$width.'" height="'.$height.'" src="'.$vid.'" frameborder="0" allowfullscreen></iframe>';
?>

Useful link

Upvotes: 0

sarnold
sarnold

Reputation: 104090

You cannot simply concatenate strings with variables by placing them next to each other on the source input file. Instead, use a . to concatenate the values:

$ php
<?php
echo 'one' 'two';
?>
PHP Parse error:  syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in - on line 2
$ php
<?php
echo 'one' . 'two';
?>
onetwo$ 

Upvotes: 0

Related Questions