$ symbol in print function?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

$ symbol in print function?

Post by C_Calav »

hi guys, i have this print function that i need to have included in my site, all works fine, but when i go to add the $ (dollar sysmbol) in in front of the shipping for example,

Code: Select all

<td colspan="2" valign="top">$&#123;$shipping&#125;</td>
the shipping amount and the $ dollar sign do not show up.

if however i do not have the $ dollar sign in front of the shipping it does show up.

for example how i have it at the moment.

Code: Select all

print <<<HTMLEOF

   <tr> 
        <td height="30" colspan="3" valign="middle"><!--DWLayoutEmptyCell-->&nbsp;</td>
        <td colspan="2" valign="top"><strong>Freight:</strong>&nbsp;</td>
   		 <td colspan="2" valign="top">&#123;$shipping&#125;</td>
      </tr>
      <tr> 
        <td height="30" colspan="3" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td>
        <td colspan="2" valign="top" align="left"><strong>Total: </strong></td>
		<td colspan="2" valign="top">&#123;$totalCost&#125;</td>
      </tr>	

HTMLEOF;
is this $ dollar sign not allowed to be in the print function can someone please help or explain? thanx!
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

looks like it is being evaluated as a variable variable

http://php.net/manual/en/language.varia ... riable.php

Code: Select all

$first = 'second';
$second = 'third';

echo $first; // outputs second
echo ${$first}; // outputs third

// this is because php first evaluates whats in the curly braces, 
// which is the string 'second'

// since there is a variable named $second, it now echos the contents of second


// to avoid this, you must escape the $ symbol like \ $

echo "\$$money";

the board isnt letting me post a \ directly before a $ symbol, so i couldnt show you


or you could just do like this

echo '$' . $money . 'rest of string';
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

thanx rehfeld, i had a read and looks abit over my head.

i tried the echo thing but got muddled up with the

print <<<HTMLEOF echo '$' HTMLEOF;

is that what you mean?
User avatar
potsed
Forum Commoner
Posts: 50
Joined: Sat Oct 09, 2004 12:00 pm
Location: - South Africa

Post by potsed »

try escaping the dollar sign as rehfeld is suggesting like this...

Code: Select all

<td colspan="2" valign="top">\$&#123;$shipping&#125;</td>
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

that was it! thanx very much,

i have one more problem

now how can i print this:

Code: Select all

<?php
echo number_format($totalCost, 2, ".", ","); 
?>
as this:
\${$shipping} (but with the formatting)

do you get what i mean?

because im in a print function it will not let me, or i do not know how to include the formatting.

does anyone know about this or anything to do with this?

thanx
User avatar
potsed
Forum Commoner
Posts: 50
Joined: Sat Oct 09, 2004 12:00 pm
Location: - South Africa

Post by potsed »

Format it in a var

Code: Select all

$shipping = number_format($totalCost, 2, ".", ",");
Then put the formatted var into the print
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

thanx man, i did something i had the right idea but i didnt dfo it right!

cheers for every ones help
Post Reply