When writing code, if you find yourself copy/pasting a chunk of code from one place to another that is something other than “include: ‘header.php'” and “include: ‘footer.php'”, you might want to double-check what you’re doing.

Instead of copy/pasting the code that displays a widget for example, make that code into a function. Call is something like “createWidget()” and then reference that in your code.

In a recent example, I needed to show a notes list on two different view pages. The code was the exact same between the two so I created a function to do it instead. That function looked something like this:

function qt_getNotes($params=array()) {
	$id = $params['id'];
	$query = "SELECT * FROM table WHERE id='".encodeSQL($id)."'";
	$result = getMySQLData($query);
	$out = '';
	while($row = @mysql_fetch_assoc($result)) {
		$out .= 'Author: '.$row['author'].'<br>';
		$out .= $row['body'];
		$out .= '<hr>';
	return $out;

if($command=='adminView') {
	echo "Show admin stuff here.";
	echo qt_getNotes(array('id'=>$id));
	echo "Show admin footer here.";

if($command=='workerView') {
	echo "Show worker stuff here.";
	echo qt_getNotes(array('id'=>$id));
	echo "Show worker footer here.";

This is obviously a pretty simple example in terms of visual sophistication, I just wanted to show how it could be used.

Why did I preface my function with “gt_”? Because this function was designed for use within our “Quality Ticket” area and would not be used outside of that area. Also, I didn’t want to try to double-define any function that may have been previously called “getNotes”.

And why did I use an entire array for passing the $id? Because you’ll likely need to pass other variables to that function at some point in time (like whether or not to show a delete button) and you can now add those as parameters in the array rather than defining additional in-line inputs to the function. Keeps it clean when you have a variable number of possible inputs.

You’ll also notice I used “$out” to store the view components of this rather than just echo’ing it straight onto the page. This comes in handy if you need to store the returned HTML or have it displayed in an email or something like that.

The benefits of using a function like this is that you can change the code once and it effectively is updated in multiple places. You also have your own variable namespace within the function so you can re-use variables like $id, $data, $result, and $row which are commonly used in the body of a page without concern that you’ll overwrite an existing loop you might be in.

The downsides of a function are that they don’t inherit page variables and must be carefully defined once in your system. Variable inheritance is helpful if you have usernames, permissions, or display variables on your page that you would want your function to utilize. To use those variables, you’d need to pass them in your $params array.

August 20th, 2013

Posted In: Functions, Syntax

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: