Maverickb7 Posted April 18, 2007 Share Posted April 18, 2007 Hello. You've probably seen me around asking question before so bear with me because I'm learning as I go along. I have the following files company.php (contains code that displays and sorts companies) company_functions.php (contains functions used within company.php) global_settings.php (contains variables used throughout the site alot) Within global_settings.php I have database information like the names of tables and such. now instead of adding the table name everytime i run a sql query I wanna just be able to add a variable that I can change the value of at any time. On the first line of company.php i require global_settings.php and then the line after I require company_functions.php. I'm trying to use the variables assigned within global_settings.php within functions in company_functions.php but for some reason it doesn't work. I even tried including global_settings.php at the top of company_functions.php. Does anyone know what I'm doing wrong? Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted April 18, 2007 Share Posted April 18, 2007 Yes, function scope This wont work... <?php $myVar = 'blah'; function test() { echo $myVar; // Wont work, not in the function's scope } ?> This, on the other hand, will.... <?php $myVar = 'blah'; function test() { global $myVar; // Call in the variable from the global scope echo $myVar; } ?> or <?php $myVar = 'blah'; function test() { echo $GLOBALS['myVar']; // Use the GLOBALS superglobal } ?> or <?php define('MY_VAR', 'blah'); // Declare a constant, the disadvantage is they CAN'T BE CHANGE function test() { echo MY_VAR; // Use the constant } ?> Quote Link to comment Share on other sites More sharing options...
Maverickb7 Posted April 18, 2007 Author Share Posted April 18, 2007 Thanks. I think you may have helped me by showing the define code. Any suggestions on how not to use it? Quote Link to comment Share on other sites More sharing options...
Maverickb7 Posted April 18, 2007 Author Share Posted April 18, 2007 btw... you wouldn't happen to have a link to a page on php.net that tells me about using $GLOBALS[''] would you? Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted April 18, 2007 Share Posted April 18, 2007 Some people shy away from defining constants except for certain things like giving names to numeric values. I should also mention that in some languages constants tend to be a little bit faster than normal variable (though I don't know if this is true in PHP.) Constants are generally frowned upon because they are just that... constant, they can put a damper on many things if they are used on values that even have a slight chance of needing to change during the course of the script execution. I'd suggest going with a different option unless you're 100% sure the constants wont have to change in the execution of the file. As for your newest question here is a great writeup on variable scope and all the options you have available... http://www.php.net/manual/en/language.variables.scope.php Quote Link to comment Share on other sites More sharing options...
Maverickb7 Posted April 18, 2007 Author Share Posted April 18, 2007 Alright. I won't need to change this variables what so ever. I'm using them within a settings file to display common things like Site Name, Site URL, Site PATH, Table names, and that kinda stuff. None of that will ever have to be modifed unless done so from the settings file itself. So I'm guessing from the sound of it this is exactly what I need and the perfect thing to do the job I want. Would you not agree? Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted April 18, 2007 Share Posted April 18, 2007 I'm actually not in the norm when it comes to what I use constants for. I use constants for almost the exact same thing you are describing (configuration), so I don't see any problem in it while some people may think otherwise. Some people like to isolate it in a class (too messy for me) some people like to use an ini parser (too much overhead for me). Go with what works for you! Quote Link to comment Share on other sites More sharing options...
Maverickb7 Posted April 18, 2007 Author Share Posted April 18, 2007 Sounds good and works great. Thanks for all your advice and help. Quote Link to comment 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.