TAILIEUCHUNG - PHP 5 Recipes A Problem-Solution Approach 2005 phần 8

Trong PHP 4, đoạn mã này sẽ in ra “Andi”. Lý do là ở chỗ chúng ta truyền đối tượng $person vào hàm changeName() theo giá trị, và do vậy, $person được sao chép và changeName() hoạt động trên một bản sao của $person. | 11-10 CREATING DYNAMIC FUNCTIONS 449 11-10. Creating Dynamic Functions One of the advantages of using PHP functions is that you can create conditional occurrences that allow you to write functions only if strictly necessary. By placing function declarations within conditional statements you can force PHP to create a function only if a condition has been met. By using this sort of functionality you can actually create functions dynamically by allowing functions to be born based on a certain condition. Let s say you want to take in a value from the user and based on that value you create a function that performs a certain task. For instance based on what the user enters you need a function either to add two values to subtract two values or to multiply two values. Rather than clutter your code with functions you may not use you can create the valid function on the fly and call it by just one name. The following example is useful in a site where a user can log in and log out based upon their current status. The Code php if _GET go yes Now if you are logged in you want the function to log you out. if _GET loggedin true Create a logout function. function dosomething _GET loggedin false echo You have been successfully logged out. br Now if you were not logged in you want to be able to log in. if _GET loggedin false Create a login function. function dosomething _GET loggedin true echo You have been successfully logged in. br dosomething 450 11-10 CREATING DYNAMIC FUNCTIONS if _GET loggedin a href go yes amp loggedin true - click here to log out a php elseif _GET loggedin a href go yes amp loggedin false - click here to log in a php If you click to log in you should get this message and hence be logged in You have been successfully logged in. click here to log out If however you click to log out you should get the following result You have been successfully logged out. click here to log in How It Works This particular instance

TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã 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.