2010-03-02 130 views
2

我有一些基本的PHP代碼:如何在PHP中回顯回聲?

$raceramps56["short"] = "My Test Product"; 

$leftMenu = 

'<div class="leftMenuProductButton"><?PHP echo $raceramps56["short"] ?></div>'; 

不會呼應PHP代碼,只有元素。我試過的東西

<div class="leftMenuProductButton">' . <?PHP echo $raceramps56["short"] ?>. '</div>'; 

剛剛返回Parse error: parse error, unexpected '<'

所以我的問題是,我怎麼要麼得到這個工作,或採取另一種方法?

回答

5

試試這個

$raceramps56["short"] = "My Test Product";

$leftMenu ='<div class="leftMenuProductButton">'.$raceramps56["short"].'</div>';

+3

啊確定,所以答案是,沒有回聲的回聲:) – Jared 2010-03-02 15:57:19

0

需要或不需要轉義雙引號:

$leftMenu = '<div class="leftMenuProductButton">' . $raceramps56["short"] . '</div>'; 
0
$raceramps56["short"] = "My Test Product"; 

$leftMenu = '<div class="leftMenuProductButton">' . $raceramps56["short"] . '</div>'; 
echo $leftMenu; 
1

我想我會提供一些額外的信息,只是讓你明白。

在您的代碼:

$raceramps56["short"] = "My Test Product"; 

$leftMenu = 

'<div class="leftMenuProductButton"><?PHP echo $raceramps56["short"] ?></div>'; 

要包括從字面上。

請仔細閱讀本文。 http://php.net/manual/en/language.types.string.php

當我第一次學習時,我不理解字面意思和雙引號之間的差異,尤其是在我試圖迴應事物時引發問題。

看看這個:

<?php 
echo 'this is a simple string'; 

echo 'You can also have embedded newlines in 
strings this way as it is 
okay to do'; 

// Outputs: Arnold once said: "I'll be back" 
echo 'Arnold once said: "I\'ll be back"'; 

// Outputs: You deleted C:\*.*? 
echo 'You deleted C:\\*.*?'; 

// Outputs: You deleted C:\*.*? 
echo 'You deleted C:\*.*?'; 

// Outputs: This will not expand: \n a newline 
echo 'This will not expand: \n a newline'; 

// Outputs: Variables do not $expand $either 
echo 'Variables do not $expand $either'; 
?> 

如果你使用「」,而不是「你不會得到相同的輸出,因爲」會解釋一切而不是把它從字面上

我希望這一直是額外的幫助,即使你有你的問題回答了。

+0

感謝你的額外信息! – Jared 2010-03-02 16:24:36

1

您可以在」如果你知道我的意思你也只能隨聲附和它是這樣的。

運行php
$leftMenu ='<div class="leftMenuProductButton">.$raceramps56["short"].</div>'; 
echo $leftMenu; 

使用此:

$leftMenu ='<div class="leftMenuProductButton">'.$raceramps56["short"].'</div>';