2013-01-20 30 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假定它是一個變量,並替換它與它的價值。所以你的選擇是在它之前使用'\'來轉義它們,或者使用一個單引號字符串。

我建議使用「\」,因爲你不能總是去的第二個選項。

我搬到這裏來的回覆爲答案。可能會幫助別人。