Page 1 of 1

So EZ ur gonna laugh but quick answer?

Posted: Tue Apr 25, 2006 11:38 pm
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.

Posted: Wed Apr 26, 2006 1:49 am
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

Posted: Wed Apr 26, 2006 2:23 am
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: .

Posted: Wed Apr 26, 2006 9:06 am
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>?

Posted: Wed Apr 26, 2006 10:10 am
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:( .

Posted: Wed Apr 26, 2006 10:10 am
by feyd
multiple things can be returned (using an array) as well as objects.

Posted: Wed Apr 26, 2006 10:14 am
by timvw
And references can be used in both php and c++... As already suggested, check the manual.

Posted: Wed Apr 26, 2006 10:20 am
by jito
but feyd if i'm right, Deseree don't want to use the return keyword. is it possible without return?

Posted: Wed Apr 26, 2006 10:22 am
by Maugrim_The_Reaper
Then he can research the use of references in PHP: http://www.php.net/manual/en/language.references.php

Posted: Wed Apr 26, 2006 10:22 am
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.