Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Tài liệu PHP & MySQL for Dummies- P4 pdf
Nội dung xem thử
Mô tả chi tiết
Part III
PHP
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
In this part . . . In Part III, you find out how to use PHP for your Web
database application. Here are some of the topics
described:
U Adding PHP to HTML files
U PHP features that are useful for building a
dynamic Web database application
U Using PHP features
U Using forms to collect information from users
U Showing information from a database in a Web
page
U Storing data in a database
U Moving information from one Web page to the
next
You find out everything you need to know to write PHP
programs.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Chapter 6
General PHP
In This Chapter
▶ Adding PHP sections to HTML files
▶ Writing PHP statements
▶ Using PHP variables
▶ Comparing values in PHP variables
▶ Documenting your programs
Programs are the application part of your Web database application.
Programs perform the tasks: Programs create and display Web pages,
accept and process information from users, store information in the database, get information out of the database, and perform any other necessary
tasks.
PHP, the language that you use to write your programs, is a scripting language designed for use on the Web. It has features to aid you in programming
the tasks needed by dynamic Web applications.
In this chapter, I describe the general rules for writing PHP programs — the
rules that apply to all PHP statements. Consider these rules similar to general
grammar and punctuation rules. In the remaining chapters in Part III, you find
out about specific PHP statements and features and how to write PHP programs to perform specific tasks.
Adding a PHP Section to an HTML Page
PHP is a partner to HTML, enabling HTML to do things it can’t do on its own.
For example, HTML can display Web pages, and HTML has features that allow
you to format those Web pages. HTML also allows you to display graphics in
your Web pages and to play music files. But HTML alone does not allow you
to interact with the person viewing the Web page.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
134 Part III: PHP
HTML is almost interactive. That is, HTML forms allow users to type information that the Web page is designed to collect; however, you can’t access that
information without using a language other than HTML. PHP processes form
information and allows other interactive tasks as well.
HTML tags are used to make PHP language statements part of HTML scripts.
The file is named with a .php extension. (The PHP administrator can define
other extensions, such as .phtml or .php5, but .php is the most common.
In this book, I assume .php is the extension for PHP programs.) The PHP language statements are enclosed in PHP tags with the following form:
<?php ?>
Sometimes you can use a shorter version of the PHP tags. You can try using
<? and ?> without the php. If short tags are enabled, you can save a little
typing. However, if you use short tags, your programs will not run if they’re
moved to another Web host where PHP short tags are not activated.
PHP processes all statements between the two PHP tags. After the PHP section is processed, it’s discarded. Or if the PHP statements produce output,
the PHP section is replaced by the output. The browser doesn’t see the PHP
section — the browser sees only its output, if there is any. For more on this
process, see the sidebar, “How the Web server processes PHP files.”
As an example, I’ll start with an HTML program that displays Hello World!
in the browser window, shown in Listing 6-1. (It’s a tradition that the first program you write in any language is the Hello World program. You might have
written a Hello World program when you first learned HTML.)
Listing 6-1: The Hello World HTML Program
<html>
<head><title>Hello World Program</title></head>
<body>
<p>Hello World!</p>
</body>
</html>
If you point your browser at this HTML program, you see a Web page that
displays
Hello World!
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Chapter 6: General PHP 135
Listing 6-2 shows a PHP program that does the same thing — it displays
Hello World! in a browser window.
Listing 6-2: The Hello World PHP Program
<html>
<head><title>Hello World Program</title></head>
<body>
<?php
echo “<p>Hello World!</p>”
?>
</body>
</html>
If you point your browser at this program, it displays the same Web page as
the HTML program in Listing 6-1.
Don’t look at the file directly with your browser. That is, don’t choose
File➪Open➪Browse from your browser menu to navigate to the file and click
it. You must open the file by typing its URL, as I discuss in Chapter 2. If you
see the PHP code displayed in the browser window instead of the output that
you expect, you might not have typed the URL.
How the Web server processes PHP files
When a browser is pointed to a regular HTML
file with an .html or .htm extension, the
Web server sends the file, as-is, to the browser.
The browser processes the file and displays
the Web page described by the HTML tags
in the file. When a browser is pointed to a PHP
file (with a .php extension), the Web server
looks for PHP sections in the file and processes
them instead of just sending them as-is to the
browser. The Web server processes the PHP
file as follows:
1. The Web server starts scanning the file in
HTML mode. It assumes the statements are
HTML and sends them to the browser without any processing.
2. The Web server continues in HTML mode
until it encounters a PHP opening tag
(<?php).
3. When it encounters a PHP opening tag, the
Web server switches to PHP mode. This is
sometimes called escaping from HTML. The
Web server then assumes that all statements are PHP statements and executes
the PHP statements. If there is output, the
output is sent by the server to the browser.
4. The Web server continues in PHP mode
until it encounters a PHP closing tag (?>).
5. When the Web server encounters a PHP
closing tag, it returns to HTML mode. It
resumes scanning, and the cycle continues
from Step 1.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.