$_SESSION[] inside of Double quotation

1.2k views Asked by At
$_SESSION['result'] = '2011-08-14 20:34:12';

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

Expect output: '2011-08-14 20:34:12 +1 hour'

I know there is error on the double quotation, but don't know how to fix it. Can anyone help me out? It would be really appreciate anyone can give some explanation, Thanks!

7

There are 7 answers

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

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

echo($dateTime);
3
Kurt Funai On

You can concatenate the string

echo $dateTime = $_SESSION['result']." +1 hour";
5
Aurelio De Rosa On

Use this:

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

or this:

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

and then

echo($dateTime);
0
Shakti Singh On

Use the single quote for string and nothing for variable, in your case.

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

I suggest you read about strings in the PHP docs. What you want here is called concatenation.

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

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

echo $dateTime;

Also notice the last line you want to echo $dateTime after you have set its contents.

0
Nick On

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

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

Try the above.

0
VolkerK On

You can find many examples of how to access array elements under 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;