aximbigfan Posted March 8, 2008 Share Posted March 8, 2008 I made an app in PHP, where a large string of vars is loaded (and can be reloaded in certain instances to validate them). Basically, I want to have a function where vars are loaded, but I don't want them to be global, because they need to stay in the validation functions. So, I know this won't work, but this is what I want to do: INSTEAD of this: $var1 = $systemvars['var1'] $var2 = $systemvars['var2'] ect..... I WANT to do this: function load_vars() { $var1 = $systemvars['var1'] $var2 = $systemvars['var2'] ect..... } function valid() { load_vars); if (isset($var1 )) echo "hi!"; } Thanks. chris Quote Link to comment https://forums.phpfreaks.com/topic/95001-function-to-load-vars/ Share on other sites More sharing options...
amites Posted March 8, 2008 Share Posted March 8, 2008 sounds like you want to work within a class, though hard to tell based on your question, rather vague Quote Link to comment https://forums.phpfreaks.com/topic/95001-function-to-load-vars/#findComment-486706 Share on other sites More sharing options...
Stooney Posted March 8, 2008 Share Posted March 8, 2008 You could use constants if the values don't change. Pardon my possibly wrong terminology, but constants are basically in the same scope and globals; they can be used anywhere. <?php define('system_var1', 'valuehere'); //Then to call it.. echo system_var1; //You don't use $ with constants. And can't be called from within string, gotta concatenate em... echo 'System Var1='.system_var1; ?> Quote Link to comment https://forums.phpfreaks.com/topic/95001-function-to-load-vars/#findComment-486712 Share on other sites More sharing options...
aximbigfan Posted March 8, 2008 Author Share Posted March 8, 2008 I did kind of think about having a class. How do classes work? I can't find a straight explanation. Like I said, it can't be global. The values do change. Chris Quote Link to comment https://forums.phpfreaks.com/topic/95001-function-to-load-vars/#findComment-486719 Share on other sites More sharing options...
tippy_102 Posted March 8, 2008 Share Posted March 8, 2008 foreach will take care of this: http://ca.php.net/foreach Quote Link to comment https://forums.phpfreaks.com/topic/95001-function-to-load-vars/#findComment-486724 Share on other sites More sharing options...
laffin Posted March 8, 2008 Share Posted March 8, 2008 <?php function load_vars() { return array('arg1' => 'Arg 1', 'arg2' => 'Arg 2'); } function my_sub() { extract(load_vars()); echo "$arg1<br>$arg2"; } my_sub(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/95001-function-to-load-vars/#findComment-486726 Share on other sites More sharing options...
aximbigfan Posted March 8, 2008 Author Share Posted March 8, 2008 oooooooooo......... So..... Thats how you ret arrays... Thanks, I'll try that... Chris Quote Link to comment https://forums.phpfreaks.com/topic/95001-function-to-load-vars/#findComment-486728 Share on other sites More sharing options...
aximbigfan Posted March 8, 2008 Author Share Posted March 8, 2008 One more thing... Say I want to inject this into something: $var = "var" $var2 = "var" is there some way I could make just a one line solution to this? For example function test() { <one line code to inject var "table" here> echo $var; Chris Quote Link to comment https://forums.phpfreaks.com/topic/95001-function-to-load-vars/#findComment-486733 Share on other sites More sharing options...
aximbigfan Posted March 8, 2008 Author Share Posted March 8, 2008 Never mind! Laffin solved my problem in a differnt thread. Basically, I created a var with my vars in it, and when I am ready to parse those vars... eval($vars); I would hit the "topic solved" button, but I think it ran away :'(. Chris Quote Link to comment https://forums.phpfreaks.com/topic/95001-function-to-load-vars/#findComment-486745 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.