php 如何用php打印$
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9473876/
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 to print $ with php
提问by Mat Kay
Basically I have a block of html that I want to echo to the page and the html has the $ sign in it and the php thinks it is a variable so $1 is treated as the variable not the value and is not displayed.
基本上我有一个 html 块,我想回显到页面,html 中有 $ 符号,php 认为它是一个变量,所以 $1 被视为变量而不是值并且不显示。
There is the standard answers here but none are working: PHP: How to get $ to print using echo
这里有标准答案,但没有一个有效:PHP: How to get $ to print using echo
My next idea is to split the string at the $ and echo each part out.
我的下一个想法是在 $ 处拆分字符串并回显每个部分。
Here is the code I have tried echo and print.
这是我尝试回显和打印的代码。
foreach ($rows as $rowmk) {
$s = $rowmk->longdescription;
//$s = str_replace('$', '@', $s);
$s = str_replace('$', '$', $s);
//echo "$s" . "<br>";
print $s;
}
All help appreciated.
所有帮助表示赞赏。
OK I solved by using the character code value for $
好的,我通过使用 $ 的字符代码值解决了
foreach ($rows as $rowmk) {
$s = $rowmk->longdescription;
$s = str_replace('$', '$', $s);
echo $s . "<br>";
}
I figured I should just post it anyway.
我想无论如何我应该发布它。
Thanks,
谢谢,
Mat
垫
采纳答案by Mchl
You're doing it in the wrong place. Variable interpolating is done when double quoted string literal (which in your case is stored within $rowmk->longdescription
is daclared. Once it's done, you can't really do anything to get your $
s back.
你在错误的地方做这件事。当双引号字符串文字(在您的情况下存储在其中的内容$rowmk->longdescription
被声明。一旦完成,您就无法真正做任何事情来$
恢复您的s 。
Solution, do proper escaping, when you declare the string.
解决方案,在声明字符串时进行适当的转义。
回答by Kai Qing
Or you could echo string literal using single quotes...
或者您可以使用单引号回显字符串文字...
<?php
echo 'Give me ';
?>
will print:
将打印:
Give me $1
给我 1 美元
PHP string docs:
PHP 字符串文档:
http://php.net/manual/en/language.types.string.php
http://php.net/manual/en/language.types.string.php
Side note - the link you provide has many answers that would work perfectly. How are you applying them in a way that doesn't work?
旁注 - 您提供的链接有许多可以完美运行的答案。你如何以一种不起作用的方式应用它们?
回答by AlienWebguy
Just use a single quoted string.
只需使用单引号字符串。
$foo = 'Hello';
echo '$foo'; // $foo
echo "$foo"; // Hello
回答by bjelli
I assume you read your rows from a database. Dollar Signs inside these strings will not be interpolated by php. Here's a little test script to try it out:
我假设您从数据库中读取了行。php 不会插入这些字符串中的美元符号。这是一个小测试脚本来尝试一下:
// you'd first have to set the three variables according to your database
$dbh = new PDO($DSN, $DB_USER, $DB_PASS);
// create a table and insert a string containing a dollar sign
$dbh->exec('CREATE TABLE IF NOT EXISTS some_text ( longdescription VARCHAR( 255 ))');
$dbh->exec('INSERT INTO some_text ( longdescription ) VALUES ( "10 $" )');
// query all the data from the table
$query =$dbh->query("SELECT * FROM some_text");
$rows = $query->fetchAll(PDO::FETCH_CLASS);
// loop over all the rows (as in your example) and output the rows
// no problem at all
foreach ($rows as $rowmk) {
$s = $rowmk->longdescription;
echo $s . "<br>";
}
回答by jesus alberto acosta heredia
I did it using this
我是用这个做的
echo "$" . "VariableName";