php - 在回显字符串中插入一个变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8054989/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:51:54  来源:igfitidea点击:

php - insert a variable in an echo string

phpvariablesecho

提问by Mechaflash

$i = 1
echo '
<p class="paragraph$i">
</p>
'
++i

Trying to insert a variable into an echoed string. The above code doesn't work. How do I iterate a php variable into an echo string?

尝试将变量插入回显字符串中。上面的代码不起作用。如何将 php 变量迭代为 echo 字符串?

回答by Derek

Single quotes will not parse PHP variables inside of them. Either use double quotes or use a dot to extend the echo.

单引号不会解析其中的 PHP 变量。使用双引号或使用点来扩展回声。

$variableName = 'Ralph';
echo 'Hello '.$variableName.'!';

OR

或者

echo "Hello $variableName!";

And in your case:

在你的情况下:

$i = 1;
echo '<p class="paragraph'.$i.'"></p>';
++i;

OR

或者

$i = 1;
echo "<p class='paragraph$i'></p>";
++i;

回答by Jake

Always use double quotes when using a variable inside a string and backslash any other double quotes except the starting and ending ones. You could also use the brackets like below so it's easier to find your variables inside the strings and make them look cleaner.

在字符串中使用变量时始终使用双引号,并反斜杠除起始和结束双引号外的任何其他双引号。您还可以使用如下方括号,这样可以更轻松地在字符串中找到您的变量并使它们看起来更简洁。

$var = 'my variable';
echo "I love ${var}";

or

或者

$var = 'my variable';
echo "I love {$var}";

Above would return the following: I love my variable

以上将返回以下内容:我爱我的变量

回答by codaddict

Variable interpolation does not happen in single quotes. You need to use double quotes as:

单引号中不会发生变量插值。您需要使用双引号作为:

$i = 1
echo "<p class=\"paragraph$i\"></p>";
++i;

回答by Ash Burlaczenko

echo '<p class="paragraph'.$i.'"></p>'

should do the trick.

应该做的伎俩。

回答by rogerlsmith

echo '<p class="paragrah"' . $i . '">'

回答by Ali Nouman

echo '<p class="paragraph'.$i.'"></p>';

回答by John123

Here's the 3 best ways of doing this.

这是执行此操作的 3 种最佳方法。

Method One:

方法一:

$x = '+3';
echo "1+2$x";

Double Quotes (") allows you to just pass the variable directly inside it.

双引号 (") 允许您直接在其中传递变量。



Method Two:

方法二:

$x = '+3';
echo '1+2'.$x;

When you don't want to use double quotes for whatever reason go with this. The (.) simply means "Add" basically. So if you were to want to add something like, 1+2+3+4+5 and have your variable in the middle all you need to do is:

当您出于任何原因不想使用双引号时,请使用此方法。(.) 基本上意味着“添加”。因此,如果您想添加类似 1+2+3+4+5 的内容并将变量放在中间,您需要做的就是:

$x = '+3';
echo '1+2'.$x.'+4+5';


Method 3: (Adding a variable directly inside the called variable)

方法三:(直接在被调用变量里面加一个变量)

$x = '+3';
$y = '+4';
$z = '+5';
echo "1+2${"x".$y.$z}";
Output: 1+2+3+4+5

Here we are adding $yand $zto $xusing the "."; The {}prioritize's the work inside it before rendering the undefined variable.

在这里,我们正在增加$y,并$z$x使用"."; {}在呈现未定义的变量之前,优先处理其中的工作。

This personally is a very useful function for calling functions like:

这对于调用如下函数是一个非常有用的函数:

//Add the Get request to a variable.
$x = $_GET['tool'];

//Edit: If you want this if to contain multiple $xresult's change the if's
//Conditon in the "()" to isset($get). Simple. Now just add $xresultprogram
//or whatever.
if($x == 'app') {
    $xresultapp = 'User requested tool: App';
}

//Somewhere down far in HTML maybe...

echo ${"xresult".$x}; // so this outputs: $xresultapp's value

//Note: doing ${"xresult".$_GET['tool']} directly wont work.
//I believe this is because since some direct non-echo html was loaded
//before we got to this php section it cant load cause it has already
//Started loading client side HTML and JS.

This would output $xresultapp's 'User requested tool: App' if the url query is: example.com?tool=app. You can modify with an else statement to define what happens when some value other than 'app' is requested. Remember, everything is case-sensitive so if they request 'App' in capitals it won't output $xresultapp.

$xresultapp如果 url 查询是:,这将输出“用户请求的工具:应用程序” example.com?tool=app。您可以使用 else 语句进行修改,以定义在请求除“app”以外的其他值时会发生什么。请记住,所有内容都区分大小写,因此如果他们以大写形式请求“App”,则不会输出$xresultapp.

回答by Naftali aka Neal

Use double quotes:

使用双引号:

$i = 1;
echo "
<p class=\"paragraph$i\">
</p>
";
++i;

回答by Naftali aka Neal

You can try this

你可以试试这个

$i = 1
echo '<p class="paragraph'.$i.'"></p>';
++i;