How to Avoid Conflicting Variable Names

On occasion, variables in PHP may conflict. This is easy to avoid when all the code appears in a single file. What happens when code is scattered throughout the website? Maybe there are variables tucked away in a template file which is brought in with a PHP include. How do you avoid conflicting variables within a file that's used by many other pages on the website?

Let's say we have a section listing our upcoming training events.

<?php
function getEvents() {
     //...code excluded for the sake of simplicity
}
 
//MAKE SURE THE EVENT ID CONTAINS A VALUE
if(!isset($_GET['eventID'])) {
     $_GET['eventID'] = '';
}
 
//GET THE EVENT LIST / EVENT INFORMATION
$eventInfo = getEvents();
 
//SET THE PAGE TITLE
$pageTitle = ($_GET['eventID'] != '') ? 'Event Details' : 'Upcoming Events';
?>
<html>
<head>
<title><?php print $pageTitle; ?></title>
</head>
<body>
<?php
print $eventInfo;
?>
</body>
</html>

With the exception of the getEvents() function, the code should be relatively straight forward. If there are any questions, please ask in the comments section below. The code for the getEvents() function was left out to keep the example simple. It basically returns the upcoming events list or the details associated with the event ID from the GET variable. Before continuing, let's add a return statement to the user-defined function.

<?php
function getEvents() {
     return '<ul><li>Event 1</li><li>Event 2</li><li>Event 3</li></ul>';
}

With the code in place, the upcoming events page displays a bulleted list. Now to add a side bar which appears throughout the website, including the upcoming events page. The side bar shows the next three events.

<?php
function getEvents($version='') {
     if($version == 'sideBar') {
          return '<ul><li>Event 4</li><li>Event 5</li><li>Event 6</li></ul>';
     } else {

          return '<ul><li>Event 1</li><li>Event 2</li><li>Event 3</li></ul>';
     }
}
 
//...snip...
 
<body>
<?php
require 'sideBar.php';
print $eventInfo;
?>
</body>
</html>

The sideBar.php script is currently set up as follows:

<?php
$eventInfo = getEvents('sideBar');
print $eventInfo;
?>

That should be all we need…right? Hey, why does Event 4, 5, and 6 appear in both the side bar and the upcoming events page? It may be obvious now since we just built both sections. But for those that missed it, both sections used the same $eventInfo variable. So whatever happens prior to the side bar code being executed is overwritten.

The variable name in the side bar could be changed to $eventInfo_sideBar. But what if there were quite a few more variables to correct? Instead, let's leverage variable scope in PHP functions.

<?php
function displayEvents() {
     $eventInfo = getEvents('sideBar');
     print $eventInfo;
}
 
displayEvents();

?>

Now, everything works as expected without changing a bunch of variables names and needing to check how they affect other aspects of the website.

Conclusion

Variables created outside of functions are not typically available within functions by default, and vice versa. This state of unawareness could be used to our advantage. If there's any concern that your code may have adverse reactions to the rest of the website, moving the code into a user-defined function could help alleviate those issues.

0 Comments

There are currently no comments.

Leave a Comment