Passing default values for argument of type array

Hello everyone,

Any suggestions as to how to streamline this? Feels like I shouldn’t need the extra $title etc. variables.

==

<?php

$variables['title'] = 'default title';
$variables['content'] = 'default content';
$variables['id'] = 'default id';
	
function load($variables) {
	$title = $variables['title'];
	$content = $variables['content'];
	$id = $variables['id'];
	include 'index.html.php';
	exit();
}

$page = 'home';
if (!empty($_GET)) {
	$getArray =  array_keys($_GET);
	$page = $getArray[0];
}

switch ($page) {

	case 'home':
	$variables['title'] = 'home';
	$variables['content'] = 'home' . '.php';
	$variables['id'] = 'home';
	load($variables);
	break;
	
	case 'about':
	$variables['title'] = 'about';
	$variables['content'] = 'about' . '.php';
	$variables['id'] = 'about';
	load($variables);
	break;

	#DEFAULT#
	
	default:
	$variables['title'] = 'error';
	$variables['content'] = 'error' . '.php';
	$variables['id'] = 'error';
	load($variables);
	exit();
}

	/*
		'title' => 'default title'
	,	'content' => 'default content'
	,	'id' => 'default id'
	*/

foreach($variables as $key=>$value) {
    $$key = $value
}

So, taking a url query string such as this: ?action=home


//spoof a GET request for now:
$_GET['action'] = 'home';

// create a white list
$permitted = array('home', 'about', 'error');

// set some defaults:
$variables['title'] = 'default title';
$variables['content'] = 'default content';
$variables['id'] = 'default id';

if( in_array($_GET['action'], $permitted) ){

// set up some vars if they are in your white list $permitted

	$variables['title'] = $_GET['action'];
	$variables['content'] = $_GET['action'] . '.php';
	$variables['id'] = $_GET['action'];

}

var_dump( $variables );

I mean your example is simplistic, so the solution is also simplistic.

In the real world are you not more likely to be dealing with output which is more akin to:


	$variables['title'] = "Welcome to the home page";
	$variables['content'] = 'home.php';
	$variables['id'] = 23;


Thank you.

However, if I put the foreach loop into the function, the default values automatically overwrite anything I have specified below.

What I am looking for is a way to load the index.html.php page: this is why I want to pass the variables as an array.

Then create a whitelist before looping

Never mind. In my index.html.php, I was referring to stuff as $title instead of $variables[‘title’]. Duh! Sorry to bug you.

I think I got it figured out. I’ll post it here once I’m finished in case it is of any help to anyone down the road.

Thanks again :slight_smile:

Here is what I came up with in the end. Seems to work ok.

<?php

$variables['title'] = 'default title';
$variables['content'] = 'default content';
$variables['id'] = 'default id';
	
function load(array $variables) {
	include 'index.html.php';
	return;
}

$page = 'home';
if (!empty($_GET)) {
	$getArray =  array_keys($_GET);
	$page = $getArray[0];
}

switch ($page) {

	case 'home':
	$variables['title'] = 'home';
	$variables['content'] = 'home' . '.php';
	$variables['id'] = 'home';
	break;
	
	case 'about':
	$variables['title'] = 'about';
	$variables['content'] = 'about' . '.php';
	$variables['id'] = 'about';
	break;

	#DEFAULT#
	
	default:
	$variables['title'] = 'error';
	$variables['content'] = 'error' . '.php';
	$variables['id'] = 'error';
}

load($variables);

And the index.html.php:

title is <?php echo $variables['title'];?> <br />
content is  <?php echo $variables['content'];?> <br />
id is <?php echo $variables['id'];?> <br />

Be aware that include resolves in the scope of that function - its code will not be able to see any variables that were set in the global scope.

So why are you statically calling page title, etc based on the request? This makes for messy code. Why not set up a simple cms?

After setting up a simple .htaccess file:

MySQL Table:


tbl_cms
---------
page_slug --page url from root, PK
page_title
page_keywords
page_content

Index.php


$pageRequest = $_SERVER['REQUEST_URI'];

$dbs = new PDO('mysql:host=127.0.0.1;dbname=dbname', 'username', 'password');
       
$statement = $dbs->prepare("select * from tbl_cms where page_slug = '$pageRequest'");
$statement->execute();
$result = $statement->fetchAll();

if(count($result) == 1) {
//pull vars from $result
} else {
//throw a 404
}

Over simplified of course, but makes for less, cleaner and more extendable code.

Ok, sure thing!

Thank you – that’s great advice!

BTW, i gave a bad example of PDO, make sure you use parameter binding to avoid sql injection.

Got it. Thanks a million!