Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
PHP Programming With MySQL Second Edition.doc
Скачиваний:
0
Добавлен:
01.05.2025
Размер:
43.07 Mб
Скачать

CHAPTER 1

Getting Started with PHP

2

PHP: Hypertext Preprocessor, or PHP, is an open-source, server-

side programming language. PHP is specifically designed to fill the

gap between static HTML pages and fully dynamic pages, such as

those generated through CGI code. PHP is embedded directly in the

XHTML source code; throughout the book you will apply the W3C

standard syntax and structure for XHTML documents and integrate

CSS to format the document for browser display. You will explore the

basic syntax and structure of the PHP scripting language and learn to

upload files to a remote server.

As you progress through the book, functional examples and com-

prehensive, hands-on learning activities will reinforce the concepts

presented and demonstrate how PHP and MySQL work together to

provide the Web developer with a set of tools that build content-rich

Web applications with database connectivity.

You will write your PHP scripts in a basic text editor. An editor

designed to work with XHTML, with features such as built-in syntax

highlighting and indentation, is helpful but not necessary. To run the

PHP script, you will need an FTP client to upload the PHP source

code files to a Web server and have access to a browser to view the

Web pages on the Internet. In this chapter, you will study the basics of

how to create PHP scripts.

Creating Basic PHP Scripts

JavaScript and PHP are both referred to as embedded languages

because code for both languages is embedded within a Web page

(either an HTML or XHTML document). You type this code directly

Into a Web page as a separate section. Although JavaScript code can

be added to standard Web page documents that have an extension

of .html, a Web page document containing PHP code must have an

extension of .php. Whenever a request is made for a document with

an extension of .php, the Web server sends the file to the scripting

engine for processing. The scripting engine then processes any PHP

code it encounters. Although PHP files use an extension of .php, they

can contain the same HTML or XHTML elements you would find in

a static Web page. The scripting engine ignores any non-PHP code

and only processes the PHP code it finds within PHP code blocks

(which you study next). The Web server then returns the results of the

PHP script and any HTML or XHTML elements found in the PHP file

to the client, where the file is rendered by the client’s Web browser. In

most cases, the results returned from a PHP script, such as database

records, are formatted with HTML or XHTML elements. This means

that PHP code is never sent to a client’s Web browser; only the result-

ing Web page that is generated from the PHP code and HTML or

Creating PHP Code Blocks

XHTML elements found within the PHP file are returned to the cli-

ent. Later in this chapter, you will see an example of a Web page that is

returned to a client from a PHP file that contains both PHP code and

XHTML elements. First, you need to learn about PHP code blocks.

Short Quiz

1.

2.

Define the term “embedded language” as it applies to PHP.

Why should you avoid using the .php extension if the

document contains only XHTML code?

Explain why you do not see any PHP code when you view the

source code of a PHP page in the browser.

It is possible

to create a

PHP file that

does not

need to con-

tain any PHP

code. However, if the file

contains no PHP code,

you should name the file

with an extension of .html

to avoid having the file

processed by the script-

ing engine unnecessarily.

You can use

any valid

extension

you want for

your PHP

scripts,

provided that your Web

server is configured to

process the extensions

you use with the scripting

engine. However, .php is

the default extension that

most Web servers use to

process PHP scripts. For

this reason, the files you

create with this book that

contain PHP code will

have an extension of .php.

3

3.

Creating PHP Code Blocks

You write PHP scripts within code declaration blocks, which are

separate sections on a Web page that are interpreted by the scripting

engine. You can include as many code declaration blocks as you want

within a document. This section discusses the following four types of

code declaration blocks you can use to write PHP:

• Standard PHP script delimiters

• The

<script> element

• Short PHP script delimiters

• ASP-style script delimiters

Standard PHP Script Delimiters

The standard method of writing PHP code declaration blocks is to

use the <?php and ?> script delimiters. A delimiter is a character

or sequence of characters used to mark the beginning and end of a

code segment. When the scripting engine encounters the <?php and

?> script delimiters, it processes any code between the delimiters as

PHP. The individual lines of code that make up a PHP script are called

statements. You need to use the following syntax in a document to

tell the Web server that the statements that follow must be inter-

preted by the scripting engine:

<?php

statements;

?>


CHAPTER 1

Getting Started with PHP

The following script contains a single statement that writes the text

“Explore Africa!” to a Web browser window using an

echo statement,

as you will study shortly:

<?php

echo "Explore Africa!";

?>

4

Notice that the preceding statement ends in a semicolon. PHP, along

with other programming languages, including C++ and Java, requires

you to end all statements with a semicolon. Note that the primary

purpose of a semicolon is to identify the end of a statement, not the

end of a line. Just as Web browsers ignore white space in an HTML or

XHTML document, the scripting engine ignores white space within

code blocks. For this reason, semicolons are critical to identify the

end of a statement. This also means that you do not need to place

each statement on its own line. For example, the following script con-

tains two echo statements on the same line, with each statement end-

ing in a semicolon:

<?php

echo "Explore "; echo "Africa!";

?>

Further, statements can be placed on the same line with the <?php

and ?> script delimiters, as follows:

<?php echo "Explore "; echo "Africa!"; ?>

Although the preceding syntax is legal, for better readability you

should typically use separate lines for the <?php and ?> script delimit-

ers and for each statement within a code block. However, many of the

examples in this book show delimiters and statements on the same

line to conserve space.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]