
When creating the content for a website you probably notice that some parts of the text (such as your company name, or page title) are used many times in many pages. Imagine if you wanted to change one of these names, or add a little ® at the end of your company name at each occurance, you would have to search your entire content and replace them. You might miss one or two, and suffer inconsistancies!
With PHP you can easily create text variables that show everywhere in your text. Then if you wanted to change something, you only change it in one place in a single file, and your entire website is updated!
Simply create a text.php (or english.php if you want multiple languages! yes! also helpful!) file, and start defining texts. For example:
<?PHP
define('COMPANY_NAME', 'Ralph van den Berg Co.,Ltd.');
define('URL', 'http://ralphvandenberg.com');
?>
In all the files that you want to use these variables you just need to include the text variables file: include('text.php');
Then when you go about writing your text you just stick these variables in wherever you want. Later on, any changes to the text.php file will change each occurance of these variables and save you a lot of time, and keep good consitency throughout your website.
Example: "Welcome to <?PHP echo COMPANY_NAME; ?>"
Will show: "Welcome to Ralph van den Berg Co.,Ltd."
Example: "<a href="<?PHP echo URL; ?>">Visit My Site</a>"
Will show: "Visit My Site"
You can include the text.php page at the very top, even before your opening html tags so that you can use these text variables in the title like this:
<?PHP include('text.php'); ?>
<head>
<title>
<?PHP echo COMPANY_NAME; ?>
</title>
</head>
You don't need to be a PHP expert for this. Try it yourself!
(Note: you do need a server that supports PHP, and your files do need the .php extention instead of the .htm or .html)
You can actually go so far as to put all your text into a text.php file and include the bits you want all over your website, but I only recommend that for those who wish to create multiple language versions of their website. If you only make variables for the common words or phrases that might be subject to change in the future, you can keep your site organized and consistent.
Comments
gettext and a nice abbr
some additions to kens article... for bigger projects take a look at "gettext" library, which provides a very good functionality to implement multiple languages.
http://www.gnu.org/software/gettext/manual/gettext.html
http://th.php.net/gettext
By the way... if you want to print out textvariables or in general php variables, there is a abbreviation, which is useful, plus it looks kind of fancy ;-)
instead of <?PHP echo COMPANY_NAME; ?>
you can simply use
<?=COMPANY_NAME?>
try it out!
Being specific
Instead of putting everything in text.php you could also be more specific, by putting names in names.php, and links in links.php so that you don't have to load a mega large file of text for every page if a given page only uses a small portion of the variables.
---Ralph van den Berg
visit RalphvandenBerg.com