Jump to content

[SOLVED] trouble writting a variable name to a file


mike12255

Recommended Posts

so im trying to "create" a config file based on a users entered data so i setup myself kind of a test

 

I want write this to the file:

 

$dbuser = dffdd;

 

instead im getting this:

 

dffdd = dffdd

 

can anyone help me change the first dffdd to the variable name $dbuser?

 

 

<?php

$fn = "config.php";

$fh = fopen($fn ,'w') or die("can't open file");
chmod($fn,0777);
$dbuser = "dffdd";

$info = "".$dbuser." = $dbuser";
fwrite($fh, $info);
fclose($fn);


?>

 

So that you understand what is going on..

 

echo $variable; // no quotes: $variable is parsed. Value is echoed
echo "$variable"; // double quotes: $variable is parsed. Value is echoed
echo '$variable'; // single quotes: $variable is not parsed.  Literal $variable output. 

 

Your situation involves assigning it to a variable.  echo was just an example to portray how php parses things. The reason

 

echo " '$dbuser' = $dbuser";

 

did not work is because you are still wrapping it in double quotes, overall.  so that would output

 

'value' = value

 

So the idea is to get the first $dbuser to be interpreted literally, while the 2nd one should be parsed.  So you can escape the dollar sign as rhodesa did, or any other combo such as

 

$info = '$dbuser' . " = '$dbuser'";
$info = '$dbuser' . " = '" . $dbuser . "'";
// etc...

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.