1. Trang chủ
  2. » Công Nghệ Thông Tin

Professional Information Technology-Programming Book part 61 docx

5 254 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 20,54 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

If you entered Listing 1.1 exactly as shown, you might notice that the actual output produced could be formatted a little betterthere is no space between the time and... Any line in a sc

Trang 1

Your First Script

Before you go any further, you need to make sure you can create and run PHP scripts as you go through the examples in this book This could be on your own machine, and you can find instructions for installing PHP in Appendix A,

"Installing PHP." Also, many web hosting companies include PHP in their

packages, and you may already have access to a suitable piece of web space

Go ahead and create a new file called time.php that contains Listing 1.1, in a location that can be accessed by a PHP-enabled web server This is a slight

variation on the date example shown previously

Listing 1.1 Displaying the System Date and Time

The time is

<?php echo date('H:i:s');?>

and the date is

<?php echo date('j F Y');?>

When you enter the URL to this file in your web browser, you should see the

current date and time, according to the system clock on your web server, displayed

Running PHP Locally If you are running PHP from your local PC,

PHP code in a script will be executed only if it is accessed through

a web server that has the PHP module enabled If you open a local

script directly in the web browserfor instance, by double-clicking

or dragging and dropping the file into the browserit will be treated

as HTML only

Web Document Location If you were using a default Apache

installation in Windows, you would create time.php in the

folder C:\Program Files\Apache

Group\Apache\htdocs, and the correct URL would be

http://localhost/time.php

If you entered Listing 1.1 exactly as shown, you might notice that the actual output produced could be formatted a little betterthere is no space between the time and

Trang 2

the word and Any line in a script that only contains code inside PHP tags will not take up a line of output in the generated HTML

If you use the View Source option in your web browser, you can see the exact output produced by your script, which should look similar to the following:

The time is

15:33:09and the date is

13 October 2004

If you insert a space character after ?>, that line now contains non-PHP elements, and the output is spaced correctly

The echo Command

While PHP is great for embedding small, dynamic elements inside a web page, in fact the whole page could consist of a set of PHP instructions to generate the output if the entire script were enclosed in PHP tags

The echo command is used to send output to the browser Listing 1.1 uses echo

to display the result of the date command, which returns a string that contains a formatted version of the current date Listing 1.2 does the same thing but uses a series of echo commands in a single block of PHP code to display the date and time

Listing 1.2 Using echo to Send Output to the Browser

<?php

echo "The time is ";

echo date('H:i:s');

echo " and the date is ";

echo date('j F Y');

?>

The non-dynamic text elements you want to output are contained in quotation marks Either double quotes (as used in Listing 1.2) or single quotes (the same character used for an apostrophe) can be used to enclose text strings, although you will see an important difference between the two styles in Lesson 2, "Variables." The following statements are equally valid:

Trang 3

echo "The time is ";

echo 'The time is ';

Notice that space characters are used in these statements inside the quotation marks

to ensure that the output from date is spaced away from the surrounding text In fact the output from Listing 1.2 is slightly different from that for Listing 1.1, but in

a web browser you will need to use View Source to see the difference The raw output from Listing 1.2 is as follows:

The time is 15:59:50 and the date is 13 October 2004

There are no line breaks in the page source produced this time In a web browser, the output looks just the same as for Listing 1.1 because in HTML all whitespace, including carriage returns and multiple space or tab characters, is displayed as a single space in a rendered web page

A newline character inside a PHP code block does not form part of the output Line breaks can be used to format the code in a readable way, but several short

commands could appear on the same line of code, or a long command could span several linesthat's why you use the semicolon to indicate the end of a command Listing 1.3 is identical to Listing 1.2 except that the formatting makes this script almost unreadable

Listing 1.3 A Badly Formatted Script That Displays the Date and Time

<?php echo "The time is "; echo date('H:i:s'); echo

" and the date is "

; echo date(

'j F Y'

);

?>

Using Newlines If you wanted to send an explicit newline

character to the web browser, you could use the character

sequence \n There are several character sequences like this that

have special meanings, and you will see more of them in Lesson

6, "Working with Strings."

Trang 4

Comments

Another way to make sure your code remains readable is by adding comments to it

A comment is a piece of free text that can appear anywhere in a script and is

completely ignored by PHP The different comment styles supported by PHP are shown in Table 1.1

Table 1.1 Comment Styles in PHP

Comment Description

// or # Single-line comment Everything to the end of the current line is

ignored

/*

*/

Single- or multiple-line comment Everything between /* and */

is ignored

Listing 1.4 produces the same formatted date and time as Listings 1.1, 1.2, and 1.3, but it contains an abundance of comments Because the comments are just ignored

by PHP, the output produced consists of only the date and time

Listing 1.4 Using Comments in a Script

<?php

/* time.php

This script prints the current date

and time in the web browser

*/

echo "The time is ";

echo date('H:i:s'); // Hours, minutes, seconds

echo " and the date is ";

echo date('j F Y'); // Day name, month name, year

?>

Listing 1.4 includes a header comment block that contains the filename and a brief

Trang 5

description, as well as inline comments that show what each date command will produce

Summary

In this lesson you have learned how PHP works in a web environment, and you have seen what a simple PHP script looks like In the next lesson you will learn how to use variables

Lesson 2 Variables

In this lesson you will learn how to assign values to variables in PHP and use them

in some simple expressions

Ngày đăng: 07/07/2014, 03:20

TỪ KHÓA LIÊN QUAN