How can I split PHP up and reuse parts of it? -
i'm trying split line of php up, reason being don't need of code , can reuse parts of it.
the main reason i'm doing because currency shows more digits currencies e.g. 1.2562 instead of 1.25, want use substr function on get's , able modify other get's.
symbol required, substr isn't, $converter required, end part of substr isn't can change, new currency required.
$symbol[2] . substr(($converter->convert($defaultcurrency, $newcurrency) * 1), 0, 4) . " <b>" .$newcurrency. "</b>";
i've tried doing explode, i'm not entirely sure how have never had split before i'm little puzzled on how go it.
once code has gone through checking current 1 set, want grab specified split code pieces , output it.
put code in function this:
function formatcurrency($symbol, $converter, $defaultcurrency, $newcurrency, $factor=1.0, $suffix="", $substrchars=0) { if($substrchars>0) { return $symbol . substr(($converter->convert($defaultcurrency, $newcurrency) * $factor), 0, $substrchars) . $suffix . " <b>" . $newcurrency. "</b>"; } else { return $symbol . ($converter->convert($defaultcurrency, $newcurrency) * $factor) . $suffix . " <b>" . $newcurrency. "</b>"; } }
if call without $substrchars
parameter, omit substr()
call, otherwise strip first $substrchars
characters:
if( $_get['currency'] === "gbp" ){ $newcurrency = $_get['currency']; $string = formatcurrency($symbol[1], $converter, $defaultcurrency, $newcurrency, 2.0, ".00", 0); } elseif( $_get['currency'] === "usd" ){ $newcurrency = $_get['currency']; $string = formatcurrency($symbol[2], $converter, $defaultcurrency, $newcurrency, 1.0, "", 4); }
this solution readable because see difference between 2 branches in conditional statement.
Comments
Post a Comment