maharg
Technical User
- Mar 21, 2002
- 184
Hi,
I am trying to create a string variable with some php code in it , which will be used multiple times in a document. This is so if I want to edit that code, I only need edit that one variable.
For example, I would simply like to put the following code snippet...
... into a variable called $poundsformat, which I will then introduce in the script with echo $format.
The array will be in an include file and will be much larger than this example.
I am getting parse errors, which didn't surprise me, but I have been unable to find a solution. I tried HEREDOC, EOD, EOT, double quotes, curly braces etc!
Any help would be much appreciated!
Many thanks,
Graham
Here is an example of the code I am testing with....
I am trying to create a string variable with some php code in it , which will be used multiple times in a document. This is so if I want to edit that code, I only need edit that one variable.
For example, I would simply like to put the following code snippet...
Code:
fprintf('%01.2f', $price)
... into a variable called $poundsformat, which I will then introduce in the script with echo $format.
The array will be in an include file and will be much larger than this example.
I am getting parse errors, which didn't surprise me, but I have been unable to find a solution. I tried HEREDOC, EOD, EOT, double quotes, curly braces etc!
Any help would be much appreciated!
Many thanks,
Graham
Here is an example of the code I am testing with....
Code:
<html>
<head>
</head>
<body>
<?php
// Currency format, rounded and force 2 decimal places if whole numbers
$poundsformat = fprintf('%01.2f', $price) ;
// Exchange rates
$pounds=1;
$euros=1.1397.
$usdollars=1.5602;
$cdndollars=1.5769;
// Prices arrays
$prices = array (
"intuitive" => array
(
"INT2-AH" => "173",
"INT2-C" => "168",
"INT2-L" => "173",
"INT2-P" => "142",
"INT2-S2" => "163",
"INT2-S4" => "184",
"INT2-H" => "170",
"INT2-R" => "163",
"INT-T" => "125",
"INT2-I" => "157",
),
"tester" => array
(
"ABC" => "400",
"DEF" => "887",
),
);
?>
<table width=800>
<tr> <th style="text-align:left">Part No.</th> <th style="text-align:left">Description</th><th style="text-align:left">GB Pounds</th><th style="text-align:left">Euros</th><th style="text-align:left">US Dollars</th><th style="text-align:left">Cdn Dollars</th><th>Group</th></tr>
<tr><td>INT2-AH</td><td>Ampere hour meter</td><td><?php $price=($prices["intuitive"]["INT2-AH"]); echo $poundsformat ?></td><td><?php echo sprintf("%01.2f", round(($euros* $price),2)); ?></td><td><?php echo sprintf("%01.2f", round(($usdollars* $price),2)); ?></td><td><?php echo sprintf("%01.2f", round(($cdndollars* $price),2)); ?></td><td bgcolor="blue"> </td></tr>
<tr><td>INT2-C</td><td>Totaliser/Rate meter</td><td><?php $price=($prices["intuitive"]["INT2-C"]); echo sprintf("%01.2f", $price); ?></td><td><?php echo sprintf("%01.2f", round(($euros* $price),2)); ?></td><td><?php echo sprintf("%01.2f", round(($usdollars* $price),2)); ?></td><td><?php echo sprintf("%01.2f", round(($cdndollars* $price),2)); ?></td><td bgcolor="blue"> </td></tr>
<tr><td>INT2-P</td><td>Process meter</td> <td><?php $price=($prices["intuitive"]["INT2-P"]); echo sprintf("%01.2f", $price); ?></td><td><?php echo sprintf("%01.2f", round(($euros* $price),2)); ?></td><td><?php echo sprintf("%01.2f", round(($usdollars* $price),2)); ?></td><td><?php echo sprintf("%01.2f", round(($cdndollars* $price),2)); ?></td><td bgcolor="blue"> </td></tr>
</table>
</body>
</html>