Is there any chance to read a variable from a function?
Moderator: General Moderators
Is there any chance to read a variable from a function?
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?
-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: Is there any chance to read a variable from a function?
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?
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?
Edit the function, close the connection inside of it.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.
Re: Is there any chance to read a variable from a function?
Functions have their own scope.
Re: Is there any chance to read a variable from a function?
I don't want to, because I need this connection for some more codes after the function, and then I want to close.Mirge wrote:Edit the function, close the connection inside of it.
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......jackpf wrote:Functions have their own scope.
Re: Is there any chance to read a variable from a function?
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.
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?
mysql_close() is essentially called after the script is finished, so more than likely you do not need to call mysql_close()
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Re: Is there any chance to read a variable from a function?
So it's not danger to let Mysql connect open?scottayy wrote:mysql_close() is essentially called after the script is finished, so more than likely you do not need to call mysql_close()
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?
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.
include 'connect.php' is fine
You might want to use require instead.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Re: Is there any chance to read a variable from a function?
ok I will use require 'connect.php' thnx for advice.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.