How to echo an echo in php?

2019-02-25 20:46发布

I have some basic PHP code:

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

$leftMenu = 

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

Won't echo the PHP code, only the element. I've tried things like

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

Which just returns Parse error: parse error, unexpected '<'

So my question is, how do I either get this to work, or take another approach?

标签: php echo
5条回答
Ridiculous、
2楼-- · 2019-02-25 21:26

try this

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

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

查看更多
贪生不怕死
3楼-- · 2019-02-25 21:28
$raceramps56["short"] = "My Test Product";

$leftMenu = '<div class="leftMenuProductButton">' . $raceramps56["short"] . '</div>';
echo $leftMenu;
查看更多
叼着烟拽天下
4楼-- · 2019-02-25 21:35

I thought I'd provide some extra information just so you understand.

In your code:

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

$leftMenu = 

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

You are including literally.

Take a read of this. http://php.net/manual/en/language.types.string.php

When I was first learning, I did not understand the different between literal ' and double quotes and it especially caused problems when I was trying to echo things.

Take a look at this:

<?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';
?>

If you were to use " " instead of ' you would not get the same output because " will interpret everything rather then take it literally.

I hope this has been of additional help, even though you have had your question answered already.

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-02-25 21:36

Or without the need of escaping double quotes:

$leftMenu = '<div class="leftMenuProductButton">' . $raceramps56["short"] . '</div>';
查看更多
对你真心纯属浪费
6楼-- · 2019-02-25 21:40

You can run php in a '. You can only echo it like this if you know what I mean.

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

Use this:

$leftMenu ='<div class="leftMenuProductButton">'.$raceramps56["short"].'</div>';
查看更多
登录 后发表回答