Php 无法将youtube嵌入代码分解为变量

Php 无法将youtube嵌入代码分解为变量,php,youtube,Php,Youtube,我试图将基本的youtube嵌入代码分解为高度、宽度和url变量,但我使用的代码不断抛出错误 <?php $width = "10"; $height = "20"; $vid url = "http://www.youtube.com/embed/HjgSmoilwV4"; echo '<iframe width="'$width'" height="'$height'" src="'$vid'" frameborder="0"allowfullscreen></if

我试图将基本的youtube嵌入代码分解为高度、宽度和url变量,但我使用的代码不断抛出错误

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

使用此代码,我得到以下错误

分析错误:语法错误,第4行的D:\webdesign\webserver\root\dynapage\scripts\admin\add\u video.php中出现意外的T\u字符串


我做错了什么?我在谷歌上搜索并找到了关于转义的内容,但不确定它希望我转义什么。

为了在字符串中使用变量,需要在PHP中使用双引号。因此:

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

为了在字符串中使用变量,您需要在PHP中使用双引号。因此:

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

在PHP中使用双引号(“)可以按名称嵌入变量内容,因为脚本引擎会解析已知模式的双引号字符串。我用双引号替换了单引号,然后转义了所有双引号

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


在PHP中使用双引号(“)允许您按名称嵌入变量内容,因为脚本引擎将双引号字符串解析为已知模式。我用双引号替换了单引号,然后转义了所有双引号


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

第4行$vid url,不能有那个空间,这是一个语法错误。 因此,将其更改为:

$vid = "http://www.youtube.com/embed/HjgSmoilwV4";
最后一行应该是:

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

第4行$vid url,不能有那个空间,这是语法错误。 因此,将其更改为:

$vid = "http://www.youtube.com/embed/HjgSmoilwV4";
最后一行应该是:

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

不能简单地通过将字符串与变量相邻放置在源输入文件中来连接字符串。相反,请使用
连接值:

$ 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$ 
$php
PHP分析错误:语法错误,意外的T_常量_封装的_字符串,应为“,”或“;”在线2
$php
一两美元

不能简单地通过将字符串与变量相邻放置在源输入文件中来连接字符串。相反,请使用
连接值:

$ 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$ 
$php
PHP分析错误:语法错误,意外的T_常量_封装的_字符串,应为“,”或“;”在线2
$php
一两美元

唯一缺少的连接=]

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

有用链接


    • 唯一缺少的连接=]

      <?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>';
      ?>
      
      
      
      有用链接


      我刚刚尝试过,但它仍然抛出相同的错误,现在它不是将我的变量视为文本而不是php吗?因为代码高亮显示会使其变灰,与其他代码一样link@user1121999这是因为我使用了
      $vid
      而不是
      $vid\u url
      。这就是你在例子中所做的。我编辑了我的答案。现在应该可以工作了。我刚刚试过,它仍然会抛出相同的错误,不是吗?它现在将我的变量视为文本而不是php,因为代码高亮显示会使其变灰,与其他代码一样link@user1121999这是因为我使用了
      $vid
      而不是
      $vid\u url
      。这就是你在例子中所做的。我编辑了我的答案。我不知道那个网址发生了什么事,我不知道或者没有看到它也谢谢你的帮助,我现在可以知道我是怎么做的了rep@vdbuilder他已经接受了我的。哦,好吧,你比我更需要这个代表:)我不知道那个网址出了什么事,我要么不知道,要么没看到,也谢谢你的帮助,我现在可以知道我是怎么做的了+1rep@vdbuilder他已经接受了我的。哦,你比我更需要代表:)