PHP: how to get $ to print using echo

I am very new to PHP and have difficulty finding this answer since $ denotes a variable in PHP. I am trying to repeat the total number of elements, for example:

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

The problem is that $ front forces it to do nothing. I tried making "$" but it prints '' around $. How to get $ to print before a variable?

+3
source share
5 answers

If you use double quotes ("), you need to escape the $ character:

 echo "Your total is \${$total}"; 
+7
source

Just use single quotes.

echo 'Your total is $' . $total;

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

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

+9
source

And last but not least:

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

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

+5
source
 echo "Your total is $" . $total; 

You do not need to use variable parsing in all possible situations. Very often it is much easier (to write and understand) when it is simply written as a string concatenation.

+2
source

Source: https://habr.com/ru/post/1398716/


All Articles