Page 1 of 1

function help

Posted: Sat Nov 25, 2006 2:44 pm
by paul_20k
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hi 

How can I get the value of the $albnumber out of the function?

Code: Select all

function test($act) 
{ 
$alquery = mysql_query("SELECT * FROM active WHERE apro='Albe' AND ar LIKE '%$act%'"); 
$albnumber=mysql_num_rows($alquery); 
echo $albnumber; 
}
value of $act is coming from other page with $_get['somevalue'].
function is working fine but I am unable to use the value of $albnumber outside of the function. I tried "return" but unable to get the value. I have multiple variables inside the function and I want to use their values inside the script. Anyone can help ?

Thanks


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sat Nov 25, 2006 5:13 pm
by Ambush Commander
Can you post more code (like the surrounding stuff)

Posted: Sat Nov 25, 2006 5:14 pm
by feyd
Client Side :arrow: PHP - Code

... return it.

Posted: Sat Nov 25, 2006 5:16 pm
by Ambush Commander
As in:

Code: Select all

return $albnumber;
...but if you want to return multiple variables this won't work.

Posted: Sat Nov 25, 2006 6:00 pm
by paul_20k
Hi Ambush

rest of the code is just displaying the variables value on the page. There are about 25 variables in all and I am getting all inside the function with $activity1=$_GET['activity1']; and so on...The value of activity1 is coming from other page. So I used the function as test($activity1); . How can I use multiple variable values outside the function? I tried return $albnumber; but its not working at all.

Thanks

Posted: Sat Nov 25, 2006 6:46 pm
by John Cartwright

Code: Select all

function foo($bar) {
   return $bar;
}

$foo = foo('hello');

echo $foo;

//prints 'hello'
Now, if you want to return multiple values, you'll need to stick the values into an array

Code: Select all

function foo() {
   $bar['test1'] = 'foobar';
   $bar['test2'] = 'foobar';
   $bar['test3'] = 'foobar';
   return $bar;
}

$foo = foo();

echo '<pre>';
print_r($foo);
:wink:

Posted: Sat Nov 25, 2006 10:00 pm
by paul_20k
Thanks Jcart

How can I print individual value from that array? like I have 15 elements in the array and I want the separate values of each and want to use that separate value in the other part of the script.

Thanks

Posted: Sat Nov 25, 2006 10:56 pm
by dibyendrah
paul_20k wrote:Thanks Jcart

How can I print individual value from that array? like I have 15 elements in the array and I want the separate values of each and want to use that separate value in the other part of the script.

Thanks
Just like Jcart suggested,

Code: Select all

<?php
function foo() {
   $bar['test1'] = 'foobar1';
   $bar['test2'] = 'foobar2';
   $bar['test3'] = 'foobar3';
   $bar['test4'] = 'foobar4';
   $bar['test5'] = 'foobar5';
   $bar['test6'] = 'foobar6';
   $bar['test7'] = 'foobar7';
   $bar['test8'] = 'foobar8';
   $bar['test9'] = 'foobar9';
   $bar['test10'] = 'foobar10';
   $bar['test11'] = 'foobar11';
   $bar['test12'] = 'foobar12';
   $bar['test13'] = 'foobar13';
   $bar['test14'] = 'foobar14';
   $bar['test15'] = 'foobar15';
   
   return $bar; //now $bar has 15 index with different values
}

$foo = foo();
//print each elements with loop
foreach ($foo as $key=>$value) {
	print $foo[$key]."\n";
}
?>
Output:

Code: Select all

foobar1
foobar2
foobar3
foobar4
foobar5
foobar6
foobar7
foobar8
foobar9
foobar10
foobar11
foobar12
foobar13
foobar14
foobar15
Hope that makes clear enough.

With Regards,
Dibyendra

Posted: Sun Nov 26, 2006 5:56 am
by paul_20k
thanks Dibyendrah