PHP: How to use function to either echo result or to save it in a variable (partially working) -


i new php , hope can me this.

i have following function can pass itemid (unique integer) use return corresponding translation created array (this final part).

php function (saved in header file):

function fetchtransmain($trans, $itemid){     foreach($trans $key => $val){         if($val["id"] == $itemid){             echo $val["trans"];         }     } } 

this works intended long refer function anywhere on html page follows.

echoing result on html page (working):

echo fetchtransmain($trans, someid); 

however, have scenarios instead of echoing result page need save variable. tried following still echoes somewhere @ top of screen.

save result variable instead of echoing (not working):

$somevariable = fetchtransmain($trans, someid); 

can tell me how can use function both echo result (working part) and save variable alternatively (not working part) - needed ?

many in advance, mike

just return value in function instead of echoing it:

function fetchtransmain($trans, $itemid){     foreach($trans $key => $val){         if($val["id"] == $itemid){             return $val["trans"];         }     } } 

then, when want print do:

echo fetchtransmain($trans, someid); 

otherwise do:

$somevariable = fetchtransmain($trans, someid); 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -