Ten advanced skills in PHP (middle)

zhaozj2021-02-16  40

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: Something </ title> </ head> <body> [a lot of content] <// INSERT COPYRIGHT Require ('/ home / me / mycopyright');?> < / Body> </ html> On the other hand, I often use include () to control many functions in the beginning of the file: <? // Get the library include ('/ home / me / myfunctions'); // DO PHP Things with my functions?> <html> <head> <title> Something </ title> </ head> <body> [a lot of content] </ body> </ html> The next question is "include and Where is the REQUIRE file? ", a simple answer to this question is" "If your code contains database connections with user names and passwords, you will definitely put them in the document root. Directory is open to all people. Included or Required files can be available anywhere on the system, as long as the user on the system running in PHP can access these files, you can make these files any suffix, or do not use the suffix.</p> <p>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 = "<ul>"; / * Using a While statement, read all elements in the open directory, if the name of the file is not "" or "..", The name in the list * / while ($ file_name = readdir) {IF (($ file_name! = ") && ($ file_name! =". ")) {$ File_list. =" <Li > $ file_name ";}} $ file_list. =" </ ul> "; / * Close the open directory, end the PHP module * / closedir ($ dir);?> <! - start your html -> <html > <Head> <title> Directory Listing </ title> </ head> <body> <! - Use php to print -> <p> files in: <? Echo "$ dir_name ";> </ p> <! - Use php to print the directory listing -> <? 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.</p> <p>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.</p> <p>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 <br>";}?> 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 <br > ";}?> 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.</p> <p><? / * 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 <br>";}?> 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.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-21754.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="21754" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.035</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'TawY7sxiBId0YIkd0vZA2Wuoj1BYgPC_2FJPazYZyApFuiIqb_2Fgt8k0OD3Vvh35xcnSZUW5sDWDIh5qBnB00WguA_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>