Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWeb Page and Site Features

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Site-Wide Variables

Site-wide variables (examples are a phone number or an advertisement) can be accessed and their values published on any PHP web pages on the web site.

The variables and their values are all in one file. The rest of the website gets the values from that file for publishing.

Here are ideas for using site-wide variables. The variable values can be plain text or any valid HTML and/or JavaScript code.

  • Copyright notice.
  • Phone numbers.
  • Addresses.
  • A div with special information.
  • Event names and dates.
  • Social media links.
  • Ads published site-wide that frequently change.

The file that contains the variables and their values is a PHP file. Let's call it the central data file.

Change the value of any variable in the central data file and the change is automatically propagated throughout your website.

• The pages of your website have one line of PHP code to associate the page with the central data file.

• A snippet of PHP code on a web page is used to publish the value of any site-wide variable.

With the site-wide variable system in place, you can make a site-wide change (like an event registration cutoff date, for example) in one spot rather than changing multiple pages.

The central data file with the site-wide variables and their values is a PHP file, written so PHP can understand it. An example with copious notes is further below.

What Is Required

Implementation requires 3 things.

  1. The central data file.

  2. A couple lines of PHP code on every web page to associate with the central data file.

  3. A snippet of PHP code where site-wide variables will be used.

The Central Data File

Here is an example of a central data file with variables for site-wide use. As you can see, it is heavily commented. Either create your own or update this example for your implementation.

<?php
# Make as many or as few custom site-wide variables as you need. The format is 
#     $SiteWideCustomVariable["KEY"]
# Replace KEY with a string of characters unique to this page.
#
# For each custom site-wide variable, specify a value in 
#   this format (without "#" leading the lines):
#
#     $SiteWideCustomVariable["testing-login"] = <<<SITEWIDE
#     [single or multiple-line value goes here]
#SITEWIDE;
#
# As you see above, the value for the site-wide variable is between the lines that 
#   that contain the word SITEWIDE
#     Important -> The second SITEWIDE line MUST begin at the left margin.
# 
# 
# For comments, begin the line with a # character, like this and the above lines do.
#
# Here are examples of assigning values to custom variables.

# Event telephone number.
$SiteWideCustomVariable["eventphone"] = <<<SITEWIDE
123-555-4567
SITEWIDE;


# Affiliate code for the main product.
$SiteWideCustomVariable["affiliate code"] = <<<SITEWIDE
abc123
SITEWIDE;


# Current ad.
$SiteWideCustomVariable["now_ad"] = <<<SITEWIDE
<div style="border:1px solid #333; border-radius:.5em; padding:.75em;">
<p>
The <a href="javascript:alert('not live')">Handsome Widget</a> is the 
most handsome widget we were able to find.
</p>
<p>(It really is a wonderful product.)</p>
</div>
SITEWIDE;

?>

The PHP data file can be named anything with a *.php file name extension. (In this article, we'll assume variables-file.php.) The file needs to be uploaded to your server. When you do that, make a note of its location.

Associating a Web Page With the Central Data File

Every web page where site-wide variables are likely to be used needs to be associated with the central data file.

Let's suppose the variables-file.php was uploaded into the /php-bin subdirectory. In that case, the location is /php-bin/variables-file.php.

This block of PHP code associates a web page with the central data file.

<?php
$PHPdataFileLocation = '/php-bin/variables-file.php';
include($_SERVER['DOCUMENT_ROOT'].$PHPdataFileLocation);
?>

Replace /php-bin/variables-file.php with the location of your central data file.

Put the block of PHP code into your web page source code either at the beginning of the <body …> tag or somewhere within the header (immediately after the <head> tag would work).

With the central data file on your server associated with each of your web pages, you are now ready to publish the values of site-wide variables.

The Snippet of Code to Publish a Site-wide Variable

This is a template for the snippet of PHP code:

<?php echo($SiteWideCustomVariable["unique-custom-variable-name"]) ?>

To publish a site-wide variable, use the above template and replace unique-custom-variable-name with the relevant variable name in the central data file. As an example, if you had this code in the central data file:

$SiteWideCustomVariable["eventphone"] = <<<SITEWIDE
123-555-4567
SITEWIDE; ?>

then you would publish the value by putting this snippet on your web page where the value should appear:

<?php echo($SiteWideCustomVariable["eventphone"]) ?>

The value of any site-wide variable may be published on any of your PHP web pages. Whenever you change the value in the central data file, the value will change accordingly on every page where it is published.

(This article first appeared in Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC