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

发布于 2024-12-14 11:07:24 字数 142 浏览 2 评论 0原文

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

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

$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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

雪若未夕 2024-12-21 11:07:24

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

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

echo "Hello $variableName!";

在你的情况下:

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

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

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

$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;
隔纱相望 2024-12-21 11:07:24

在字符串内使用变量时始终使用双引号,并反斜杠除开头和结尾之外的任何其他双引号。您还可以使用如下所示的括号,以便更轻松地在字符串中找到变量并使它们看起来更干净。

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

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

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

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

一抹苦笑 2024-12-21 11:07:24

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

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

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

$i = 1
echo "<p class=\"paragraph$i\"></p>";
++i;
掐死时间 2024-12-21 11:07:24
echo '<p class="paragraph'.$i.'"></p>'

应该可以解决问题。

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

should do the trick.

可是我不能没有你 2024-12-21 11:07:24
echo '<p class="paragrah"' . $i . '">'
echo '<p class="paragrah"' . $i . '">'
海之角 2024-12-21 11:07:24
echo '<p class="paragraph'.$i.'"></p>';
echo '<p class="paragraph'.$i.'"></p>';
最初的梦 2024-12-21 11:07:24

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

方法一:

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

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


方法二:

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

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

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

方法三:(直接在被调用的变量内部添加变量)

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

这里我们使用$x添加$y$z$x >“。”; {}
在渲染未定义之前优先处理其中的工作
多变的。

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

//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.

如果 url 查询是:example,这将输出 $xresultapp 的“用户请求的工具:App” .com?tool=app。您可以使用 else 语句进行修改,以定义当请求“app”以外的某个值时会发生什么。请记住,一切都区分大小写,因此如果他们请求大写的“App”,则不会输出 $xresultapp

Here's the 3 best ways of doing this.

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:

$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 $y and $z to $x using the "."; The {}
prioritize's the work inside it before rendering the undefined
variable.

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.

哀由 2024-12-21 11:07:24

使用双引号:

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

Use double quotes:

$i = 1;
echo "
<p class=\"paragraph$i\">
</p>
";
++i;
初相遇 2024-12-21 11:07:24
$i = 1;

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

$i++;
$i = 1;

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

$i++;
哆啦不做梦 2024-12-21 11:07:24

你可以试试这个

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

You can try this

$i = 1
echo '<p class="paragraph'.$i.'"></p>';
++i; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文