Đang chuẩn bị liên kết để tải về tài liệu:
PHP 5 Recipes A Problem-Solution Approach 2005 phần 8

Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ

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 sample11_10.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 sample11_10.php go yes amp loggedin true - click here to log out a php elseif _GET loggedin a href sample11_10.php 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