0

我想将一些变量写入文件以将它们包含在另一个脚本中。但是我在运行脚本时遇到了这些错误:

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 表单:

<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>

和PHP代码:

<?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);
?>
4

6 回答 6

1

当使用双引号 ( " ) 包裹字符串时,PHP 将尝试将字符串中的任何变量名 ($variable) 替换为其值。如果您不希望 PHP 这样做,请使用单引号 ( ' ) 包裹字符串。

有关更多信息,请阅读 PHP 手册中的字符串:

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

附带说明,PHP 不会对使用单引号的字符串进行任何解释。所以\n在单引号字符串中不起作用,它需要在双引号字符串中。

于 2013-01-20T17:58:48.430 回答
1

对文字字符串使用单引号。或者逃离他们"\"

于 2013-01-20T17:59:08.140 回答
1

选项:

  1. $用反斜杠转义\
  2. 改用单引号

例子:

$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"];

使用单引号时,特殊字符之类的\n也需要特别考虑。在我的示例中,我只是将它们放在双引号中,但您也可以将它们转义。

于 2013-01-20T17:59:34.970 回答
1

您有两种选择来解决这个问题。

PHP 中的双引号字符串执行变量名替换(以及用花括号括起来的更高级的替换)。您可以改为使用单引号字符串以便能够在$其中使用,如下所示:

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

请注意,您必须将\ns 放入双引号字符串中,否则将无法正确替换。

另一种选择是逃避(使用\)你$的 s,像这样:

$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']}";

不过,这并不意味着我会建议您这样做:)

做到这一点的最好方法可能是使用sprintf,这使它更具可读性,如下所示:

$config = sprintf("\$host = %s\r\n\$database = %s\r\n\$user = %s\r\n\$password = %s",
              $_POST['host'], $_POST['database'], $_POST['user'], $_POST['password']);
于 2013-01-20T18:01:28.090 回答
0

"$var" 将尝试查找变量 $var; 尝试阅读此http://php.net/manual/en/language.types.string.php

于 2013-01-20T17:59:31.950 回答
0

当您在双引号字符串中使用 '$' 时,php 将其假定为变量并将其替换为它的值。因此,您的选项是在它之前使用 '\' 转义它们或使用单引号字符串。

我建议使用“\”,因为您不能总是选择第二个选项。

我在这里将回复作为答案移动。也许它会帮助别人。

于 2013-01-20T18:10:22.077 回答