Jump to content

[SOLVED] GLOBAL Settings


Maverickb7

Recommended Posts

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?

Link to comment
Share on other sites

Yes, function scope :D

 

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
}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.