0

I'm trying to get variables that I defined while in a function from another function I called in that function, example:

$thevar = 'undefined';
Blablahblah();
echo $thevar; (should echo blaaah)
function Blahedit(){

     echo $thevar; (should echo blah)
     $thevar = 'blaaah';

}
function Blablahblah(){

     global $thevar;
     $thevar = 'blah';
     Blahedit();

}

I want to know if there's another way of doing this without passing down params to Blahedit(), get_defined_vars gives me vars within the function not $thevar... and calling global $thevar will just give me the previous unedited version.

Please help ):

Xansy
  • 53
  • 1
  • 3
  • 6
  • You already did this in the latter function, just add `global $thevar;` to the former function as well. – AlecTMH Dec 29 '12 at 14:32
  • 2
    *Why?!* Why would you want such a thing? It burns my eyes just looking at it! – Madara's Ghost Dec 29 '12 at 14:33
  • 1
    Global variables are a very bad idea. Why not use parameters? Simpler and safer – Ed Heal Dec 29 '12 at 14:34
  • global $thevar; gives me $thevar = 'undefined'... – Xansy Dec 29 '12 at 14:35
  • I just want to know if there's another way of going about, and yes it is simpler thank you Ed and Madara for being the only two actually reading what I said ♥ – Xansy Dec 29 '12 at 14:36
  • The other thing I can do is use a class obj (which is in my actual code) define variables there, assign them to the var and pass that back, but that is a mess and would be stupid considering the fact that im just using those variables for 1 function. – Xansy Dec 29 '12 at 14:47
  • @user1936522 - Why is that stupid? OOD would not go a miss here as it will make your code scalable. – Ed Heal Dec 29 '12 at 17:19

3 Answers3

0

You can use this: http://php.net/manual/en/reserved.variables.globals.php

or better have a look at oop

http://php.net/manual/en/language.oop5.php http://php.net/manual/en/language.oop5.basic.php

GreenRover
  • 1,486
  • 1
  • 14
  • 33
0

You can pass the variables as a reference parameter (shown below), encapsulate your code in a class and use your variable as class attribute or let the functions return the changed variable.

$thevar = 'undefined';
Blablahblah($thevar);
echo $thevar; 

function Blahedit(&$thevar){
     echo $thevar;
     $thevar = 'blaaah';
}

function Blablahblah(&$thevar){
     $thevar = 'blah';
     Blahedit($thevar);
}

Using globals inside functions is considered to be a bad practice. However, passing a lot of variables by reference also is not good style.

If you want to get your code to work the way it is, you have to add a global $thevar to your edit function:

function Blahedit(){
     global $thevar;
     echo $thevar; (should echo blah)
     $thevar = 'blaaah';
}
David Müller
  • 5,291
  • 2
  • 29
  • 33
  • I already know this is possible through params, I want to know if there's another way without doing so. – Xansy Dec 29 '12 at 14:41
0

Just global $thevar inside blahedit.

function Blahedit(){
    global $thevar;
    echo $thevar; //(should echo blah)
    $thevar = 'blaaah';

}
Menztrual
  • 40,867
  • 12
  • 57
  • 70
  • Global calls upon the old $thevar (undefined) and I want the edited version passed by blablahblah – Xansy Dec 29 '12 at 14:49