2

我有一些基本的 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 '<'

所以我的问题是,我该如何让它发挥作用,或者采取另一种方法?

4

5 回答 5

5

试试这个

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

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

于 2010-03-02T15:48:39.213 回答
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';
?>

如果您使用 " " 而不是 ' 您将不会得到相同的输出,因为 " 将解释所有内容而不是按字面意思理解。

我希望这对您有所帮助,即使您的问题已经得到解答。

于 2010-03-02T16:11:32.117 回答
1

您可以在 '. 如果您知道我的意思,您只能像这样回应它。

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

用这个:

$leftMenu ='<div class="leftMenuProductButton">'.$raceramps56["short"].'</div>';
于 2012-10-11T15:25:31.327 回答
0

或者不需要转义双引号:

$leftMenu = '<div class="leftMenuProductButton">' . $raceramps56["short"] . '</div>';
于 2010-03-02T15:49:57.287 回答
0
$raceramps56["short"] = "My Test Product";

$leftMenu = '<div class="leftMenuProductButton">' . $raceramps56["short"] . '</div>';
echo $leftMenu;
于 2010-03-02T15:50:37.727 回答