-1

Possible Duplicate:
Curly braces in string in PHP

I run into some "strange" PHP code, so instead of this:

<?php echo $variable; ?>

I have this one:

${variable}

and I can't get how to make this to the variable:

<?php echo number_format($variable, 2, ',', '.') ?>

Any ideas?

Thanks.

EDIT: This is the actual code:

<script id="productTemplate" type="text/x-jquery-tmpl">
                <?php echo "Save:"; ?> ${savings} <?php echo "or"; ?> ${savings_percentage} <?php echo "%"; ?>
</script>

This one outputs:

Save 15 or 3.2302678810608 %

I need to add number_format to ${savings_percentage} so it can output:

Save 15 or 3.23 %

but have no idea how to...

Community
  • 1
  • 1
Nikola Nastevski
  • 917
  • 5
  • 14
  • 25

3 Answers3

2

You're using a jquery plugin (not supported now) called TMPL.

You should convert your data via javascript.

${savings_percentage.toFixed(2)} 

You can use native .toFixed method or use some helper (see for example this question)

Community
  • 1
  • 1
Luca Rainone
  • 16,138
  • 2
  • 38
  • 52
  • This did the "trick", thanks a lot :) Any chance to replace the **.** with **,** ? Currently, it's **3.23 %**, can it be made so it would be: **3,23 %** with **,** instead of **.** ? – Nikola Nastevski Oct 27 '12 at 19:18
  • @NikolaNastevski see the link in my answer :) http://stackoverflow.com/questions/10351003/how-to-format-number-the-using-jquery – Luca Rainone Oct 27 '12 at 19:21
  • Ok, got it, thanks a lot for all your help ;) I also want to thank to all of the others that tried to help me out, really appreciate your help ;) – Nikola Nastevski Oct 27 '12 at 19:25
1

This is not PHP variable. This is template engine variable or something like part of script written in JavaScript.

Its outside PHP script (defined by <?php ?>).

Read template engine or that script manual for more information.

There is no way to access it from PHP, because this is processed by JavaScript on client machine, after server sends html page to browser. PHP is processed on server and you cant pass variables between browser and PHP code directly.

Kamil
  • 13,363
  • 24
  • 88
  • 183
0

${variable} syntax

Unless this is inside a double quote string, this is identical to $variable

Double-quoted string

In a double quoted string, you'd use this syntax where, for example, the variable name would otherwise be misinterpreted.

i.e. Here's some code echoing a variable named $variable:

$variable = "something";
echo "This is a string with a $variable";
// outputs "This is a string with a something"

Here's the same code, but in this case the variable is immediately followed by the string "name":

echo "This is a string with a $variablename";
// outputs "This is a string with a "

In addition to the 'wrong' output, it'll throw an undefined variable error because $variablename isn't defined.

Here's the same example, using curly braces to make explicit what is the variable:

$variable_name = "something";
echo "This is a string with a ${variable}name";
// outputs "This is a string with somethingname"

Template engines

From your edit:

<script id="productTemplate" type="text/x-jquery-tmpl">
    <?php echo "Save:"; ?> ${savings} <?php echo "or"; ?> ${savings_percentage} <?php echo "%"; ?>
</script>

The variables here are not in php tags, so it's not logical to look for what they mean in a php-context. They are just plain text, and as indicated from the script tag - they are intended to be used with the (defunct) jquery tmpl function.

I need to add number_format to ${savings_percentage} so it can output:

Well, fix the js that you haven't put in the question so that it does that :)

AD7six
  • 63,116
  • 12
  • 91
  • 123