ptmuldoon
03-30-2007, 04:55 PM
I'm trying to learn more about functions, how they work, etc, and have a quick, hopefully easy question.
How do you possibly echo a specific variable from function? In experimenting, I tried the below simple code, but no luck with it.
function lets_learn(){
$a = 'variable_a';
$b = 'variable_b';
}
$a = lets_learn($a);
$b = lets_learn($b);
echo $b;
Nightfire
03-30-2007, 05:09 PM
Can only return 1 variable from a function (from what I've experienced anyway). This is what you'll have to do.
function lets_learn($which){
if($which == 'a'){
$var = 'variable_a';
}else{
$var = 'variable_b';
}
return $var;
}
echo lets_learn('a');
If you want to show variable_b, then change the letter a to b
echo lets_learn('b');
david_kw
03-30-2007, 05:11 PM
I'm not sure what you are trying to learn but try this one. :)
<?php
function lets_learn($b) {
$a = 'variable_a<br>';
echo $a;
return ($b);
}
$b = lets_learn('variable_b<br>');
echo $b;
?>
david_kw
iLLin
03-30-2007, 05:51 PM
You can return as many variables you want. Just put them in an array and return them all :D, then echo the one you want.
function lets_learn(){
$learn = array();
$learn['a'] = 'variable_a';
$learn['b'] = 'variable_b';
return $learn;
}
$learn = lets_learn();
echo $learn['b'];