简体   繁体   中英

PHP: How to get $ to print using echo

I'm very new to PHP and having a hard time finding this answer as $ denotes a variable in PHP. I'm trying to echo a total for items, such as:

echo "Your total is ${$total}";

The problems is the $ in front causes it to do nothing. I tried doing '$' but it prints the ' ' around the $. How do I get the $ to print in front of the variable?

Just use single quotes.

echo 'Your total is $' . $total;

Check this article for more information regarding the differences between single and double quotes in PHP.

http://v1.jeroenmulder.com/weblog/2005/04/php_single_and_double_quotes.php

如果使用双引号(“),则必须转义$字符:

echo "Your total is \${$total}";

最后但并非最不重要的:

printf('Your total is $%.2f', $total);

You need to escape the dollar sign: echo "\\$"; .

echo "Your total is $" . $total;

You don't have to use variable parsing in every possible situation. Very often it is much easier (both to write and to understand) when simply writing as a string concatenation..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM