Basic PHP syntax
Let's take a look at the basics of creating a page with PHP. In the next section, you'll look at using an HTML form to submit information to PHP, but first you need to know how to do some of the basic tasks.
Start by opening your text editor and creating the most basic PHP page (see Listing 1).
Listing 1. Basic PHP page
<html> <title>Workflow Registration</title> <body> <p>You entered:</p> <p><?php echo "Some Data"; ?></p> </body> </html>
Overall, you have a simple HTML page with a single PHP section in bold. When the server encounters
symbol, it knows to evaluate the commands that follow, rather than simply send them out to
It keeps following instructions—which you'll see in a moment—until
the end of the section, as indicated by the
In this case, you have just one command,
echo, which tells the server to
output the indicated text.
That means that if you save the page and call it with your browser
(which you'll do in a moment), the browser receives the page shown in Listing 2.
Listing 2. The browser receives the PHP page
<html> <title>Workflow Registration</title> <body> <p>You entered:</p> <p>Some Data</p> </body> </html>
To see this in action, save the file as registration_action.php and move it to the document root of your server. For XAMPP, this directory is <XAMPP_HOME>/htdocs. (For Apache, this will likely be /var/www/html.)
Figure 1. Output from the echo command
Congratulations, you've written your first PHP page. Everything else you'll do in the language builds on this.
A variable is a placeholder for data. You can assign a value to it, and from then on, any time PHP encounters your variable, it will use the value instead. For example, change your page to Listing 3.
Listing 3. Using variables in your PHP page
<html> <title>Workflow Registration</title> <body> <p>You entered:</p> <?php $username = "nick"; $password = "mypassword"; echo "<p>Username = " . $username . "</p>"; echo "<p>Password = " . $password . "</p>"; ?> </body> </html>
First, notice that each line must end with a semicolon. Also, notice that you use a period (
concatenate text, or put it together. You can put together any number of strings, or chunks of text, this
One more note about variables: In PHP, variable names are case-sensitive, so
$UserName is a different
A consistent naming convention, such as deciding that all variables will be lowercase, can go a long way in preventing hard-to-catch errors.
Save the file (and upload it if necessary) and refresh your browser. You should see something similar to Figure 2.
Figure 2. Browser after refresh
Before moving on, let's look at a special kind of variable.
You can change the value of a variable as many times as you want, but sometimes you want to set up a variable with the expectation that the value will not change. These items are not called variables—they're constants. For example, you might want to define a constant that represents the title of each page as shown in Listing 4.
Listing 4. Defining a constant
<?php define("PAGE_TITLE", "Workflow Registration"); ?> <html> <title><?php echo PAGE_TITLE ?></title> <body> <p>You entered:</p> ...
(It may seem a little trivial now, but later you'll see how this definition can be used on multiple pages.)
Notice that you're defining the name of the constant and its value. If you try to change its value after it's been defined, you'll get an error.
Notice also that when you reference the constant, as in the
element, you don't use a dollar sign,
just the name of the constant. You can name a constant anything you like, but it's customary to use
all capital letters.
Later, when you learn about objects, you'll see a slightly more compact way of specifying a constant.
Up to now, you've used the
command to output information, but when you have just one piece of data
to output, this command can be a little cumbersome.
Fortunately, PHP provides a simpler way. By using the output operator
<?= ?> construct, you can specify information to output
as shown in Listing 5.
Listing 5. Using the output operator
<?php define("PAGE_TITLE", "Workflow Registration"); ?> <html> <title><?= PAGE_TITLE ?></title> <body> <p>You entered:</p> ...
Notice that when you use the output operator, you don't follow the information with a semicolon.
Later, you'll learn about other basic PHP constructs, such as if-then statements, because you'll need them in building the application.