Jump to content


Photo
- - - - -

Make local function var into GOBAL var. PHP


  • Please log in to reply
4 replies to this topic

#1 SpeedCrazy

SpeedCrazy

    Life is one heck of a ride, so hang on tight!

  • Folding Member
  • 3795 posts
  • Gender:Male
  • Location:Between birth and death.

Posted 15 July 2011 - 06:12 PM

Hey Guys,
I need to make a local function variable into a Global variable for access outside of the function in PHP. I have a feeling if i figured out classes i would be able to do it without using functions but currently i need them.
Any ideas?
Thanks,
Speed

Every man dies, but not every man truly lives.

 

Fold for Team OCC
sigimage.gif?c1=FFFFFF&c2=000000&c3=0000
i7 930 @ 3.9 || Dual-SLI GTX 250 || ASUS P6X58D-E || 3x2GB G.Skill Pi @ 1600 || Corsair TX850w || 2x WD Caviar Black 500gb

 

“The problem is not the problem. The problem is your attitude about the problem. Do you understand?”

-Captain Jack Sparrow-


#2 flareback

flareback

    justified through faith

  • Members
  • PipPipPipPip
  • 1759 posts
  • Gender:Male
  • Location:SC, USA

Posted 16 July 2011 - 07:40 AM

Hey Guys,
I need to make a local function variable into a Global variable for access outside of the function in PHP. I have a feeling if i figured out classes i would be able to do it without using functions but currently i need them.
Any ideas?
Thanks,
Speed

Is there a reason you can't return the variable from the function itself to use it? If you can't simply return the value, I would look to see if using a class made sense. If you don't want to mess with classes then you can make the variable global in scope (see this page)
Primary Rig - Intel core i7 920 | Gigabyte EX58 UD4P | 6 GB DDR3 1333 | Sapphire Radeon HD 4850
Secondary Rig - 20 inch iMac | 2.4 Ghz Intel Core 2 Duo | 2 GB DDR2 RAM
Mobile Rig - Asus Eee pc 1000 - 1.6Ghz intel atom | 40Gig SSD | Ubuntu Netbook Remix

#3 Guest_johnBMitchell_*

Guest_johnBMitchell_*
  • Guests

Posted 19 September 2011 - 02:57 AM

Below is the code showing how variables can be declared inside the function as global. I hope you will get solution from this.

<?php 
$v1="Hello 1"; 

function test(){ 
     echo "The value of \$v1= $v1"; 
     // The above line will print The value of $v1= 
     global $v1; 
     echo "<br>The value of \$v1= $v1"; 
     // The above line will print The value of $v1= Hello 1 
     $v2="Hello 2"; 
} 
test(); 
echo "<br>The value of \$v2=$v2"; 
// The above line will print The value of $v2= 
?>


#4 d3bruts1d

d3bruts1d

    Posting Nut

  • Forum Admin
  • PipPipPipPipPipPip
  • 9957 posts
  • Gender:Male
  • Location:Knoxville, TN

Posted 20 September 2011 - 01:35 AM

I recomend using a function with return() as flareback stated. Though it is possible to do what you want... here are some examples:

<?php
// Example 1 - Return value from function
$test_1 = testFunction1();

function testFunction1(){
	return '1';
}
$test_1 += 1;

echo 'Output of test 1:<br />';
echo $test_1; // outputs '2'
echo '<br /><br />';


// Example 2 - Declare global using variable outside of function
$test_2 = 100;
testFunction2();
function testFunction2(){
	global $test_2;
	$test_2 += 99;
}
$test_2 +=1;

echo 'Output of test 2:<br />';
echo $test_2; // outputs '200'
echo '<br /><br />';



// Example 3 - Declare global using variable inside of function
$test_3 = 9999;
testFunction3();
function testFunction3(){
	global $test_3;
	$test_3 = 4;
	$test_3 += 1;
}
$test_3 += 5;

echo 'Output of test 3:<br />';
echo $test_3; // outputs '10'
?>

The opinions expressed in this post are my own and do not necessarily represent that of OverclockersClub.com, its affiliates or sponsors.
If you enjoy my ramblings you may want to visit my blog, follow me on Twitter, or friend me on Facebook.


#5 SpeedCrazy

SpeedCrazy

    Life is one heck of a ride, so hang on tight!

  • Folding Member
  • 3795 posts
  • Gender:Male
  • Location:Between birth and death.

Posted 20 September 2011 - 12:51 PM

I had totally forgotten this thread existed, i went the route suggested by flareback.
Thanks all.

Every man dies, but not every man truly lives.

 

Fold for Team OCC
sigimage.gif?c1=FFFFFF&c2=000000&c3=0000
i7 930 @ 3.9 || Dual-SLI GTX 250 || ASUS P6X58D-E || 3x2GB G.Skill Pi @ 1600 || Corsair TX850w || 2x WD Caviar Black 500gb

 

“The problem is not the problem. The problem is your attitude about the problem. Do you understand?”

-Captain Jack Sparrow-