- As a server-side language, PHP only actually works when running on an active server. To create an environment that runs PHP correctly in order to use it yourself, you'll need to install a private home server. The open-source package XAMPP provides all the components you need to do so (one of the P's in there stands for PHP).
A PHP file is essentially an HTML file with snippets of PHP include. You can write HTML code as normal, but whenever you write any PHP code, begin with "<?php" and end with "?>". You can also plant HTML code inside PHP code using the "print" or "echo" commands. Here's a sample PHP document:
<html>
<head>
<title>My First PHP Program</title>
</head>
<body>
<?php
print "<h1>Hello, World!</h1>";
?>
</body>
</html>
Note that "print" doesn't mean "print to the computer screen" -- it means "print to the HTML document." The web browser, in turn, will interpret the HTML document (in this example, converting "<h1>Hello, World!</h1>" into a heading). Also note that each line in PHP ends with a semicolon; if you don't include one at the end of each line, the program won't work. - Variables are a way to store data. Variables start with a "$" sign and can be named whatever you wish. The following declares a variable and then prints it out again.
$language = "PHP";
print "My favorite server-side language is $language";
Of course, you wouldn't need to create the $language variable in this case; you could just write "My favorite server-side language is PHP." But what if you had a form on your website that asked for a user's favorite server-side language? You could store their answer in the $language variable for use later. It's that kind of situation where variables can really come into play.
Arrays are essentially variables that can store multiple values. Here's how you declare an array:
$bookshelf = array("Macbeth", "Hamlet", "Julius Caesar");
Each value in the array is then automatically stored in an array "key":
$bookshelf[0] = "Macbeth";
$bookshelf[1] = "Hamlet";
$bookshelf[2] = "Julius Caesar";
There are many different types of arrays, such as "associative arrays," which can have non-numerical keys:
$info = array();
$info["name"] = "Bill Gates";
$info["company"] = "Microsoft"; - If/else conditional statements allow your code to make decisions. For example, let's say you have an option in a member directory for whether a member's email address will be shown or not. You've stored the user's answer to the question in the $displayEmail variable:
if ($displayEmail == "yes"){
print "johndoe@example.com";
} else {
print "Email hidden by request.";
}
Note that the double equal signs check for equality, because a single equal sign would mean you're assigning a value to a variable.
Loops, on the other hand, repeat an action until a certain condition is met. PHP contains a few different loop functions, such as the "while" loop:
$number = 1;
while($number < 11){
print $number;
$number++;
}
Once PHP gets to the closing bracket, it jumps back up to the opening bracket again until "$number < 11" isn't true--so that snippet of code would print "12345678910". The line "$number++;" is shorthand for saying "$number = $number + 1;" and is an easy way to auto-increment your variables. - Obviously, this only scratches the surface of PHP's possibilities. While all the rules and syntax can be daunting at first, the best way to learn the language is to look at a lot of code and write a lot of code. With that in mind, work through the PHP tutorials at instructional sites such as W3 Schools. Also check out MySQL, a database system that can work closely with PHP to create dynamic sites and comes packaged with XAMPP. For examples of PHP and MySQL at work, examine how website management programs such as Joomla create websites by using MySQL databases and PHP template files.
Getting Started with PHP
Variables and Arrays
If/Else and Loops
Further Training
SHARE