2

I searched but didn't find a question about this. Is there a difference between $_session['var'] and $_session["var"] in PHP?

I know the difference of single and double quotes at the echo command but can't figure out the above. At my testing server it works fine both ways, but is there something deeper?

Dhanuka
  • 2,826
  • 5
  • 27
  • 38
teravice
  • 33
  • 2
  • http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php – Saty Jun 25 '15 at 07:53
  • store some info in session and do , there is your answer – SpongePablo Jun 25 '15 at 07:55
  • See [info on the differences between single-quoted and double-quoted strings in the PHP manual](https://secure.php.net/language.types.string). Strings inside array references are still strings, and they follow exactly the same rules. – Simba Jun 25 '15 at 08:27

4 Answers4

3

actually difference between " " and ' '. Whenever you write something inside ' ' it will consider as string. it cant recognize any variable or like this. but " " is working for both string and variable.

example:

$foo = 10;
echo '$foo';
output: $foo;

again for " ",

echo "$foo";
output: 10;

when you write $_session["$var"]. it will work.

but if you write $_session['$var'] not going to work.

here $var is a variable. value can be $var = 'var' or like this.

Kalyan Halder
  • 1,485
  • 24
  • 28
  • what about for $_SESSION["success"] vs $_SESSION['success'] ? where the parameter isnt a variable but just a value – DrFaraday Mar 18 '21 at 10:17
1

Difference is as follows:-

Eg: $key = 'demo';
In single quote, 
$_SESSION['$key'] = $key; wont work.
But in double quotes,
$_SESSION["$key"] = $key; works.

Means you can pass php variable directly in double quotes but not in single quote.

Domain
  • 11,562
  • 3
  • 23
  • 44
0

When you write something in "", it gets parsed and written in '' never get parsed, echo's data as it is.

for example

$cur = 10;

echo "i need USD $cur.";

it will parse "" and will output as : i need USD 10.

but if you put it in ''

echo 'i need USD $cur.';

it will not parse it and output as : i need USD $cur.

Same thing applies for SESSION too. single quote works faster than double quote.

Mangesh Sathe
  • 1,987
  • 4
  • 21
  • 40
0

"" or '' both aren't different in $_SESSION[], unless you want to put variables on it, just like $_SESSION["session_$sesscode"]

Eko Dedy
  • 399
  • 1
  • 6
  • 18