PHP and MySQL Web Development - P33

PHP and MySQL Web Development - P33: PHP and MySQL Web Development teaches the reader to develop dynamic, secure, commercial Web sites. Using the same accessible, popular teaching style of the first edition, this best-selling book has been updated to reflect the rapidly changing landscape of MySQL and PHP. | Using require for Web Site Templates 127 require This code will needlessly load both files every time the script is run but only use one depending on the value of variable. However if the code had been written using two include statements only one of the files would be loaded and used as in the following version if variable true include else include Unlike files loaded via a require statement files loaded via an include can return a value. Therefore we can notify other parts of the program about a success or failure in the included file or return an answer or result. We might decide that we are opening files a lot and rather than retyping the same lines of code every time we want an include file to open them for us. Our include file might be called and resemble the following php @ fp fopen name mode if fp echo p strong Oh No I could not open the file. strong p return 0 else return l This file will try to open the file named name using the mode given by mode. If it fails it will give an error message and return 0. If it succeeds it will return 1 and generate no output. We can call this file in a script as follows name mode r result include if result 1 128 Chapter 5 Reusing Code and Writing Functions do what we wanted to do with the file refer to fp created in the include file Note that we can create variables in the main file or in the included or required file and the variable will exist in behavior is the same for both require and include statements. You cannot use require in exactly the way shown here because you cannot return values from require statements. Returning a value can be useful because it enables you to notify later parts of your program about a failure or to do some self-contained processing and return an answer. Functions are an even better vehicle than included files for breaking code into self-contained modules. We will look at functions next. If you are wondering why given

Không thể tạo bản xem trước, hãy bấm tải xuống
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.