PHP echo replacement for a variable
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...
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)
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.
${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.
ie 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 :)
链接地址: http://www.djcxy.com/p/59532.html上一篇: 这个语法是什么意思? $节点
下一篇: PHP回声替换为一个变量