An Introduction to PHP for WordPress Users
PHP has become one of the most popular programming languages in the world. It is fairly simple to learn, and the free pricetag means that many new users have the chance to learn without too much risk. One of the more common introductions to PHP is modifying WordPress blogs. Though most blogs can be installed without programming, eventually a blog owner wants to customize their blog to set it apart from the million other blogs on the Internet. This quick tutorial will show you some of the basic PHP structures to help you get started.
PHP vs HTML
First off, what is PHP? Some people have the mistaken impression that PHP is just like HTML, but they are two totally different technologies. HTML is basically a highly modified markup language, which means that it’s just bold and italic tags at heart. Of course, HTML has been greatly expanded over the last decade, so it’s capable of things far beyond just structuring text. However, the major flaw with HTML is that it can only produce static code. In other words, you must make all changes manually. For example, let’s say you wanted to have a greeting for your clients on your website. If you use HTML, all the users will see the same greeting. But what if you want to have a customized greeting for each of your customers?
This is where PHP comes in. PHP is a programming language specifically designed for use on web servers. When a person visits a webpage that only uses HTML, they can view the actual code that their browser is using to display the page. For instance a simple HTML line of code may look like this: This is in bold. When a person views a webpage, that line is displayed in bold in the browser. Most browsers will allow a user to view the entire code for any page they happen to visit. A webpage designer must create a separate HTML file for every different page on their website, and the links on webpages simply tell the browser which HTML page to display next.
PHP works quite a bit differently. Rather than a simple page that is viewed by the browser, the code that is used is generated on the fly whenever a viewer visits a PHP page. This means that each time a viewer goes to a website a new page is created right on the spot for their particular visit. Instead of just reading the HTML code, a PHP page actually sends a list of instructions to the webserver. The webserver then generates all the HTML code and sends that back to the browser. This means that a visitor never sees the PHP code. They only see the HTML code that is returned. For example, to display the same code that we used above, a PHP would actually read
IF – ELSE STATEMENTS
So far, it would appear that PHP just does what HTML does. However, IF-ELSE statements, also known as conditional statements, show the real power of PHP.
An IF – ELSE statement is a piece of code that changes the actions of a program if certain conditions are met. For example, a movie site may display the titles of R rated movies ONLY IF the user is old enough. Here’s how it works.
if (IF statement) {
stuff to do when if statement is true…
}else{
stuff to do when IF statement is false…
}
?>
So, if we want to show age-appropriate movie titles based on whether a user is an adult or child, it might look like this:
$KidMovies = “Muppets, Anne of Green Gables, Toy Story”;
$AllMovies = $Kidmovies . ”, Rambo, Braveheart”;
$UserAge = “adult”;
if ($UserAge == “adult”) {
echo $AllMovies;
}else{
echo $KidMovies;
}
?>
This code would display “Muppets, Anne of Green Gables, Toy Story, Rambo, Braveheart”.
Note that to check if an IF statement is true, you use two equal signs, not one. If you use a statement like this:
if ($UserAge = “adult”)
PHP only checks to see if the string “adult” can be assigned to the variable $UserAge, which is always true, so the page would not react the way you desire.
WORDPRESS
Though it appears far more complicated, a large portion of the code you see on a WordPress PHP page will be IF statements that are nestled within each other. You can add your own customized code in almost any place you wish. For example, what if you wanted to use a different database for different languages? In the file called “wp-config.php”, you will find a line of code that says:
define(‘DB_NAME’, ‘database_name_here’);
You could actually replace this with an IF statement to change the database.
If ($language == “Spanish”) {
define(‘DB_NAME’, ‘SpanishDB’);
} else {
define(‘DB_NAME’, ‘EnglishDB’);
}
Thus, if the variable $language was stored as “Spanish”, WordPress would use the SpanishDB database. Otherwise, it would use the EnglishDB database. You can do the same sort of modification for almost any code in WordPress. However, please note that WordPress is fairly complicated piece of coding for a beginner, and modifications might have unintended consequences. If possible, make sure you test your changes before publishing them to your blog.






