Danh mục

Giải pháp thiết kế web động với PHP - p 6

Số trang: 10      Loại file: pdf      Dung lượng: 669.02 KB      Lượt xem: 16      Lượt tải: 0    
tailieu_vip

Phí tải xuống: 5,000 VND Tải xuống file đầy đủ (10 trang) 0
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

HOW TO WRITE PHP SCRIPTS To save space, most examples in this book omit the PHP tags. You must always use them when writing your own scripts or embedding PHP into a web page. Embedding PHP in a web page PHP is an embedded language. This means that you can insert blocks of PHP code inside ordinary web pages. When somebody visits your site and requests a PHP page, the server sends it to the PHP engine, which reads the page from top to bottom looking for PHP tags. HTML passes through untouched, but whenever the PHP engine encounters a tag. If...
Nội dung trích xuất từ tài liệu:
Giải pháp thiết kế web động với PHP - p 6 HOW TO WRITE PHP SCRIPTS To save space, most examples in this book omit the PHP tags. You must always use them when writing your own scripts or embedding PHP into a web page. Embedding PHP in a web page PHP is an embedded language. This means that you can insert blocks of PHP code inside ordinary web pages. When somebody visits your site and requests a PHP page, the server sends it to the PHP engine, which reads the page from top to bottom looking for PHP tags. HTML passes through untouched, but whenever the PHP engine encounters a tag. If the PHP code produces any output, it s inserted at that point. You can have multiple PHP code blocks on a page, but they cannot be nested inside each other. Figure 3-1 shows a block of PHP code embedded in an ordinary web page and what it looks like in a browser and in a page source view after it has been passed through the PHP engine. The code calculates the current year, checks whether it s different from a fixed year (represented by $startYear in line 26 of the code on the left of the figure), and displays the appropriate year range in a copyright statement. As you can see from the page source view at the bottom right of the figure, there s no trace of PHP in what s sent to the browser. Figure 3-1. The PHP code remains on the server; only the output is sent to the browser. PHP doesn t always produce direct output for the browser. It may, for instance, check the contents of form input before sending an email message or inserting information into a database. So some code blocks are placed above or below the main HTML code, or in external files. Code that produces direct output, however, always goes where you want the output to be displayed. Storing PHP in an external file As well as embedding PHP in HTML, it s common practice to store frequently used code in separate files. When a file contains only PHP code, the opening tag is 31 CHAPTER 3 optional. In fact, the recommended practice is to leave out the closing PHP tag. However, you must use the closing ?> tag if the external file contains HTML after the PHP code. Using variables to represent changing values The code in Figure 3-1 probably looks like an awfully long-winded way to display a range of years. Surely it s much simpler to just type out the actual dates? Yes, it is, but the PHP solution saves you time in the long run. Instead of you needing to update the copyright statement every year, the PHP code does it automatically. You write the code once and forget it. What s more, as you ll see in the next chapter, if you store the code in an external file, any changes to the external file are reflected on every page of your site. This ability to display the year automatically relies on two key aspects of PHP: variables and functions. As the name suggests, functions do things; they perform preset tasks, such as getting the current date and converting it into human readable form. I ll cover functions a little later, so let s take variables first. The script in Figure 3-1 contains two variables: $startYear and $thisYear. A variable is simply a name that you give to something that may change or that you don t know in advance. Variables in PHP always begin with $ (a dollar sign). Download from Wow! eBook Although the concept of variables sounds abstract, we use variables all the time in everyday life. When you meet somebody for the first time, one of the first things you ask is “What s your name?” It doesn t matter whether the person you ve just met is Tom, Dick, or Harry, the word “name” remains constant. Similarly, with your bank account, money goes in and out all of the time (mostly out, it seems), but as Figure 3-2 shows, it doesn t matter whether you re scraping the bottom of the barrel or as rich as Croesus, the amount available is always referred to as the balance. Figure 3-2. The balance on your bank statement is an everyday example of a variable—the name stays the same, even though the value may change from day to day. So, “name” and “balance” are everyday variables. Just put a dollar sign in front of them, and you have two ready-made PHP variables, like this: $name $balance Simple. 32 HOW TO WRITE PHP SCRIPTS Naming variables You can choo ...

Tài liệu được xem nhiều: