php 如何在单引号内使用单引号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3383838/
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
How do I use single quotes inside single quotes?
提问by Joey Morani
Can anyone explain how to make this code work?
谁能解释一下如何使这段代码工作?
echo '<div id="panel1-under">Welcome <?php echo "$_SESSION['username']"; ?></div>';
I've tried removing the single quotes (echo '<div id="panel1-under">Welcome <?php echo "$_SESSION[username]"; ?></div>';), but it doesn't work.
我试过删除单引号 ( echo '<div id="panel1-under">Welcome <?php echo "$_SESSION[username]"; ?></div>';),但它不起作用。
回答by Kami
echo "<div id=\"panel1-under\">Welcome ".$_SESSION['username']."</div>";
or
或者
echo '<div id="panel1-under">Welcome '.$_SESSION['username'].'</div>';
Quick Explain :
快速解释:
- You don't have to reopen the tags inside a echo String (" ... ")
- What I have done here is to pass the string "Welcome "concatenated to $_SESSION['username']and ""(what the . operator does)
PHP is even smart enough to detect variables inside a PHP string and evaluate them :
$variablename = "Andrew";
echo "Hello $variablename, welcome ";
- 您不必重新打开 echo String (" ... ") 内的标签
- 我在这里所做的是传递连接到$_SESSION['username']和""的字符串“Welcome ”(. 运算符的作用)
PHP 甚至足够聪明,可以检测 PHP 字符串中的变量并评估它们:
$variablename = "安德鲁";
echo "你好 $variablename, 欢迎 ";
=> Hello Andrew, welcome
=> 你好安德鲁,欢迎
More infos : PHP.net - echo
更多信息:PHP.net - 回声
回答by Hammerite
Inside single quotes, variable names aren't parsed like they are inside double-quotes. If you want to use single-quoted strings here, you'll need to use the string concatenationoperator, .:
在单引号内,变量名不会像在双引号内那样被解析。如果要在此处使用单引号字符串,则需要使用字符串连接运算符.:
echo '<div id="panel1-under">Welcome <?php echo "'.$_SESSION['username'].'"; ?></div>';
By the way: the answer to the question in the title is that in order to use a literal single-quote inside a single-quoted string, you escapethe single-quote using a backslash:
顺便说一句:标题中问题的答案是,为了在单引号字符串中使用文字单引号,您可以使用反斜杠转义单引号:
echo 'Here is a single-quote: \'';
回答by Ben Rowe
You need to concatenate your strings & variables.
您需要连接字符串和变量。
echo '<div id="panel1-under">Welcome ' . $_SESSION['username'] . '</div>';
回答by jasonbar
Variable expansion doesn't work inside of single quotes. You can do either:
变量扩展在单引号内不起作用。您可以执行以下任一操作:
echo "<div id=\"panel1-under\">Welcome {$_SESSION['username']}</div>";
or
或者
echo '<div id="panel1-under">Welcome ' . $_SESSION['username'] . '</div>';
回答by kiamlaluno
Generally speaking, to use the single quote inside a string that is using the single quote as delimiter, just escape the single quote inside the string
一般来说,要在使用单引号作为分隔符的字符串内使用单引号,只需将字符串内的单引号转义即可
echo 'That\'s all, folks';
It's not clear what the purpose of your code is, though.
不过,尚不清楚您的代码的目的是什么。
echo '<div id="panel1-under">Welcome <?php echo "$_SESSION['username']"; ?></div>';
As you are already using PHP code, <?php echois not necessary. If you are only trying to output the content of a session variable, then you can use
由于您已经在使用 PHP 代码,<?php echo因此没有必要。如果您只是想输出会话变量的内容,那么您可以使用
echo '<div id="panel1-under">Welcome ' . $_SESSION['username'] . '</div>';
回答by abilashov
Use the following code
使用以下代码
$data = $session['user'];
echo "a big string and $data thats simple"
回答by csandreas1
You can use double or single quotes inside double quotes.
您可以在双引号内使用双引号或单引号。
#Result: Hello "World" 's
$a = '"World"';
echo "Hello $a 's";
#Result: Hello "World" 's
echo "Hello \"World\" 's";
You can also use double or single quotes inside single quotes. (Not recommended when building SQL queries)
您还可以在单引号内使用双引号或单引号。(在构建 SQL 查询时不推荐使用)
#Result: Hello World's "s
echo 'Hello World\'s "s';

