2010-03-02 37 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>';