Build Your Own Database Driven Website Using PHP & MySQL: Pt. 3

In Chapter 2, we learned how to use the MySQL database engine to store a list
of jokes in a simple database (composed of a single table named
“Jokes”). To do so, we used the MySQL command-line client to
enter SQL commands (queries). In this chapter, we’ll introduce
the PHP server-side
scripting language. In addition to the basic features we’ll
explore here, this language has full support for communication
with MySQL databases.

As we’ve discussed previously, PHP is a server-side scripting
language. This concept is not obvious, especially if you’re used
to designing pages with just HTML
and JavaScript. A server-side
scripting language is similar to JavaScript in many ways, as they
both allow you to embed little programs (scripts) into the HTML
of a Web page. When executed, such scripts allow you to control
what will actually appear in the browser window with more
flexibility than is possible using straight HTML.

The key difference between JavaScript and PHP is simple.
JavaScript is interpreted by the Web browser once the Web page
that contains the script has been downloaded. Meanwhile, server-
side scripting languages like PHP are interpreted by the Web
server before the page is even sent to the browser. And, once
it’s interpreted, the results of the script replace the PHP code
in the Web page itself, so all the browser sees is a standard
HTML file. The script is processed entirely by the server, hence
the designation: server-side scripting language.

Let’s look back at the today.php example presented
in Chapter 1:

<html>
<head>
<title>Today's Date</title>
</head>
<body>
<p>Today's Date (according to this Web server) is
<?php
  echo( date("l, F dS Y.") );
?></p>
</body>
</html>

Most of this is plain HTML. The line between
<?php and ?>, however, is written
in PHP. <?php means “begin PHP code”, and
?> means “end PHP code”. The Web server is asked
to interpret everything between these two delimiters, and to
convert it to regular HTML code before it sends the Web page to
the requesting browser. The browser is presented with something
like this:

<html>
<head>
<title>Today's Date</title>
</head>
<body>
<p>Today's Date (according to this Web server) is
Wednesday, May 30th 2001.</p>
</body>
</html>

Notice that all signs of the PHP code have disappeared. In their
place, the output of the script has appeared, and looks just like
standard HTML. This example demonstrates several advantages of
server-side scripting:

  • No browser compatibility issues. PHP scripts are
    interpreted by the Web server and nothing else, so you don’t have
    to worry about whether the language you’re using will be
    supported by your visitors’ browsers.
  • Access to server-side resources. In the above example,
    we placed the date according to the Web server into the Web page.
    If we had inserted the date using JavaScript, we would only be
    able to display the date according to the computer on which the
    Web browser was running. Now, while this isn’t an especially
    impressive example of the exploitation of server-side resources,
    we could just as easily have inserted some other information that
    would only be available to a script running on the Web server,
    for example, information stored in a MySQL database that runs on
    the Web server computer.

  • Reduced load on the client. JavaScript can
    significantly slow down the display of a Web page on slower
    computers, as the browser must run the script before it can
    display the Web page. With server-side scripting, this becomes
    the burden of the Web server machine.

Basic Syntax and Commands

PHP syntax will be very familiar to anyone with an understanding
of C, C++, Java, JavaScript,
Perl, or any other C-
derived language. A PHP script consists of a series of commands,
or statements, each of which is an instruction that the
Web server must follow before it can proceed to the next. PHP
statements, like those in the above-mentioned languages, are
always terminated by a semicolon (;).

This is a typical PHP statement:

echo( "This is a <b>test</b>!" );

This statement invokes a built-in function called
echo and passes it a string of text: This is a
<b>test</b>!
Built-in functions can be thought
of as things that PHP knows how to do without us having to spell
out the details. PHP has a lot of built-in functions that let us
do everything from sending email, to working with information
that’s stored in various types of databases. The
echo function, however, simply takes the text that
it’s given, and places it into the HTML code of the page at the
current location. Consider the following (echo.php
in the code archive):

<html>
<head>
<title> Simple PHP Example </title>
</head>
<body>
<p><?php echo("This is a <b>test</b>!"); ?></p>
</body>
</html>

If you paste this code into a file called echo.php
(or echo.php3, if your Web host has not configured
.php files to be recognized as PHP scripts) and
place it on your Web server, a browser that views the page will
see this:

<html>
<head>
<title> Simple PHP Example </title>
</head>
<body>
<p>This is a <b>test</b>!</p>
</body>
</html>

Notice that the string of text contained HTML tags
(<b> and </b>), which is
perfectly acceptable.

You may wonder why we need to surround the string of text with
both parentheses and quotes. Quotes are used to mark the
beginning and end of strings of text in PHP, so their presence is
fully justified. The parentheses serve a dual purpose. First,
they indicate that echo is a function that you want to call.
Second, they mark the beginning and end of a list of “parameters”
that you wish to provide, in order to tell the function what to
do. In the case of the echo function, you only need to provide
the string of text that you want to appear on the page. Later on,
we’ll look at functions that take more than one parameter (and
we’ll separate those parameters with commas), and we’ll consider
functions that take no parameters at all (for which we’ll still
need the parentheses, though we won’t type anything between
them).

Latest Articles