Double引文中的$ _SESSION []

时间:2011-10-24 15:51:00

标签: php variable-subsitution

$_SESSION['result'] = '2011-08-14 20:34:12';

echo $dateTime = "$_SESSION['result'] +1 hour";

期待输出:'2011-08-14 20:34:12 +1小时'

我知道双引号有错误,但不知道如何修复它。谁能帮我吗?非常感谢任何人都能给出一些解释,谢谢!

7 个答案:

答案 0 :(得分:4)

$_SESSION['result'] = '2011-08-14 20:34:12';

$dateTime = "{$_SESSION['result']} +1 hour";

echo($dateTime);

答案 1 :(得分:2)

使用此:

$dateTime = "{$_SESSION['result']} +1 hour";

或者这个:

$dateTime = $_SESSION['result'] . " +1 hour";

然后

echo($dateTime);

答案 2 :(得分:1)

您可以连接字符串

echo $dateTime = $_SESSION['result']." +1 hour";

答案 3 :(得分:1)

我建议你阅读strings in the PHP docs。你想要的是连接。

$_SESSION['result'] = '2011-08-14 20:34:12';

$dateTime = $_SESSION['result'] . ' +1 hour';

echo $dateTime;

在设置内容后,还要注意要回显$dateTime的最后一行。

答案 4 :(得分:1)

您可以在PHP: Array - Array do's and don'ts下找到许多如何访问数组元素的示例。

$arr = array('foo'=>1234, 'bar'=>array('baz'=>'abcdef'));

// simply no quotes within double-quoted string literals
echo "foo: $arr[foo]\n";
// echo "foo: $arr[bar][baz]\n"; <- doesn't work as intended

// curly-braces -> same syntax as outside of a string literal
echo "foo: {$arr['foo']}\n";
echo "foo: {$arr['bar']['baz']}\n";

// string concatenation
echo "foo: ". $arr['foo'] ."\n";
echo "foo: ". $arr['bar']['baz'] ."\n";

// printf with placeholder in format string
printf("foo: %s\n", $arr['foo']);
printf("foo: %s\n", $arr['bar']['baz']);

// same as printf but it returns the string instead of printing it
$x = sprintf("foo: %s\n", $arr['foo']);
echo $x;
$x = sprintf("foo: %s\n", $arr['bar']['baz']);
echo $x;

答案 5 :(得分:0)

在您的情况下,对字符串使用单引号,对变量不使用任何内容。

echo $dateTime = $_SESSION['result'].' +1 hour';

答案 6 :(得分:0)

  

$ _ SESSION ['result'] ='2011-08-14 20:34:12';

     

echo $ dateTime = $ _SESSION ['result']。 “+1小时”;

尝试以上方法。