Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Learning PHP, Part 1: Register for an account, upload files for approval, and view and download approved files

Nicholas Chase, Founder, NoTooMi
Nicholas Chase is the founder and creator of NoTooMi. In addition to technical writing for large corporations, he has been involved in website development for companies such as Lucent Technologies, Sun Microsystems, Oracle, and the Tampa Bay Buccaneers. He has been a high school physics teacher, a low-level-radioactive waste facility manager, an online science fiction magazine editor, a multimedia engineer, an Oracle instructor, and the chief technology officer of an interactive communications company. He is the author of several books, including XML Primer Plus (Sams 2002).

Summary:  This tutorial is Part 1 of a three-part "Learning PHP" series teaching you how to use PHP through building a simple workflow application. This tutorial walks you through creating a basic PHP page using HTML forms and covers accessing databases.

23 Oct 2012 - Nicholas Chase updated content throughout this tutorial to reflect current PHP technology.

View more content in this series

Date:  23 Oct 2012 (Published 14 Jun 2005)
Level:  Intermediate PDF:  A4 and Letter (948 KB | 38 pages)Get Adobe® Reader®

Activity:  85082 views
Comments:  

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.

A basic PHP page

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 the <?php symbol, it knows to evaluate the commands that follow, rather than simply send them out to the browser. It keeps following instructions—which you'll see in a moment—until the end of the section, as indicated by the ?> symbol.

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.)

To call this page, open your browser and point it to http://localhost/registration_action.php. You should see something similar to Figure 1.


Figure 1. Output from the echo command
Output from echo command

Congratulations, you've written your first PHP page. Everything else you'll do in the language builds on this.


Variables

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 (.) to concatenate text, or put it together. You can put together any number of strings, or chunks of text, this way.

One more note about variables: In PHP, variable names are case-sensitive, so $UserName is a different variable from $username.

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
Browser refresh

Before moving on, let's look at a special kind of variable.


Constants

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 title 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.


Easier output

Up to now, you've used the echo 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.

2 of 10 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Open source, Linux
ArticleID=133648
TutorialTitle=Learning PHP, Part 1: Register for an account, upload files for approval, and view and download approved files
publish-date=10232012
author1-email=ibmquestions@nicholaschase.com
author1-email-cc=