PHP echo变量和html一起
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14790703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP echo variable and html together
提问by cusejuice
I know this is a basic php question, but I'm having trouble showing variables inside an echo of HTML. Below is what I'm trying to achieve.
我知道这是一个基本的 php 问题,但我无法在 HTML 回显中显示变量。以下是我正在努力实现的目标。
<?php
$variable = 'testing';
echo <span class="label-[variable]">[variable]</span>
?>
should output:
应该输出:
<span class="label-testing">testing</span>
回答by Julio
<?php
$variable = 'testing';
echo <span class="label-[variable]">[variable]</span>
?>
Should be
应该
<?php
$variable = 'testing';
echo '<span class="label-' . $variable .'">' . $variable . '</span>';
?>
回答by Kermit
If your HTML contains double quotes, then you can wrap it in single quotes:
如果您的 HTML 包含双引号,那么您可以将其用单引号括起来:
echo '<span class="label-' . $variable . '">' . $variable . '</span>';
Additionally, you can do it inline by escaping the double quotes:
此外,您可以通过转义双引号来内联:
echo "<span class=\"label-$variable\">$variable</span>";
Using double quotes also allows for special characters such as \nand \t.
使用双引号还允许使用特殊字符,例如\n和\t。
The documentationis your friend.
该文档是你的朋友。
回答by Miguel García
One of the great features of php is the ability to be called and quit using php at any time during html. Here is what I'd do:
php 的一大特点是能够在 html 期间随时调用和退出使用 php。这是我要做的:
<?php
// FIRST WRITE THE PHP PART
$variable = 'testing';
// THEN START USE HTML
// (call it when it's needed later)
?>
<span class="label-testing"><?php echo $variable; ?></span>
回答by Tynach
I know this is an old question, but what's wrong with something like this?
我知道这是一个老问题,但这样的事情有什么问题?
<?php
$variable = 'testing';
?>
<span class="label-<? echo variable ?>"><? echo variable ?></span>
回答by Desert_king
<?php
$variable = 'testing';
echo '<span class="label-'.$variable.'">'.$variable.'</span>';
?>
回答by metatron
This works too and is much sweeter:
这也有效,而且更甜:
<?php
$variable = 'testing';
echo "<span class='label-$variable'>$variable</span>";
?>
Order of double and single quote matters, double quotes on the outside. Be careful if you use this syntax, if the variable name is followed by normal characters things might go horribly wrong. Curly braces around variable names are required then.
双引号和单引号的顺序很重要,双引号在外面。使用这种语法时要小心,如果变量名后跟普通字符,事情可能会出错。然后需要围绕变量名称的花括号。
Example:
例子:
<?php
$max_image_with = 1000;
echo "<div style='width:${max_image_width}px;'>";
?>
Documentation here: http://php.net/manual/en/language.types.string.php#language.types.string.parsing
文档在这里:http: //php.net/manual/en/language.types.string.php#language.types.string.parsing
回答by Vijay Deva
PHP Code... It's Working
PHP 代码......它正在工作
<?php
$st_a = '<i class="fa fa-check-circle" ></i> Active';
?>
To Display in HTML
以 HTML 显示
<h1> Sample Heading <?php echo $st_a; ?></h1>
回答by Kip
If you are saying that $variableis the name of the variable you wish to print, you want this:
如果您说这$variable是要打印的变量的名称,则需要这样:
echo "<span class=\"label-$variable\">${$variable}</span>";
The ${$variable}syntax tells PHP to print the variable named $variable. PHP calls these variable variables.
该${$variable}语法告诉PHP打印变量名为$变量。PHP 调用这些变量变量。
So for example:
例如:
$itemName = 'pencil';
$variable = 'itemName';
echo "<span class=\"label-$variable\">${$variable}</span>";
//Prints: <span class="label-itemName">pencil</span>"
回答by Valera Leontyev
It's TOO basic question to be asked here...
在这里问这个问题太基本了...
<?php
$variable = 'testing';
echo '<span class="label-[' . $variable . ']">[' . $variable . ']</span>;
?>
回答by Brandon Anzaldi
Something like this should work.
像这样的事情应该有效。
<?PHP
$variable = 'testing';
echo "<span class=\"label-$variable\">$variable</span>";
?>
The type of quotes on the echo are very important though. If you'd rather use single quotes on the echo, it'd be more like this.
但是,echo 上的引号类型非常重要。如果您更愿意在回声上使用单引号,则更像是这样。
<?PHP
$variable = 'testing';
echo '<span class="label-' . $variable . '">' . $variable . '</span>';
?>
This should allow you to echo back the variable.
这应该允许您回显变量。

