Ten advanced skills in PHP (middle)
Third, the document is our friend, regardless of the size of the website you develop, you should realize the importance of code reuse, whether these code is HTML, or PHP code. For example, you must change a footer that contains copyright information at least every year. If your website contains 1000 pages, it is also an annoying thing to modify each year. In PHP, at least there are at least few functions help you implement the code reuse, the function used depends on the code you reuse, the main functions are: * include () and include_once () * Require () And Require_once The include () function contains and calculates a given file, for example: include ('/ home / me / myfile'); any code in the include file is executed within the code range of include (), you can Contains static files on its own server by jointly using the combination, containing the target file on another server. The functionality of include_once () is the same as include (). The difference between the two is whether it checks if the code in a file is already included in an existing script. If the code already exists, it will not be included again. Require () function replaces itself with the content of a given file itself, this replacement process occurs during the PHP engine compile code, not during execution, it is not like include () will be calculated first. The Require () function is more used in static elements, and include () is more used in dynamic elements. Similar to include_once (), request_once () will first check if the given code is inserted, and if the code already exists, it is no longer inserted. In order to understand its content, in the elements of copyright information, static text, and other elements that do not contain variables or elements that rely on other ongoing scripts, I tend to use the Require function. For example:
Use include () and Require () to make a generalization of elements in the website, and bring you great convenience when you need to upgrade your website. Fourth, PHP and file system maintenance PHP has many functions related to the file system, but these functions can not only open files, but also display content, mobile files, and other functions in the directory, many people even develop based on the Internet with php File Explorer. About the interpretation of the file path: In Windows, you can use / and symbols in the path, and only / symbols in other operating systems can be used. For consistency, we use / symbols. The following script sample can display a directory list, which is already included in the code: / * Stores the full path name of the directory to be read into a variable of $ dir_name. * / $ Dir_name = "/ home / me /"; / * Create a handle that is the result of opening a given directory * / $ dir = OpenDir ($ dir_name); / * Create a text block for placement List element (file name) * / $ file_list = "
files in: Echo "$ dir_name ";> p> echo" $ file_list ";?> body> html> Ok, we have received a list of directories . It should be noted that the contents of the system on the system that should be read by reading a file (later we will explain later), the user on the system of PHP must have at least read files.
Here is an example of how to copy files: / * Assign the full path of the original file you want to copy to a variable of $ Original, give a full path to the copy file to a name $ COPIED variable * / $ Original = "/ home / me / mydatabaseDump"; $ copy = "/ archive / mydatabaseDumo_1010"; / * Using the Copy () function copy the original file, if the copy is not completed, an error message is displayed * / @copy $ ORIGINAL, $ COPIED) OR DIE ("COULDN't Copy File.");?> This example is the prototype of a file backup system. When this script is run, it copies the file to a different location for saving. Slightly modify the daemon, you can execute it at the time you specified during the day without the need for user intervention. Assume that you install Lynx on the system, you can create a daemon entry to access this file, access this file, run this script and create a copy file, the following example will run this script at 5 o'clock in the morning, then close Lynx: 0 5 * * * [username] lynx -dump http://localhost/copyfile.php 1> / dev / null 2> & 1 If you run the CGI version of PHP, you can skip the Lynx section, and call the binary directly: 0 5 * * * [username] php /path/to/copyfile.php 1> / dev / null 2> & 1 5, rich array function PHP 4.0 newly added 30 functions related to the number of groups, some of which are common The function can determine if an array contains an element, counts elements in an array, add or delete elements in an array or sort the elements in the array. If there is a big array, you need to find out if you contain a specific element, you can use IN_ARRAY (). The following example will display "Not Found In this Array", because in an array of $ Nameserray, you don't have such an element in the $ NameserRay array. $ nameserray = array ("Joe", "Jane", "Bob", "Mary", "Paul", "EDDIE", "John"); $ lookingfor = "albert"; if (in_Array ($ LOOKINGFOR, $ Nameserray) {echo "You'Ve Found It!";} else {echo "not found in this array!";}?> You've Found IT if you change the value of $ toOKingfor to Mary Information, because Mary is an element in the NameserRay array.
If you want to count the number of elements in an array, just simply use the count () function: <$ nameserray = array ("joe", "jane", "bob", "mary", "paul" , "EDDIE", "John"); $ count = count ($ nameserray); "The value returned is 7. You can add an element at the beginning or end of an array, and use array_merge () to create a new array of elements in two or more groups, when combined, the order of the elements is arranged in the specified order, if the original The array is sorted by sequence, and it needs to be reordered it after the merger. We can first use Array_Push () to add an element at the end of the array: / * Create an array * / $ fruitaray = array ("Apple", "Orange", "Banana", "Kiwi", "PEAR") ; / * Add an element to an array * / array_push ($ FrUitArray, "Grape", "PineApple", "Tomato"); / * Display Each Element and its serial number * / while (list ($ key, $ value) = Each ($ fruitaray)) {echo "$ key: $ value
";}?> Run the above program will get the following result: 0: Apple 1: Orange 2: Banana 3: Kiwi 4: PEAR 5: grape 6: Pineapple 7: Tomato If you need to add an element at the beginning of the array, the code is similar to the above code, the only difference is to replace Array_Push () with array_unshift (). / * Create an array * / $ fruitaray = array ("Apple", "Orange", "Banana", "Kiwi", "PEAR"); / * Add an element to an array * / array_unshift ($ fruitaray, " Grape "," PineApple "," Tomato "); / * Shows Each Element and its serial number * / while (list ($ key, $ value) = Each ($ fruitaray) {echo" $ key: $ VALUE
";}?> Run the above program will get the following result: 0: Grape 1: PineApple 2: Tomato 3: Apple 4: Orange 5: Banana 6: Kiwi 7: Pear array_merge () function can put two or more Many arrays merge into an array.
/ * Establish the first array * / $ fruitaray = array ("Apple", "Orange", "Banana", "Kiwi", "PEAR"); / * / Establish the second array * / $ vegarray = Array ("Carrot", "Green Beans", "ASPARAGUS", "Artichoke", "Corn"); / * combine these two numbers into an array * / $ goodFoodArray = array_merge ($ fruitarray, $ vegarray); / * Show each element and its serial number * / while (list ($ key, $ value) = Each ($ goodFoodArray) {echo "$ key: $ value
";}?> Run the above script will get below Results: 0: Apple 1: Orange 2: Banana 3: Kiwi 4: Pear 5: Carrot 6: Green Beans 7: Asparagus 8: Artichoke 9: Corn now we have mastered how to add an element and merge array, let's take a look How to delete elements from an array. Delete an element from an array can use the Array_POP () function, using the array_shift () function to delete an element from an array. Although an element is removed from array_pop () or array_shift (), you can use this element as a variable.