How to print $ with php

Basically, I have an html block that I want to repeat on the page, and html has a $ sign in it, and php considers this to be a variable, so $ 1 is considered as a variable, not a value and not displayed.

There are standard answers here, but no one works: PHP: how to get $ to print using echo

My next idea is to split the string into $ and echo into each part.

Here is the code that I 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 was appreciated.

OK, I decided to use the character code value for $

 foreach ($rows as $rowmk) { $s = $rowmk->longdescription; $s = str_replace('$', '&#36;', $s); echo $s . "<br>"; } 

I decided that I should just post it anyway.

Thanks,

Mat

+4
source share
5 answers

You are doing it in the wrong place. Variable interpolation is performed when the double literal of the string (which in your case is stored in $rowmk->longdescription is daclared. After that, you cannot do anything to return your $ .

Solution, do proper escaping when you declare a string.

+1
source

Or you can echo literals using single quotes ...

 <?php echo 'Give me $1'; ?> 

will print:

Give me $ 1

PHP string docs:

http://php.net/manual/en/language.types.string.php

Side Note - The link you provide contains many answers that will work just fine. How do you apply them in a way that doesn't work?

+11
source

Just use a single quote.

 $foo = 'Hello'; echo '$foo'; // $foo echo "$foo"; // Hello 
+4
source

I assume that you are reading your lines from the database. Dollar signs inside these lines will not be interpolated by php. Here is a little test script to try:

  // 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>"; } 
0
source

I did it using this

 echo "$" . "VariableName"; 
-one
source

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


All Articles