PHP变量未以单引号传递 [英] PHP variables not passed in single quotations
问题描述
我正在尝试将变量$user
定义为页面所在目录的名称,该变量定义为页面所在目录的名称.
I am trying to pass a variable $user
which is defined as the name of the directory the page is in to an iframe on the page.
变量$user
应该将用户名传递给iframe,以便iframe可以下载用户的minecraft外观并显示它.当我用用户名替换iframe中的变量但iframe将$user
识别为$user
...
The variable $user
is supposed to pass the username to the iframe so the iframe can download the user's minecraft skin and display it. This code works when I replace the variable in the iframe with a username but the iframe recognizes $user
as $user
...
主页:
<link rel="stylesheet" type="text/css" href="/css/index.css" />
<?php
$path = DIRNAME($_SERVER['PHP_SELF']);
$position = STRRPOS($path,'/') + 1;
$user= SUBSTR($path,$position);
$root="/";
include ($_SERVER['DOCUMENT_ROOT'].'/menu/nav.php');
echo '<iframe src="skin.php?user=$user" width="200" height="400" align="left"/>'
?>
iframe:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php $user = $_GET['user']; ?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script src="/js/excanvas.js" type="text/javascript"></script>
<script src="/js/minecraftskin.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
$(".mc-skin").minecraftSkin();
});
</script>
</head>
<style>
* {
margin:0;
padding:0;
}
div {
display:inline-block;
}
.minecraft_head .head {
display:none;
}
.minecraft_head:hover .hat {
display:none;
}
.minecraft_head:hover .head {
display:inline-block;
}
.scratch {
display:none;
}
</style>
<body>
<span class="mc-skin" data-minecraft-username="<?php echo $user; ?>"></span>
</body>
</html>
我该如何纠正?
推荐答案
PHP不会解析单引号内的变量,请更改此行:
PHP doesn't parse variables within single quotation marks, change this line:
echo '<iframe src="skin.php?user=$user" width="200" height="400" align="left"/>'
为此(连续的字符串样式):
to this (concantenated string style):
echo '<iframe src="skin.php?user=' .$user. '" width="200" height="400" align="left"/>'
或以下(使用双引号的内联变量样式):
or this (in-line variable style using double quotations):
echo "<iframe src='skin.php?user=$user' width='200' height='400' align='left'/>"
这篇关于PHP变量未以单引号传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!