Sydcul
Sydcul

Reputation: 99

PHP syntax thing (can't use (quoted) things starting with $ in string)

I wanted to write some variables to a file to include them in another script. But i get these errors while running the script:

Notice: Undefined variable: host in I:\xampp\htdocs\contact\install\writeconfig.php on line 2

Notice: Undefined variable: database in I:\xampp\htdocs\contact\install\writeconfig.php on line 2

Notice: Undefined variable: user in I:\xampp\htdocs\contact\install\writeconfig.php on line 2

Notice: Undefined variable: password in I:\xampp\htdocs\contact\install\writeconfig.php on line 2

HTML form:

<html>
<head>
<title>Contact installatie</title>
</head>
<body>
<h1>Contact installatie</h1>
<h2>Database gegevens:</h2>
<form name="databasesettings" action="writeconfig.php" method="post">
Host: <input type="text" name="host"> <br>
Database: <input type="text" name="database"> <br>
User: <input type="text" name="user"> <br>
Password: <input type="password" name="password"> <br>
<input type="submit" value="Generate config">
</form>
</body>
</html>

And PHP code:

<?php
$config = "$host = " . $_POST["host"] . "\n$database = " . $_POST["database"] . "\n$user = " . $_POST["user"] . "\n$password = " . $_POST["password"];

$configfile=fopen("config.txt","w+");

fwrite($configfile, $config);

fclose($configfile);
?>

Upvotes: 0

Views: 97

Answers (6)

Daniel Bingham
Daniel Bingham

Reputation: 12914

When using double quotes ( " ) to wrap a string, PHP will attempt to replace any variable names ($variable) in the string with their values. If you don't want PHP to do that, use single quotes ( ' ) to wrap the string.

For more information, read about string in the PHP manual:

http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double

http://php.net/manual/en/language.types.string.php#language.types.string.parsing

A side note, PHP won't do any interpreting of strings that use single quotes. So \n will not work in a single quoted string, it will need to be in a double quoted string.

Upvotes: 1

Rocky
Rocky

Reputation: 304

When you use the '$' inside double quoted string, php assumes it as a variable and replaces it with it's value. So your options are escaping them using a '\' before it or use a single quoted string.

I recommend using a '\', as you can't always go for the second option.

I'm moving the reply as answer here. May be it'll help others.

Upvotes: 0

Rudi Visser
Rudi Visser

Reputation: 21979

You have two options to get around this problem.

Double quoted strings in PHP perform variable name replacement (and more advanced replacements when wrapped with curly braces). You can instead use single quoted strings to be able to use $ within it, like so:

$config = '$host = ' . $_POST["host"] . "\n" . '$database = ' . $_POST["database"] . "\n" . '$user = ' . $_POST["user"] . "\n" . '$password = ' . $_POST["password"];

Note that you will have to put the \ns into double quoted strings, otherwise it won't be replaced properly.

Another alternative is to escape (using \) your $s, like this:

$config = "\$host = " . $_POST["host"] . "\n\$database = " . $_POST["database"] . "\n\$user = " . $_POST["user"] . "\n\$password = " . $_POST["password"];

As a bonus, if you wanted to use the braces as I mentioned above, you could write your string like so:

$config = "\$host = {$_POST['host']}\n\$database = {$_POST['database']}\n\$user = {$_POST['user']}\n\$password = {$_POST['password']}";

That doesn't mean I would recommend you to do so, though :)

The best way to do this is probably using sprintf, which makes it slightly more readable like so:

$config = sprintf("\$host = %s\r\n\$database = %s\r\n\$user = %s\r\n\$password = %s",
              $_POST['host'], $_POST['database'], $_POST['user'], $_POST['password']);

Upvotes: 1

John Conde
John Conde

Reputation: 219804

Options:

  1. Escape the $ with a backslash \
  2. Use single quotes instead

Examples:

$config = "\$host = " . $_POST["host"] . "\n\$database = " . $_POST["database"] . "\n\$user = " . $_POST["user"] . "\n\$password = " . $_POST["password"];

$config = '$host = ' . $_POST["host"] . "\n" . '$database = " . $_POST["database"] . "\n" . '$user = " . $_POST["user"] . "\n" . '$password = " . $_POST["password"];

When using single quotes special characters like \n will also need special consideration. I just put them in double quotes in my example but you can escape them as well.

Upvotes: 1

Dmitrii Tarasov
Dmitrii Tarasov

Reputation: 414

"$var" will try to find variable $var; Try to read this http://php.net/manual/en/language.types.string.php

Upvotes: 0

mavili
mavili

Reputation: 3424

Use single quotes for literal strings. Or escape them "\"

Upvotes: 1

Related Questions