-4

Is it possible to store a literal string saying: $_SESSION[user] in a var?

I tried

$user = "$_SESSION['user']"; 

But that resulted in an error:

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE)

Then I tried:

$user = "$_SESSION[user]"; 

If I echo $user i get a number 19, why?

Why do I need this? Because I'm using fwrite so I'm creating a new PHP file with an HTML that I already wrote, so my code its something like:

if (isset($_POST['nameofph'])) {

  $fname = fopen($_POST['nameofph'].'.php','w+');
  fwrite($fname, $fdata);
  fclose($fname);
}

I do not know if I explain it well, the point is I need to store that "string" in the var $user.

Oldskool
  • 34,211
  • 7
  • 53
  • 66
Gil
  • 43
  • 7

3 Answers3

2

You can store $_SESSION["user"] as string into variable by using single quote at start & end and double quote before INDEX.

Try

$user = '$_SESSION["user"]';

DEMO

Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
1

You have to do this to store session value into a variable

$user = (string)$_SESSION['user']; 
Aniruddha Chakraborty
  • 1,849
  • 1
  • 20
  • 32
0

What about..

$user = "\$_SESSION[user]"; 
user3636110
  • 166
  • 11