2013-07-10 58 views
0

我對這兩位代碼的格式有什麼不妥?PHP - 在var上添加混合字符串和變量?

如何格式化字符串和變量的混合以給我正確的輸出?

$inbody = 60; 
$body = "Welcome. For about " . $inbody . "-" . $inbody+15 . " minutes, you'll receive SMS messages." ; 

我可以做那樣的內聯嗎?或者是我唯一的解決方案來聲明另一個變量並將其放入?像:

$addedinbody = $inbody + 15 ; 

這裏是另一個有麻煩(具體設置$ body變量)例如IM:

for ($i=0; $i<=$duration; $i++){ 
$body = $i+1 . "/" . $duration+1 . " " . $task[$newtask]; 
} 

回答

1

你需要把括號中的除了告訴大家,有加法。

喜歡的話:

$body = "Welcome. For about " . $inbody . "-" . ($inbody+15) . " minutes, you'll receive SMS messages." ; 

你的問題將得到解決。

0

像Broken Heart說的那樣你需要()圍繞$ inbody + 15。

另一個提示你不需要從雙引號中跳出來將一個變量插入到字符串中,但是在單引號字符串中。

$body = "Welcome. For about $inbody -" . $inbody+15 . " minutes, you'll receive SMS messages." ;

一樣破碎的心靈深處說,適用於您的for循環。

for ($i=0; $i<=$duration; $i++){ $body = ($i+1) . "/" . ($duration+1) . " " . $task[$newtask]; }