Home > Archive > PHP Language > September 2005 > Returning a value from a function
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Returning a value from a function
|
|
| Shawn Wilson 2005-09-28, 9:55 pm |
| Currently I have a function written that looks up a value from a database.
The basic function is like this:
function getval($id) {
<snip sql lookup part using $id>
$bob = 'stuff from sql';
return $bob;
}
In my calling code I have this:
$bob = getval('101');
My question is, do I have to assign the $bob variable in the calling
position? Can I just do this:
getval('101','bob');
....and somehow end up with the value already set to the $bob variable?
The point here is to have this function get certain control values from a
control table and set a corisponding variable. I want to end up with one
function that I can use to pull all my control values when I need them.
Can I get to this point:
getval('102','joe'); # then $joe equals something
getval('103','sam'); # then $sam equals something
getval('104','dave'); # then $dave equals something
without haveing to do this:
$joe = getval('102');
It's a small difference, but I'd like to do it if possible.
A thought was to use a session variable maybe, but that seems to have it's
own issues to me.
Thanks in advance!
--
Shawn Wilson
| |
| Colin McKinnon 2005-09-29, 7:55 am |
| Shawn Wilson wrote:
> Currently I have a function written that looks up a value from a database.
> The basic function is like this:
>
> function getval($id) {
> <snip sql lookup part using $id>
> $bob = 'stuff from sql';
> return $bob;
> }
>
> In my calling code I have this:
>
> $bob = getval('101');
>
>
> My question is, do I have to assign the $bob variable in the calling
> position? Can I just do this:
>
> getval('101','bob');
>
> ...and somehow end up with the value already set to the $bob variable?
Sort of, but a better way would be:
function getval($id, &$dest) // note $dest is prefixed by &
{
.....
$dest = 'stuff from sql';
}
getval('101',$bob);
C.
| |
| Hilarion 2005-09-29, 6:56 pm |
| > function getval($id) {
> <snip sql lookup part using $id>
> $bob = 'stuff from sql';
> return $bob;
> }
>
> In my calling code I have this:
>
> $bob = getval('101');
>
> [...]
>
> getval('102','joe'); # then $joe equals something
> getval('103','sam'); # then $sam equals something
> getval('104','dave'); # then $dave equals something
To set global variables:
$sth = array( 'joe' => '102', 'sam' => '103', 'dave' => '104' );
foreach( $sht as $var_name => $id )
$GLOBALS[ $var_name ] = getval( $id );
to set local variables:
$sth = array( 'joe' => '102', 'sam' => '103', 'dave' => '104' );
foreach( $sht as $var_name => $id )
$$var_name = getval( $id );
Hilarion
|
|
|
|
|