1

I'm attempting to come up with the best way to use default variables and override them if a $_GET is passed via the URL. Right now, I only have two, but I'll be adding more as I develop the site. Is there a better way?

I'm using define to define the variable and isset to determine if a variable is set and is not NULL.

define("THEME", "atom");
define("VERSION", "1.00");

if(isset($_GET["theme"]))
{
    $theme = $_GET["theme"];
} else
{
    $theme = THEME;
}
if(isset($_GET["version"]))
{
    $version = $_GET["version"];
} else
{
    $version = VERSION;
}

UPDATE I decided on the following. I'm storing my site settings in settings.php:

$settings = array();
$settings['theme'] = 'default';

Then I include that file, use the ternary operator, and then define the variable:

include_once('settings.php');

$theme = (isset($_GET["theme"])) ? $_GET["theme"] : $settings['theme'];
define("THEME", $theme);

4 Answers 4

3

You can use ternary operators to lessen your typing.

$theme = (isset($_GET["theme"])) ? $_GET["theme"] : THEME;
$version= (isset($_GET["version"])) ? $_GET["version"] : VERSION;
Sign up to request clarification or add additional context in comments.

2 Comments

I prefer to use isset() && !empty($_GET['theme'])
This is not safe, you should not only check that it is set - you must also make sure it is valid so a hacker doesn't insert something bad. While you are at it, make sure it's a string/int too!
2

I would do it the other way.

//$theme = config('default.theme');
//$version = config('default.theme_version');
// or
$theme = 'default';
$version = '1.00';

if(isset($_GET["theme"]) AND is_valid_theme($_GET['theme']))
{
    $theme = $_GET["theme"];
}

if(isset($_GET["version"]) AND is_valid_version($_GET['version']))
{
    $version = $_GET["version"];
}

define("THEME", $theme);
define("VERSION", $version);

2 Comments

can you expand on your use of $variable = config('default.variable');? I think this looks like a good way to store site specific variables. Is this a custom ini file? THX
@w0lf42, most frameworks have some kind of configuration loading. config() could load a JSON/XML/INI/YAML/PHP array file to find the default.
1

You could always do something like this:

$defaults = array(
    "theme" => "atom",
    "version" => "1.00"
);

$parameters = array_merge($defaults, $_GET);
extract($parameters);

I first set up an associative array for the default values. I then merge it with the $_GET array using array_merge. Since I mention $_GET as the second parameter, it will override the values if they exist. That way, you get an associative array of parameters. If you want to use them as local variables, you can use extract.

Comments

0

I would use either a switch or ternary statement here for clarity:

define("THEME", "atom");
define("VERSION", "1.00");

isset($_GET['theme'])   ? $theme = $_GET['theme']   : $theme = THEME;
isset($_GET['version']) ? $theme = $_GET['version'] : $theme = VERSION;

Whenever I know I may be later expanding a bit of logic like this I usually take the ternary approach. Keeps everything a bit more readable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.