So EZ ur gonna laugh but quick answer?

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
Deseree
Forum Commoner
Posts: 84
Joined: Mon Feb 13, 2006 11:35 pm

So EZ ur gonna laugh but quick answer?

Post by Deseree »

Sup guys,
lol, your are gonna crack up i'm actually asking this... but..

in c++, you can return varibles in the function name....

does it work in php as well?

Code: Select all

function test($var1, &$var2)
{
$var2 = 10;
}
and now var2 = 10.

Can someone tell me what I did wrong?

Sorry for the n00b quesiotn, I usually use return $var2; to return var's.
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

To return the variable we only can return but like in C/C++ we don't have to declare the return type.

Code: Select all

function test($var1, &$var2)
{
return($var2 = 10);
}
Or to return by casting the variable to integer,

Code: Select all

function test($var1, &$var2)
{
return((int) $var2 = 10);
}
Or to return by casting the variable to string,

Code: Select all

function test($var1, &$var2)
{
return((string) $var2 = 10);
}
I might have elaborated too much. Sorry if I misunderstood your question.

Cheers,
Dibyendra
jito
Forum Commoner
Posts: 85
Joined: Sat Mar 25, 2006 4:32 am
Location: india

Post by jito »

do u have a little time to check the manual?
http://www.zend.com/manual/functions.re ... values.php
you can even return multiple values by returning a array or object :wink: .
Deseree
Forum Commoner
Posts: 84
Joined: Mon Feb 13, 2006 11:35 pm

Post by Deseree »

jito wrote:do u have a little time to check the manual?
http://www.zend.com/manual/functions.re ... values.php
you can even return multiple values by returning a array or object :wink: .
ah bummer.... I didn't want to use the return $var; method at the end of the function... maybe i'll just make it a global variable and that will work, sucks it doesn't work like c/c++.....

makes me feel better now that i konw that &$var2 doesn't work alone and you still have to return..... you can't return multiple different vars ( not arrays or objects ) using that method can you>?
jito
Forum Commoner
Posts: 85
Joined: Sat Mar 25, 2006 4:32 am
Location: india

Post by jito »

Deseree wrote:
ah bummer.... I didn't want to use the return $var;
hi Deseree,
i'm really sorry for i didn't notice the last two lines of your post and i don't knwo if it's possible without return.
i'm also interested to know some new information aboout that, plese post if you have something lately.
once again i am really sorry:( .
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

multiple things can be returned (using an array) as well as objects.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

And references can be used in both php and c++... As already suggested, check the manual.
jito
Forum Commoner
Posts: 85
Joined: Sat Mar 25, 2006 4:32 am
Location: india

Post by jito »

but feyd if i'm right, Deseree don't want to use the return keyword. is it possible without return?
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Then he can research the use of references in PHP: http://www.php.net/manual/en/language.references.php
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

As has already been said, references may be used but it is best to use the function's return for most return data as that's what it is for.
Post Reply