xor83 Posted November 18, 2010 Share Posted November 18, 2010 This question may sound very lame... ... But I have never able to know how to define a variable that I can use in different functions in a same file. There is no class structure just a PHP file with some functions. Is it possible to use some type of variable that can be share by all functions and that variable should not be a session variable . Something like: <?php $my_global_var; function A() { // use $my_global_var here } function B() { // use $my_global_var here and should retain the last value } ?> Something like in VB6.... Thanks Quote Link to comment https://forums.phpfreaks.com/topic/219059-something-global/ Share on other sites More sharing options...
litebearer Posted November 18, 2010 Share Posted November 18, 2010 See... http://php.net/manual/en/language.variables.scope.php example... <?php $a = 1; $b = 2; function Sum() { global $a, $b; $b = $a + $b; } Sum(); echo $b; ?> Quote Link to comment https://forums.phpfreaks.com/topic/219059-something-global/#findComment-1135991 Share on other sites More sharing options...
xor83 Posted November 18, 2010 Author Share Posted November 18, 2010 thax for ur reply litebearer This is what I understood to able to use any variable like $a and $b I have to write again in that function global $a, $b its like defining that variable again... ... but its working.... Quote Link to comment https://forums.phpfreaks.com/topic/219059-something-global/#findComment-1135997 Share on other sites More sharing options...
PFMaBiSmAd Posted November 18, 2010 Share Posted November 18, 2010 Please don't suggest using the global keyword. In the example you posted, what if I have more than just two variables in my application that I need to use with the function and want to reuse the function? <?php $a = 1; $b = 2; $w = 44; $x = 184; $y = 12; $z = 13; ?> ^^^ I need to use the function to find what output it gives for $w and $x and for $y and $z. Writing the function using global, it is not a general purpose function and it can only operate on the data it was hard coded to work with. You would need to use more code to copy the $w and $x values to $a and $b, call the function, then copy $y and $z to $a and $b before you can call the function again. Functions should aways be written to be general purpose and use parameters when there are called. Quote Link to comment https://forums.phpfreaks.com/topic/219059-something-global/#findComment-1136000 Share on other sites More sharing options...
PFMaBiSmAd Posted November 18, 2010 Share Posted November 18, 2010 In real applications, where you can have hundreds of functions, it creates more work and wastes time keeping track of all the variables you are passing into functions using the global keyword because you cannot duplicate any of the names between functions in order to prevent interference between the functions. Using functions are supposed to make coding easier and take less time. Not more time. Don't use the global keyword. Quote Link to comment https://forums.phpfreaks.com/topic/219059-something-global/#findComment-1136003 Share on other sites More sharing options...
xor83 Posted November 18, 2010 Author Share Posted November 18, 2010 So what should I use to do something like that? can you give me 1 example? Quote Link to comment https://forums.phpfreaks.com/topic/219059-something-global/#findComment-1136024 Share on other sites More sharing options...
trq Posted November 18, 2010 Share Posted November 18, 2010 Functions accept arguments, Use them. Quote Link to comment https://forums.phpfreaks.com/topic/219059-something-global/#findComment-1136037 Share on other sites More sharing options...
litebearer Posted November 18, 2010 Share Posted November 18, 2010 perhaps function Money_needed($a,$b) { $sub_total = $a * $b; $taxes = $sub_total * .06; $cash_needed = $sub_total + $taxes; return $cash_needed; } $price = 37; $qty = 6; echo Money_needed($price, $qty); Quote Link to comment https://forums.phpfreaks.com/topic/219059-something-global/#findComment-1136045 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.