SoFunction
Updated on 2025-03-10

Collected php to write large website problem sets

PHP has been quickly promoted for its ease of use, but ease of use does not mean that it can be used well. In fact, many programmers can easily set up WEB application systems, but how many people have carefully considered their code, whether it is easy to maintain, whether it is robust enough, efficient enough, and safe enough. These become very critical factors when PHP is used to build large websites. Let's start with minor issues, until some fatal mistakes. It is divided into three parts.
Part 1, Slight error

1. Printf(),
This function is mainly used to format and display data. Used only when you want to change the display format of a certain data.
For example, display the value of PI (3.1415926) with different accuracy.
  <?php 
   /* 
   * The three faces of Π 
   */ 

   printf ("Pi is: %.2f\n<br>\n", M_PI); 
   printf ("Pi is also: %.3f\n<br>\n", M_PI); 
   printf ("Pi is also: %.4f\n<br>\n", M_PI); 
  ?> 

But many programmers use this function only to display some variable values ​​and function return values. Because Printf() needs to format the data to be slower before displaying it, print and echo are applied only to display the data to increase the speed.

2. Semantic check
PHP is a weak-type language, which means that it does not need to be defined before using a variable, which brings great convenience and flexibility to programming, but you must know which type the variable should be, because the variable still actually corresponds to a certain type at runtime (various types can be converted freely between each other), and variables without type do not exist. It is possible that PHP cannot check for your semantic errors, but due to changes in variable types, some potential problems will occur. Another noteworthy issue is the range of variables, which may also lead to some potential problems.
There are the following basic variables in PHP:
  Boolean, resource, integer, double, string, array and object。 

3. Use of temporary variables
The abuse of temporary variables will lead to a decrease in program operation efficiency. When to use temporary variables can be considered based on the following two points:
1. Whether this variable is used at least twice.
2. Whether the use of this variable will significantly improve the readability of the program.
If none of them is satisfied, the use of this variable is omitted. For example:
  <?php 
   $tmp = date ("F d, h:i a"); /* ie January 3, 2:30 pm */ 
   print $tmp; 
  ?> 
It should be changed to:
  <?php 
   print date ("F d, h:i a"); 
  ?> 

Another example:
  <?php 

  // 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 to:
  <?php 

  // 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 ("", $characters); 
  } 

  ?> 

4. Separation of client and server code
In PHP programs, client and server-side code are actually HTML code and PHP language code. Many people mix HTML and PHP statements in one file, making this file very large. This style is very unfavorable to the maintenance and redevelopment of the program and is not suitable for the development of large sites. There are generally two ways to separate HTML and PHP statements:
1. Write a dedicated API, for example:

   ? The Client side 
  <?php include_once (""); ?> 
  <html> 
  <head> 
  <title> <?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> 

   
   ? The server side code 

   
  <?php 

  $dbh = mysql_connect ("localhost", "sh", "pass") 
  or die (sprintf ("Cannot connect to MySQL [%s]: %s", 
  mysql_errno (), mysql_error ())); 
  @mysql_select_db ("MainSite") 
  or die (sprintf ("Cannot select database [%s]: %s", 
  mysql_errno (), mysql_error ())); 

  $sth = @mysql_query ("SELECT * FROM site", $dbh) 
  or die (sprintf ("Cannot execute query [%s]: %s", 
  mysql_errno (), mysql_error ())); 

  $site_info = mysql_fetch_object ($sth); 

  function print_header () 
  { 
   global $site_info; 
   print $site_info->header; 
  } 

  function print_body () 
  { 
   global $site_info; 
   print nl2br ($site_info->body); 
  } 

  function print_links () 
  { 
   global $site_info; 

   $links = explode ("\n", $site_info->links); 
   $names = explode ("\n", $site_info->link_names); 

  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 simpler and executes faster.

2. Methods to use templates
This method makes the program look simpler and also implements the above functions. The following code can be used:
  <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> 

Use a placeholder to replace the content to be generated dynamically, and then use a parser to analyze the template file and replace the placeholder with the content of the placeholder. This method allows page makers who do not use PHP to modify template files. The disadvantage of this approach is that it is not efficient in execution because the template file is interpreted. It is also more complicated to implement.

Note: The FastTemplate class can easily implement the above functions.

5. Don’t use outdated functions
As a free software, PHP develops very quickly, and many of its functions are outdated, such as:

  while (1): 
  print "5"; 
  if ($idx++ == 5): 
  break; 
  endif; 
  endwhile; 

Although it can still be used, the efficiency is definitely not high, and it may be disabled in future versions, resulting in the program not being able to run. Therefore, you should check whether those functions are outdated and correct in time according to the latest PHP manual.