Page 1 of 1
Is there any chance to read a variable from a function?
Posted: Fri Oct 09, 2009 4:27 pm
by MicroBoy
Is there any chance to read a variable from a function? I have a function and in that function are some variables, I want to use these variables after I use for example functionOne(), so I want to read the variables from functionOne() ? Is this possible?
Re: Is there any chance to read a variable from a function?
Posted: Fri Oct 09, 2009 4:32 pm
by Mark Baker
return the variable from the function
Code: Select all
function test($a,$b) {
$c = $a + $b;
return $c;
}
$x = test(2,3);
echo $test;
Re: Is there any chance to read a variable from a function?
Posted: Fri Oct 09, 2009 6:50 pm
by MicroBoy
Maybe you explained very well, but I'm still confuse. Look, I have a function which is used to connect to database. In that function the connection is still opened, now I want to close the connection with mysql_close($con); but the variable $con is not recognized.
Re: Is there any chance to read a variable from a function?
Posted: Fri Oct 09, 2009 6:54 pm
by Mirge
MicroBoy wrote:Maybe you explained very well, but I'm still confuse. Look, I have a function which is used to connect to database. In that function the connection is still opened, now I want to close the connection with mysql_close($con); but the variable $con is not recognized.
Edit the function, close the connection inside of it.
Re: Is there any chance to read a variable from a function?
Posted: Fri Oct 09, 2009 6:57 pm
by jackpf
Functions have their own scope.
Re: Is there any chance to read a variable from a function?
Posted: Fri Oct 09, 2009 7:16 pm
by MicroBoy
Mirge wrote:Edit the function, close the connection inside of it.
I don't want to, because I need this connection for some more codes after the function, and then I want to close.
jackpf wrote:Functions have their own scope.
What do you suggest what to use to open a connection, because I will need it in many files, and in many places. I don't want that every time I need it to write mysql_connect......
Re: Is there any chance to read a variable from a function?
Posted: Fri Oct 09, 2009 8:15 pm
by jackpf
Have the connection function return a handle. Then you can pass that handle to your closing function.
I personally use a class, so I keep the handle as a private property. But yeah, up to you.
Re: Is there any chance to read a variable from a function?
Posted: Fri Oct 09, 2009 9:08 pm
by s.dot
mysql_close() is essentially called after the script is finished, so more than likely you do not need to call mysql_close()
Re: Is there any chance to read a variable from a function?
Posted: Sat Oct 10, 2009 5:32 am
by MicroBoy
scottayy wrote:mysql_close() is essentially called after the script is finished, so more than likely you do not need to call mysql_close()
So it's not danger to let Mysql connect open?
and can you tell me if it's a good way to use for example include('connect.php'); ?
Re: Is there any chance to read a variable from a function?
Posted: Sat Oct 10, 2009 3:21 pm
by s.dot
It's fine to not explicity close connections.. I never do unless I have two connections to different databases open.
include 'connect.php' is fine
You might want to use require instead.
Re: Is there any chance to read a variable from a function?
Posted: Sat Oct 10, 2009 4:29 pm
by MicroBoy
scottayy wrote:It's fine to not explicity close connections.. I never do unless I have two connections to different databases open.
include 'connect.php' is fine
You might want to use require instead.
ok I will use require 'connect.php' thnx for advice.