2011-12-30 55 views
1

我试图把YouTube的基本嵌入代码和使用不断抛出一个错误bbreak它分为高度宽度和url,但是代码IM的变量。有麻烦打破了YouTube的嵌入代码成变量

<?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>'; 
?> 

使用此代码即时得到以下错误

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

我在做什么错?我谷歌搜索,发现逃跑的东西,但不知道它期望我逃脱。

回答

3

第4行$ vid url,不能在那里有空格,这是一个语法错误。 所以将其更改为:

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

和最后一行应该是:

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

我不知道发生了什么与该网址我要么没有kow或没有看到它也谢谢你的帮助,我可以看到现在我怎么去这个+1代表 – rees92 2011-12-30 01:44:19

+0

@vdbuilder Psh,他已经接受了我的。哦,你需要的代表比我更:) – 2011-12-30 01:54:24

3

为了使用字符串里的变量,你需要在PHP中使用双引号。所以下面:

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

应该是:

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

你得到一个语法错误,因为你用单引号,然后结束他们之后却仍然有文字。

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

我只是tryed说它仍然是thro翼同样的错误也不是现在它对待我的变量作为文本,而不是PHP导致代码荧光笔灰色灰色与链接的其余部分一样 – rees92 2011-12-30 01:38:36

+0

@ user1121999这是因为我使用'$ vid'而不是'$ vid_url'。这就是你在你的例子中所做的。我编辑了我的答案。现在应该工作。 – 2011-12-30 01:42:33

0
<?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>"; 

?> 

使用双引号(“)在PHP中,您可以通过名字嵌入变量内容,作为脚本引擎解析双引号括起来的:你举的例子的代码也可以使用连接运算符.写我用双引号替换了单引号,然后转义了所有的双引号

您的代码示例的主要问题是变量不能包含空格,所以我用下划线替换了空格

0

你不能简单地通过把它们彼此相邻的源输入文件连接具有变量字符串。相反,使用.来串联值:

$ 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$ 
0

唯一缺少的连击=]

<?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>'; 
?> 

有用的链接