PHP write large website problem set

zhaozj2021-02-11  227

PHP preparations a large website problem set PHP is promoted quickly with its ease of use, but it is easy to say that it can be used, in fact, many programmers use it easy to set up a web application system, but how much People carefully consider their code, whether it is easy to maintain, whether it is strong enough, whether it is sufficiently safe, when PHP is used to establish large sites, these become a critical factor. Below we start discussing from a slight problem until some fatal errors. A total of three parts.

Part 1, slight mistake

First, Printf (), which is mainly used to format display data. Use when you want to change the display format of a data. For example, the value of PI (3.1415926) is displayed in different precursors.

Printf ("Pi IS:% .2F / N
/ N", M_PI); Printf ("Pi IS AlSo:% .3F / N
/ N", M_PI); Printf ("PI IS Also: % .4f / n
/ n ", m_pi);

However, many programmers use this function to display some variable values ​​and functions. Because Printf () is pre-formatted before displaying the data, it is slower, so it is applied to the application of Print and ECHO only to increase the speed.

Second, the language checking PHP is a weak type of language, that is, do not use definition before using a variable, which brings a lot easier and flexible, but you must know which type of variable should be. Because the variable still actually corresponds to a certain type (between various types, there is no type of variable without types of variables. It is possible that PHP cannot check your language error, but due to variable type changes, there will be some potential problems. Another worthless problem is the range of variables, which may also cause some potential problems. There are several basic variables in PHP: Boolean, Resource, INTEGER, DOUBLE, STRING, ARRAY AND OBACT.

Third, the use of temporary variables will cause a reduction in program operation efficiency. When using temporary variables can be based on the following two points: 1, the variable is used at least twice. 2, whether the use of this variable will significantly improve the readability of the program. If an item is not satisfied, the use of the variable is omitted. For example: It should be changed to:

Another example:

// String Reverse_Characters (String Str) // Reverse All of the Characters in A String.Function Reverse_Characters ($ Str) {Return Implode ("", array_reverse (preg_split ("//", $ str));}

?> The readability is not strong, it can be changed:

// string reverse_characters (string str) // Reverse all of the characters in a string.function reverse_characters ($ str) {$ characters = preg_split ( "//", $ str); $ characters = array_reverse ($ characters);

RETURN IMPLODE ("", $ taracters);}?>

4. The separation client and server-side code separation client and server-side code are actually HTML code and PHP language code in the PHP program. Many people mix HTML and PHP statements in a file, making this file very large. This style is very disadvantageous to the maintenance and renewal of the procedure and is not suitable for the development of large sites. There are usually two ways to separate HTML and PHP statements: 1. Write a dedicated API, for example: index.php? The client side <? Php print_header ();> </ title> </ head> <body> <h1> < PHP Print_Header ();?> </ h1> <table border = "0" cellpadding = "0" cellspacing = "0"> <tr> <td width = "25%> <? php print_links ();? > </ td> <td> <? php print_body ();?> </ td> </ tr> </ table> </ body> </ html></p> <p>Site.lib? The Server Side Code</p> <p><? PHP</p> <p>$ dbh = mysql_connect ("localhost", "sh", "pass") or Die (SprintF ("CannotF (" Cannot Connect To MySQL [% S]:% S ", MySQL_ERRNO (), mysql_error ()); @ mysql_select_db (" ") Or Die (" Cannot Select Database [% s]:% s ", mysql_errno (), mysql_error ());</p> <p>$ Sth = @Mysql_Query ("Select * from Site", $ DBH) or Die ("Cannot EXECUTE Query [% s]:% s", MySQL_ERRNO (), mysql_error ());</p> <p>$ Site_info = mysql_fetch_object ($ STH);</p> <p>Function print_header () {global $ site_info; Print $ Site_info-> header;}</p> <p>Function print_body () {Global $ Site_info; Print NL2BR ($ Site_info-> body);</p> <p>Function Print_Links () {Global $ Site_info;</p> <p>$ links = expedition ("/ n", $ site_info-> links); $ names = expedition ("/ n", $ site_info-> link_names);</p> <p>For ($ I = 0; $ i <count ($ links); $ i ) {print "/ t / t / t <a href = /" $ links [$ i] / "> $ names [$ i] < / a> / n <br> / n ";}}?> This method makes the program look relatively simple, and the execution speed is faster.</p> <p>2. Method of using templates This method makes the program look more concise, also implement the above functions, available: <html> <head> <title> %% Page_title %% </ Title> </ head> <body %% body_properties %%> <h1> %% Page_title %% </ h1> <table border = "0" cellpadding = "0" cellspacing = "0"> <tr> <td width = "25%"> %% Page_links %% </ td> <td> %% Page_Content %% </ TD> </ TR> </ Table> </ body> </ html></p> <p>Use the placeholder to replace the content to be generated, and then analyze the template file with a parser to replace the contents of the placeholder. The method makes it possible to modify the template file even if PHP is not used. The disadvantage of this method is that the execution efficiency is not high because the template file is to be explained. At the same time, it is also more complicated.</p> <p>Note: The FastTemplate Class of www.thewebmasters.net is convenient to implement the above features.</p> <p>5. Don't use the time-out function as a free software, PHP has developed very fast, many of which have been outdated, for example:</p> <p>While (1): Print "5"; if ($ IDX == 5): Break; Endif; endwhile;</p> <p>Although it can be used but the efficiency is not high, and it may be disabled in future versions, causing the program to not run. Therefore, it is often necessary to check the latest PHP manuals to check the functions that the functions have been fixed in time.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-4115.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="4115" 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.080</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 = 'iuXmnLmrDAHYMRIOTLmqz7BvjodjwjU2Y3UT6gp4s9VSU_2FNLKSy03hnCb_2FfnzsPQQWK9THkK5TL5TYMwEcN6Kg_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>