Is there any chance to read a variable from a function?

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
MicroBoy
Forum Contributor
Posts: 112
Joined: Sat Mar 14, 2009 5:16 pm

Is there any chance to read a variable from a function?

Post 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?
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?

Post 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;
 
MicroBoy
Forum Contributor
Posts: 112
Joined: Sat Mar 14, 2009 5:16 pm

Re: Is there any chance to read a variable from a function?

Post 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.
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Is there any chance to read a variable from a function?

Post 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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Is there any chance to read a variable from a function?

Post by jackpf »

Functions have their own scope.
MicroBoy
Forum Contributor
Posts: 112
Joined: Sat Mar 14, 2009 5:16 pm

Re: Is there any chance to read a variable from a function?

Post 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......
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Is there any chance to read a variable from a function?

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Is there any chance to read a variable from a function?

Post 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()
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.
MicroBoy
Forum Contributor
Posts: 112
Joined: Sat Mar 14, 2009 5:16 pm

Re: Is there any chance to read a variable from a function?

Post 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'); ?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Is there any chance to read a variable from a function?

Post 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.
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.
MicroBoy
Forum Contributor
Posts: 112
Joined: Sat Mar 14, 2009 5:16 pm

Re: Is there any chance to read a variable from a function?

Post 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.
Post Reply