Danh mục

PHP: The Good Parts: Delivering the Best of PHP- P4

Số trang: 20      Loại file: pdf      Dung lượng: 312.63 KB      Lượt xem: 15      Lượt tải: 0    
Thu Hiền

Hỗ trợ phí lưu trữ khi tải xuống: 6,000 VND Tải xuống file đầy đủ (20 trang) 0

Báo xấu

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

PHP: The Good Parts: Delivering the Best of PHP- P4: Vượt qua tất cả các hype về PHP và thâm nhập vào sức mạnh thực sự của ngôn ngữ này. Cuốn sách này khám phá những tính năng hữu ích nhất của PHP và làm thế nào họ có thể đẩy nhanh quá trình phát triển web, và giải thích lý do tại sao thường được sử dụng PHP yếu tố thường được sử dụng sai hoặc misapplied. Bạn sẽ tìm hiểu thêm phần nào sức mạnh để lập trình hướng đối tượng, và làm thế nào để sử dụng...
Nội dung trích xuất từ tài liệu:
PHP: The Good Parts: Delivering the Best of PHP- P4The second line of output, the single use of MD5, does not change on a page refresh,while the other content does. There is more detailed discussion on the MD5 function (and its more secure cousin sha1) in Chapter 9 on security.PHP provides many more string functions, and over time you may choose to becomefamiliar with many of them. The string functions we have covered here are those thatyou are likely to find the most beneficial right away. In the next chapter, we will followa similar pattern with a discussion of arrays. String Functions (Best of) | 43 CHAPTER 5 ArraysNow that we have a handle on the concept of strings, let’s take a look at the power andflexibility of arrays. Arrays are known as compound data types; all that really means isthat they are more complex in structure than simple strings and integers, which are alsoknown as scalar data types. Imagine an array as an egg carton. It carries 12 individualcompartments that can house one egg each, yet it travels around as one entity. Thecompartments can be broken off and made into single egg holders, or holders in anynumber combination. Additionally, these compartments are not limited to holding onlyeggs; they can hold rocks, or cookies, or match sticks. Of course, an analogy like thishas its limitations—egg cartons cannot easily be expanded and they cannot hold otheregg cartons, all of which, we will see, arrays are excellent at doing.Let’s talk more precisely about arrays. Like egg cartons, arrays have compartments(elements) that hold data. The elements and their respective data always travel together(although you can have an empty element without data) and are known as key/valuepairs. So, if we had an array of five elements, each containing a number in the rangefrom 1 to 5, it would look like Table 5-1 (elements start their counting with 0).Table 5-1. Visual representation of an array with numeric (indexed) keys Keys 0 1 2 3 4 Values 1 2 3 4 5Indexed ArraysArrays with numerical keys are known as indexed arrays. The keys can also be namedwith strings, if you prefer, creating what is known as an associative array, as you willsee later in this chapter. Let’s consider an array called $myArray. Array variable names follow the same naming rules as regular PHP var- iables (see the section “Variables: Data Types, Loose Typing, and Scope” on page 9). 45In PHP code, you can reference the elements of an array by their keys, surrounded bysquare brackets. If we want to take the value of the third element of the array—thecontents being the number 3 in this case—and assign it to its own variable, the codewould look like this: // remember, the elemets of the array start with 0 $singleValue = $myArray[2] ; echo $singleValue ; // output would be 3Now, this assumes that we have already defined the array somewhere else in our code.There are two ways in which to create arrays; the first is a variation on the use of thesquare bracket syntax. Here is the code for creating an array with this method: $myArray[0] = 1 ; $myArray[1] = 2 ; $myArray[2] = 3 ; $myArray[3] = 4 ; $myArray[] = 5 ;This method assumes that we already know the key order and their values, as the keynumbers here are hardcoded. Notice that the last line of code in the above example isnot “forcing” the key number inside the square brackets; when this is done (not pro-viding the key number), the result is that the next available integer key will be assignedto that element for us.Creating an array in this fashion may or may not be what you want. The other methodavailable is to use the array function with the key/value pairs passed together andseparated by commas, like so: $myArray = array(0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5) ;This way of creating an array is much more condensed, yet it may be a little moredifficult for humans to read.If you want to create an empty array, just use the array function without any parameters: $a = array() The keys of any array have to be named uniquely, otherwise there would be confusion when referencing their contents. PHP won’t prevent you from using the same key multiple times in an assignment, but each identical key assigned will replace the one before it.Associative ArraysSo far we have looked at indexed arrays, but as I mentioned before, the key portion ofan array can also consist of character strings. The data values that we have looked atso far have also been numerical, but these too can be changed to string data, as shownin Table 5-2.46 | Chapter 5: ArraysTable 5-2. Visual representation of an array with named (associative) keys Keys first second fname initial lname Values 1 2 Peter B MacIntyreTo create the array represented in Table 5-2, use the same code syntax options as before,but with appropriate alterations for the strings: $myArray[first] = 1 ; $myArray[second] = 2 ; $myArray[fname] = Peter ; $myArray[initial] = B ; $myArray[lname] = MacIntyre ;Alternately, you can use the following: $myArray = array(first => 1, second => 2, fname => Peter, initial => B, lname => MacIntyre) ;There is no real difference here between the use of single or double quotes, and as youcan see, they are interchangeable. Just be careful with them w ...

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