Make local function var into GOBAL var. PHP
#1
Posted 15 July 2011 - 06:12 PM
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
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
Posted 16 July 2011 - 07:40 AM
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)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
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_*
Posted 19 September 2011 - 02:57 AM
<?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
Posted 20 September 2011 - 01:35 AM
<?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
Posted 20 September 2011 - 12:51 PM
Thanks all.
Every man dies, but not every man truly lives.
Fold for Team OCC
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-













