2013-01-20 29 views
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); 
?> 
+0

逃避它们。在字符串中的'$'之前使用'\'。 – Rocky

+0

嘿,看,答案!但是,它被张贴为回复?这么奇怪? _Hope你得到的point._ – Sydcul

+0

没关系有关'\ N'的事情,这是因为我在Windows(我的开发PC)上运行。如果你有这样的问题太多,你应该使用'\ r \ N' – Sydcul

回答

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将不会在单引号字符串中工作,它将需要在双引号字符串中。

1

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

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

当U唱单引号特殊字符如\n也需要特别考虑。在我的例子中,我只是把它们放在双引号中,但你也可以将它们转义出来。

+0

但\ n的东西仍然是行不通的。 '$ config ='$ host ='。 $ _POST [“主机”]。 “\ n”。 '$ database ='。 $ _POST [“数据库”]。 “\ n”。 '$ user ='。 $ _POST [“user”]。 “\ n”。 '$ password ='。 $ _POST [“password”];' – Sydcul

+0

将'\ n'改为'PHP_EOL'。这是操作系统不可知的方式 –

1

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

PHP中的双引号字符串执行variable name replacement(用大括号包装时更高级的替换)。您可以改用单引号字符串能够在其内部使用$,像这样:

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

请注意,你将不得不把\n s转换双引号中的字符串,否则将无法进行正确更换。

另一种方法是(使用\)您$ 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']); 
0

当您使用“$”双引号字符串中,PHP假定它是一个变量,并替换它与它的价值。所以你的选择是在它之前使用'\'来转义它们,或者使用一个单引号字符串。

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

我搬到这里来的回复为答案。可能会帮助别人。