Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print $ with php

Tags:

php

echo

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.

There is the standard answers here but none are working: 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('$', '&#36;', $s);
    echo  $s . "<br>";
}

I figured I should just post it anyway.

Thanks,

Mat

like image 498
Mat Kay Avatar asked Feb 27 '12 23:02

Mat Kay


People also ask

How do I print a PHP file?

The echo command is used in PHP to print any value to the HTML document. Use <script> tag inside echo command to print to the console.

Can we use print in PHP?

The PHP print StatementThe print statement can be used with or without parentheses: print or print() .

What is command for print in PHP?

Definition and Usage. The print() function outputs one or more strings. Note: The print() function is not actually a function, so you are not required to use parentheses with it. Tip: The print() function is slightly slower than echo().


2 Answers

Or you could echo string literal 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 has many answers that would work perfectly. How are you applying them in a way that doesn't work?

like image 91
Kai Qing Avatar answered Oct 27 '22 13:10

Kai Qing


Just use a single quoted string.

$foo = 'Hello';
echo '$foo'; // $foo
echo "$foo"; // Hello
like image 43
AlienWebguy Avatar answered Oct 27 '22 13:10

AlienWebguy