PHP 5/MySQL Programming- P22 docx

5 225 0
PHP 5/MySQL Programming- P22 docx

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

Thông tin tài liệu

print verse(3); print chorus(); print verse(4); print chorus(); The program is to print the first verse, then the chorus, then the second verse, then the chorus, and so on. The details of how all these things are to be gener- ated is left to the individual functions. This is an example of encapsulation . Encapsulation is good, because it allows you to think about problems in multiple levels. At the highest level, you’re interested in the main ideas (print the verses and chorus) but you’re not so concerned about the exact details. You use the same technique when you talk about your day: “I drove to work, had some meet- ings, went to lunch, and taught a class.” You don’t usually describe each detail of each task. Each major task can be broken down into its component tasks later. (If somebody asks, you could really describe the meeting: “I got some coffee, appeared to be taking notes furiously on my PDA, got a new high score on Solitaire while appearing to take notes, scribbled on the agenda, and dozed off during a presentation.”) Returning a Value: The chorus() Function Another interesting thing about the code’s main section code is the use of the print() function. In the last program, I simply said chorus() and the program printed the verse. In this program, I did it a little differently. The chorus() func- tion doesn’t actually print anything to the screen. Instead, it creates the chorus as a big string and sends that value back to the program, which can do whatever it wants with it. This behavior isn’t new to you. Think about the rand() function. It always returns a value to the program. The functions in this program work the same way. Take another look at the chorus() function to see what I mean: function chorus(){ $output = <<<HERE with a knick-knack<br> paddy-whack<br> give a dog a bone<br> this old man came rolling home<br> <br><br> HERE; return $output; } // end chorus 83 C h a p t e r 3 C o n t r o l l i n g Y o u r C o d e w i t h C o n d i t i o n s a n d F u n c t i o n s I began the function by creating a new variable called $output. You can create variables inside functions by mentioning them, just like you can in the main part of the program. However, a variable created inside a function loses its meaning as soon as the function is finished. This is good, because it means the variables inside a function belong only to that function. You don’t have to worry about whether the variable already exists somewhere else in your program. You also don’t have to worry about all the various things that can go wrong if you mis- takenly modify an existing variable. I assigned a long string (the actual chorus of the song) to the $output variable with the <<<HERE construct. The last line of the function uses the return statement to send the value of $out- put back to the program. Any function can end with a return statement. What- ever value follows the keyword return is passed to the program. This is one way your functions can communicate to the main program. Accepting a Parameter in the verse() Function The most efficient part of the newer This Old Man program is the verse() func- tion. Rather than having a different function for each verse, I wrote one function that can work for all the verses. After careful analysis of the song, I noticed that each verse is remarkably similar to the others. The only thing that differentiates each verse is what the old man played (which is always the verse number) and where he played it (which is something rhyming with the verse number). If I can indicate which verse to play, it should be easy enough to produce the correct verse. Notice that when the main body calls the verse() function, it always indicates a verse number in parentheses. For example, it makes a reference to verse(1) and verse(3). These commands both call the verse function, but they send different values ( 1 and 3) to the function. Take another look at the code for the verse() function to see how the function responds to these inputs: function verse($stanza){ switch ($stanza){ case 1: $place = “thumb”; break; case 2: $place = “shoe”; break; case 3: $place = “knee”; break; 84 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r 85 C h a p t e r 3 C o n t r o l l i n g Y o u r C o d e w i t h C o n d i t i o n s a n d F u n c t i o n s case 4: $place = “door”; break; default: $place = “I don’t know where”; } // end switch $output = <<<HERE This old man, he played $stanza<br> He played knick-knack on my $place<br><br> HERE; return $output; } // end verse In this function, I indicated $stanza as a parameter in the function definition. A parameter is simply a variable associated with the function. If you create a function with a parameter, you are required to supply some sort of value whenever you call the function. The parameter variable automatically receives the value from the main body. For example, if the program says verse(1), the verse function is called and the $stanza variable contains the value 1. I then used a switch statement to populate the $place variable based on the value of $stanza. Finally, I created the $output variable using the $stanza and $place variables and returned the value of $output. You can create functions with multiple parameters. Simply declare several variables inside the parentheses of the function definition, and be sure to call the function with the appropriate number of arguments. Make sure to separate para- meters with commas. Managing Variable Scope You have learned some ways to have your main program share variable informa- tion with your functions. In addition to parameter passing, sometimes you want your functions to have access to variables created in the main program. This is especially true because all the variables automatically created by PHP (such as those coming from forms) are generated at the main level. You must tell PHP TRICK IN THE REAL WORLD If you’re an experienced programmer, you probably know other ways to make this code even more efficient. You return to this program as you learn about loops and arrays in the coming chapters. when you want a function to use a variable created at the main level. These program- level variables are also called global variables. If you’ve programmed in another language, you’re bound to get confused by the way PHP handles global variables. In most languages, any variable created at the main level is automatically available to every function. In PHP, you must explicitly request that a variable be global inside a function. If you don’t do this, a new local variable with the same name (and no value) is created at the function level. Looking at the Scope Demo To illustrate the notion of global variables, take a look at the Scope Demo, shown in Figure 3.16. Take a look at the code for the Scope Demo and see how it works: <html> <head> <title>Scope Demo</title> </head> <body> <h1>Scope Demo</h1> <h3>Demonstrates variable scope</h3> TRAP 86 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r FIGURE 3.16 Variable $a keeps its value inside a function, but $b does not. 87 C h a p t e r 3 C o n t r o l l i n g Y o u r C o d e w i t h C o n d i t i o n s a n d F u n c t i o n s <? $a = “I have a value”; $b = “I have a value”; print <<<HERE outside the function, <br> \$a is “$a”, and<br> \$b is “$b”<br><br> HERE; myFunction(); function myFunction(){ //make $a global, but not $b global $a; print <<<HERE inside the function, <br> \$a is “$a”, and<br> \$b is “$b”<br><br> HERE; } // end myFunction ?> </body> </html> I created two variables for this demonstration: $a and $b. I gave them both the value I have a value. As a test, I printed out the values for both $a and $b. Notice the trick I used to make the actual dollar sign show up in the quotation marks. When PHP sees a dollar sign inside quotation marks, it usually expects to be working with a variable. Sometimes (as in this case) you really want to print a dollar sign. You can precede a dollar sign with a backslash to have the sign appear. So, print $a prints the value of the variable $a, but print \$a prints the value “$a”. TRICK . true because all the variables automatically created by PHP (such as those coming from forms) are generated at the main level. You must tell PHP TRICK IN THE REAL WORLD If you’re an experienced. to get confused by the way PHP handles global variables. In most languages, any variable created at the main level is automatically available to every function. In PHP, you must explicitly request. $b. Notice the trick I used to make the actual dollar sign show up in the quotation marks. When PHP sees a dollar sign inside quotation marks, it usually expects to be working with a variable.

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

Mục lục

  • PHP 5 / MySQL Programming for the Absolute Beginner

    • Cover

    • Contents

    • Introduction

    • Chapter 1: Exploring the PHP Environment

    • Chapter 2: Using Variables and Input

    • Chapter 3: Controlling Your Code with Conditions and Functions

    • Chapter 4: Loops and Arrays

    • Chapter 5: Better Arrays and String Handling

    • Chapter 6: Working with Files

    • Chapter 7: Writing Programs with Objects

    • Chapter 8: XML and Content Management Systems

    • Chapter 9: Using MySQL to Create Databases

    • Chapter 10: Connecting to Databases within PHP

    • Chapter 11: Data Normalization

    • Chapter 12: Building a Three-Tiered Data Application

    • Index

    • Team DDU

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan