function help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
paul_20k
Forum Commoner
Posts: 45
Joined: Fri Nov 10, 2006 7:02 pm

function help

Post 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]
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Can you post more code (like the surrounding stuff)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Client Side :arrow: PHP - Code

... return it.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

As in:

Code: Select all

return $albnumber;
...but if you want to return multiple variables this won't work.
paul_20k
Forum Commoner
Posts: 45
Joined: Fri Nov 10, 2006 7:02 pm

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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:
paul_20k
Forum Commoner
Posts: 45
Joined: Fri Nov 10, 2006 7:02 pm

Post 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
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post 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
paul_20k
Forum Commoner
Posts: 45
Joined: Fri Nov 10, 2006 7:02 pm

Post by paul_20k »

thanks Dibyendrah
Post Reply