2014-10-17 47 views
0

什麼原因會導致file_put_contents添加0而不是傳遞給它的字符串?file_put_contents添加「0」而不是字符串

它爲每次嘗試提交文件添加一個0。在第三次嘗試中就是如此。

$masterpostEntry = $title . "~" . $filetitle; 
$file = "../posts/master-allposts.txt"; 
$current = file_get_contents($file); 
$current .= $masterpostEntry + "\n"; 
file_put_contents($file, $current); 

$ masterpostEntry的回聲顯示正確結果:

CSS-Only Solution For UI Tracking~css-only-solution-for-ui-tracking 

但是主allposts.txt的內容是:

000 

回答

6

基本PHP:.會連接,+是算術運算:

$current .= $masterpostEntry + "\n"; 
          ^--- 

你在「添加」兩個字符串,所以PHP將它們轉換爲整數。除非這些字符串的一個字符串的開始有一些數字,你會做等價的:

$current .= 0 + 0; 
+0

謝謝,我正在愚蠢地切換PHP和JS,並且感到困惑。 – Francesca 2014-10-17 14:14:35

2

在PHP中,+是加法,到Concat的,使用點。

+0

謝謝,我一直在PHP和JS之間切換,並沒有注意到這一點。 – Francesca 2014-10-17 14:13:53

相關問題