![Phân tích tư tưởng của nhân dân qua đoạn thơ: Những người vợ nhớ chồng… Những cuộc đời đã hóa sông núi ta trong Đất nước của Nguyễn Khoa Điềm](https://timtailieu.net/upload/document/136415/phan-tich-tu-tuong-cua-nhan-dan-qua-doan-tho-039-039-nhung-nguoi-vo-nho-chong-nhung-cuoc-doi-da-hoa-song-nui-ta-039-039-trong-dat-nuoc-cua-nguyen-khoa-136415.jpg)
PHP: The Good Parts: Delivering the Best of PHP- P8
Số trang: 20
Loại file: pdf
Dung lượng: 544.21 KB
Lượt xem: 19
Lượt tải: 0
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- P8: 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- P8The PHP documentation points out that closures are also great for use in callbackfunctions when you are walking through an array (for example) and you want to runa function against each element of an array. Here is a very simple example: $person_info = function($value, $key) { echo $key . : . $value . ; }; $person_array = array(name => Dawn, age => 15, eyecolor => green); array_walk($person_array, $person_info);Here, we pass an actual variable to the array_walk function as its second parameter,rather than as a true reference to a function.NOWDOCThe next 5.3 enhancement I’d like to bring to your attention is called NOWDOC,which is merely a variation on the HEREDOC functionality in relation to string man-agement. If you recall, HEREDOC text behaves the same as if you were working witha string within double quotes, in that it allows for the inclusion of variable contentdirectly within the string such that the content of the variable will be resolved withinthe string. The NOWDOC construct behaves the same as if you were dealing withstrings in single quotes, in that there is no resolution of variable content (no interpo-lation), as the token is defined with single quotes. Here is an example of HEREDOCand NOWDOC to contrast what each does: $myname = Peter MacIntyre ; $str = The NOWDOC construct lends itself to longer segments of text that don’t require anycharacter or variable escaping, like the body of a standard email message or some dis-claimer text on a report.goto OperatorIf you have ever used a language like Basic or Visual Basic, you know all aboutthe goto statement in those languages. There was reportedly a great struggle within thePHP development team when discussing whether they should add this feature to thelanguage, and it looks like the advocates won out. This operator allows you to jump toother locations within a code file. This is a powerful new addition to the language,which is why I am introducing it here. But, as you will see in the following examples,it can also be considered a “bad part” of PHP if used improperly. There is a section onthe goto statement in the Appendix for this reason.There are some limitations on goto’s use: • You cannot jump to another code file. • You cannot jump out of or into a function or method (you can only jump within the same scope). • You cannot jump into looping structures like while loops or switch constructs, but you can jump out of them.To define a goto destination, mark it with a label (which can be alphanumeric, but muststart with an alpha character) followed by a colon (:). To activate the jumping action,simply precede the label name with the goto command. Here is a simple example: $letter = A ; if ($letter == A) { goto landing_area_a; } else { goto landing_area_b; } landing_area_a: echo The Eagle has landed; landing_area_b: echo The Hawk has landed;The browser output may not be quite what you expect: The Eagle has landed The Hawk has landedThe reason that both of these comments are sent to the browser is that once you reachyour goto destination, the code continues from that point and runs forward, so the nextlabel (landing_area_b:) is ignored, but the echo statement is executed, as it is next in124 | Chapter 10: PHP 5.3 Good Partsline. A way around this is to add another goto statement that will effectively jump overthe remaining code that you don’t want to run. landing_area_a: echo The Eagle has landed; goto lands_end; landing_area_b: echo The Hawk has landed; lands_end:Of course, you are starting to see exactly why there was so much discussion aboutwhether or not to add this feature into PHP 5.3. There is the potential to write what isknown as spaghetti code, code that potentially jumps and lands all over the file and thatmakes it frightfully difficult to follow and maintain.One further warning is the potential for endless looping. Take a look at this code andtry to follow it; I don’t recommend that you load it into your PHP environment unlessyou have the ability to shut it down on your own: starting_point: $letter = A ; if ($letter == A) { goto landing_area_a; } else { goto landing_area_b; } landing_area_a: echo The Eagle has landed; goto lands_end; landing_area_b: echo The Hawk has landed; lands_end: goto starting_point;Here are the first few lines of the browser output: The Eagle has landed The Eagle has landed The Eagle has landed The Eagle has landed The Eagle has landed The Eagle has landedAs you can see, this is the dreaded endless loop, which the goto operator can create ifyou are not careful. If this happens to you and you are quick en ...
Nội dung trích xuất từ tài liệu:
PHP: The Good Parts: Delivering the Best of PHP- P8The PHP documentation points out that closures are also great for use in callbackfunctions when you are walking through an array (for example) and you want to runa function against each element of an array. Here is a very simple example: $person_info = function($value, $key) { echo $key . : . $value . ; }; $person_array = array(name => Dawn, age => 15, eyecolor => green); array_walk($person_array, $person_info);Here, we pass an actual variable to the array_walk function as its second parameter,rather than as a true reference to a function.NOWDOCThe next 5.3 enhancement I’d like to bring to your attention is called NOWDOC,which is merely a variation on the HEREDOC functionality in relation to string man-agement. If you recall, HEREDOC text behaves the same as if you were working witha string within double quotes, in that it allows for the inclusion of variable contentdirectly within the string such that the content of the variable will be resolved withinthe string. The NOWDOC construct behaves the same as if you were dealing withstrings in single quotes, in that there is no resolution of variable content (no interpo-lation), as the token is defined with single quotes. Here is an example of HEREDOCand NOWDOC to contrast what each does: $myname = Peter MacIntyre ; $str = The NOWDOC construct lends itself to longer segments of text that don’t require anycharacter or variable escaping, like the body of a standard email message or some dis-claimer text on a report.goto OperatorIf you have ever used a language like Basic or Visual Basic, you know all aboutthe goto statement in those languages. There was reportedly a great struggle within thePHP development team when discussing whether they should add this feature to thelanguage, and it looks like the advocates won out. This operator allows you to jump toother locations within a code file. This is a powerful new addition to the language,which is why I am introducing it here. But, as you will see in the following examples,it can also be considered a “bad part” of PHP if used improperly. There is a section onthe goto statement in the Appendix for this reason.There are some limitations on goto’s use: • You cannot jump to another code file. • You cannot jump out of or into a function or method (you can only jump within the same scope). • You cannot jump into looping structures like while loops or switch constructs, but you can jump out of them.To define a goto destination, mark it with a label (which can be alphanumeric, but muststart with an alpha character) followed by a colon (:). To activate the jumping action,simply precede the label name with the goto command. Here is a simple example: $letter = A ; if ($letter == A) { goto landing_area_a; } else { goto landing_area_b; } landing_area_a: echo The Eagle has landed; landing_area_b: echo The Hawk has landed;The browser output may not be quite what you expect: The Eagle has landed The Hawk has landedThe reason that both of these comments are sent to the browser is that once you reachyour goto destination, the code continues from that point and runs forward, so the nextlabel (landing_area_b:) is ignored, but the echo statement is executed, as it is next in124 | Chapter 10: PHP 5.3 Good Partsline. A way around this is to add another goto statement that will effectively jump overthe remaining code that you don’t want to run. landing_area_a: echo The Eagle has landed; goto lands_end; landing_area_b: echo The Hawk has landed; lands_end:Of course, you are starting to see exactly why there was so much discussion aboutwhether or not to add this feature into PHP 5.3. There is the potential to write what isknown as spaghetti code, code that potentially jumps and lands all over the file and thatmakes it frightfully difficult to follow and maintain.One further warning is the potential for endless looping. Take a look at this code andtry to follow it; I don’t recommend that you load it into your PHP environment unlessyou have the ability to shut it down on your own: starting_point: $letter = A ; if ($letter == A) { goto landing_area_a; } else { goto landing_area_b; } landing_area_a: echo The Eagle has landed; goto lands_end; landing_area_b: echo The Hawk has landed; lands_end: goto starting_point;Here are the first few lines of the browser output: The Eagle has landed The Eagle has landed The Eagle has landed The Eagle has landed The Eagle has landed The Eagle has landedAs you can see, this is the dreaded endless loop, which the goto operator can create ifyou are not careful. If this happens to you and you are quick en ...
Tìm kiếm theo từ khóa liên quan:
phương pháp lập trình lập trình web với php ngôn ngữ lập trình php tính năng lập trình phpTài liệu liên quan:
-
Giáo trình Lập trình logic trong prolog: Phần 1
114 trang 206 0 0 -
Giáo trình Lập trình C căn bản
135 trang 176 0 0 -
Giáo trình Lập trình C căn bản: Phần 1
64 trang 170 0 0 -
66 trang 156 0 0
-
14 trang 137 0 0
-
Giáo trình lập trình hướng đối tượng - Lê Thị Mỹ Hạnh ĐH Đà Nẵng
165 trang 122 0 0 -
Bài giảng Phương pháp lập trình: Chương 9 - GV. Từ Thị Xuân Hiền
36 trang 115 0 0 -
47 trang 113 2 0
-
Giáo trình về môn Lập trình C căn bản
131 trang 50 0 0 -
Bài giảng Lập trình hướng đối tượng (dùng JAVA): Chương 1 - Trần Minh Thái
40 trang 42 0 0