Home  >  Article  >  Backend Development  >  How to become a PHP master? Learn "lazy" programming_PHP tutorial

How to become a PHP master? Learn "lazy" programming_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:59:22706browse

PHP is an efficient network programming language. Due to its advantages of flexible writing and fast running, it has quickly become the preferred language for Web programmers. An authoritative survey not long ago showed that 31.6% of websites now use PHP as the main server-side programming language.

However, it is not easy to become a PHP programming master. It is not as many people imagine, as long as you can quickly write a few simple codes to solve a complex problem, you are a PHP programming master. A real PHP master also needs to consider many other issues. The following three guidelines are the guidelines that a mature PHP programmer should first follow in programming.

1. Laziness is gold

2. Write beautiful code

3. Pursue the speed of the program, not the speed of programming

  1. Laziness is golden

Are you a lazy programmer? This idea is so strange! Because probably the busiest people in the world are computer programmers. But it is precisely because programmers are too busy that they should learn to be lazy when programming.

For a programmer, there are two lazy ways: First, boldly use other people’s ready-made program codes and integrate these codes into your own programs or projects. The second is to write some useful codes to build a function library, which can be easily used when writing programs in the future. This saves a lot of repetitive work and naturally makes you less lazy.
These two lazy methods are very suitable for PHP programmers.

First of all, PHP is a language that was born and grown in a free and open environment. There are thousands of programmers around the world who have been constantly striving for the perfection of PHP, and they are also willing to share their ingenuity and the code they write with others. You can find a lot of excellent program code every day from some PHP websites, mailing lists, and news groups. By saying this, I am not encouraging you to wait all day for others to write code for you, but you can "stand on the shoulders of great men" and fully promote the "use doctrine". Smart application of other people's program codes can save you a lot of money. time. Secondly, in PHP, you can easily build your own function library, which can save you a lot of trouble when writing programs in the future.

The author below introduces several common functions to you. Some of these functions come from some open source projects on the Internet, and some are selected from the mailing list. If you can add them to your own library, sooner or later you will find yourself benefiting from them.

 1. General database processing functions

Compared with other CGI functions, one of the advantages of PHP is that it has very powerful database processing capabilities. However, in PHP, some specific functions are used to handle different databases, and there is a lack of general database processing functions. This greatly reduces the portability of program code, which also brings a lot of inconvenience to beginner programming friends.

On the Internet, many programmers have solved this problem by encapsulating classes. They wrote unified functions to handle any popular database - whether it is Mysql, which is popular in the Linux world, or SqlServer, which is widely popular on Windows platforms. Personally, I like to use these functions very much, because you can directly use some simple functions such as "query" and "next_record" without having to consider complicated things such as database connections and database handles, let alone Consider what database you are using.

If you need these functions, you can get them by visiting the following URLs:

http://phplib.netuse.de/
http://phpclasses.UpperDesign .com/browse.html/package/20
http://phpdb.linuxbox.com/

2. Variable debugging function

Debugging of PHP programs has always been an exciting task The troublesome thing is that it does not have an integrated compilation and debugging environment like high-level languages ​​such as VB, nor can it run directly in a Linux or DOS environment like Perl. In fact, we can complete the debugging of PHP by flexibly using the echo statement.

The following functions allow you to check the type and value of any variable in the program at any time.

function ss_array_as_string (&$array, $column = 0) {
 $str = "Array(n";
  while(list($var, $val) = each($array)){
   for ($i = 0; $i < $column+1; $i++){
    $str .= "&nbsp;&nbsp;&nbsp;&nbsp;";
   }
   $str .= $var. ==> ;
   $str .= ss_as_string($val, $column+1)." n";
  }
  for ($i = 0; $i < $column; $i++){
   $str .= "&nbsp;&nbsp;&nbsp;&nbsp;";
  }
  return $str.);
 }
 function ss_object_as_string (&$object, $column = 0) {
  if (empty($object->classname)) {
   return "$object";
  }
  else {
   $str = $object->classname."( n";
    while (list(,$var) = each($object->persistent_slots)) {
     for ($i = 0; $i < $column; $i++){
      $str .= "&nbsp;&nbsp;&nbsp;&nbsp;";
     }
     global $$var;
     $str .= $var. ==> ;
     $str .= ss_as_string($$var, column+1)." n";
    }
    for ($i = 0; $i < $column; $i++){
     $str .= "&nbsp;&nbsp;&nbsp;&nbsp;";
    }
    return $str.);
  }
 }
 function ss_as_string (&$thing, $column = 0) {
   if (is_object($thing)) {
    return ss_object_as_string($thing, $column);
   }
   elseif (is_array($thing)) {
    return ss_array_as_string($thing, $column);
   }
   elseif (is_double($thing)) {
    return "Double(".$thing.")";
   }
   elseif (is_long($thing)) {
    return "Long(".$thing.")";
   }
   elseif (is_string($thing)) {
    return "String(".$thing.")";
   }
   else {
    return "Unknown(".$thing.")";
   }
 }

  需要的时候,在程序中简单地加入下面的一条代码即可查看程序中的所使用的变量(包括数组和对象)的类型和值:

echo ss_as_string($my_variable);

  使用下面的语句,我们可以直接查看程序中所有的变量的值:

echo ss_as_string($GLOBALS);

  3. 控制Log信息的函数

  调试PHP程序的另外一种重要的方法就是查看Log信息。如果能够方便地控制Log信息的级别以及Log信息的显示内容,将会给程序调试带来更多的便利。下面的几个函数可以方便地实现这个功能。

$ss_log_level = 0;
$ss_log_filename = /tmp/ss-log;
$ss_log_levels = array(
 NONE => 0,
 ERROR => 1,
 INFO => 2,
 DEBUG => 3);
function ss_log_set_level ($level = ERROR) {
 global $ss_log_level;
 $ss_log_level = $level;
}
function ss_log ($level, $message) {
 global $ss_log_level, $ss-log-filename;
 if ($ss_log_levels[$ss_log_level] < $ss_log_levels[$level]) {
  // 不显示Log信息
  return false;
 }
 $fd = fopen($ss_log_filename, "a+");
 fputs($fd, $level. - [.ss_timestamp_pretty().] - .$message."n");
 fclose($fd);
 return true;
}
function ss_log_reset () {
 global $ss_log_filename;
 @unlink($ss_log_filename);
}

  在上面的函数中,有四个Log级别变量。运行PHP程序时,只有当Log的级别低于预设的级别值时,Log信息才可以被记录和显示出来。例如,在程序中加入如下的一条语句:

ss_log_set_level(INFO);

  那么,运行PHP程序时,只有ERROR和INFO级别的LOG信息才能被记录和显示出来,DEBUG级的信息则被忽略了。除此之外,我们还可以设定显示的信息内容,其语句如下:

ss_log(ERROR, "testing level ERROR");
ss_log(INFO, "testing level INFO");
ss_log(DEBUG, "testing level DEBUG");

  你也可以随时使用下面的语句清空LOG信息:

ss_log_reset();

  4.速度测试函数

  为了优化代码,我们 需要一种可以测试代码运行时间的方法,从而来选择最优的代码。下面的函数可以测试运行代码所需的时间:

function ss_timing_start ($name = default) {
 global $ss_timing_start_times;
 $ss_timing_start_times[$name] = explode( , microtime());
}
function ss_timing_stop ($name = default) {
 global $ss_timing_stop_times;
 $ss_timing_stop_times[$name] = explode(, microtime());
}
function ss_timing_current ($name = default) {
 global $ss_timing_start_times, $ss_timing_stop_times;
 if (!isset($ss_timing_start_times[$name])) {
  return 0;
 }
 if (!isset($ss_timing_stop_times[$name])) {
  $stop_time = explode(, microtime());
 }
 else {
  $stop_time = $ss_timing_stop_times[$name];
 }
 $current = $stop_time[1] - $ss_timing_start_times[$name][1];
 $current += $stop_time[0] - $ss_timing_start_times[$name][0];
 return $current;
}

  现在可以轻松地检查任何一段代码的执行时间了,甚至我们可以同时使用多个计时器,只需在使用上述的几个函数时设定不同的参数作为计时器的名称就可以了。

  5.调试和优化数据库的操作

  对于数据库来说,运行速度是至关重要的。尽管很多书籍和文章都讲授了一些快速运行数据库的方法,但是所有的方法都必须经过实践的检验。下面我们将把PHPLib函数库中的query()函数和上面介绍的几个函数综合起来编写成新的query()函数,和原先的函数相比,这个函数增加了运行时间的监测功能。

function query($Query_String, $halt_on_error = 1) {
 $this->connect();
 ss_timing_start();
 $this->Query_ID = @mysql_query($Query_String,$this->Link_ID);
 ss_timing_stop();
 ss_log(INFO, ss_timing_current(). Secs - .$Query_String);
 $this->Row = 0;
 $this->Errno = mysql_errno();
 $this->Error = mysql_error();
 if ($halt_on_error && !$this->Query_ID) {
  $this->halt("Invalid SQL: ".$Query_String);
 }
 return $this->Query_ID;
}

  二、编写漂亮的代码

  1.将后台程序与前端程序分开

  在编写PHP程序时,有些代码是用来处理一些事务,例如操作数据库、进行数学运算等,而另外的一些代码则只是事务处理的结果显示出来,例如一些使用echo语句将结果以HTML的格式显示在Web浏览器上的PHP代码以及那些直接嵌入PHP程序的HTML代码。首先我们应该清晰地区分这两种代码,把前者称为后台程序,把后者称为前端程序。

  因为PHP是一种嵌入式编程语言,也就是说,所有的PHP代码都可以嵌入到HTML代码之中,这为程序的编写带来了许多便利之处。但是,“物极必反”,如果在一段较长的程序中将PHP代码和HTML代码混合编写,这将使程序杂乱无章,不利于程序的维护和阅读。所以我们需要尽可能地将这些程序中混杂于HTML代码中的PHP代码移植出来,在专门的文件中将这些代码封装成函数,然后在HTML代码中使用include语句来包含这些文件,在适当的位置调用这些函数即可。

  这种做法一方面使HTML代码和PHP代码都简单易读,另一方面因为HTML代码需要不断更新,而这种分离的方法可以确保后台程序不会被破坏。

  同前端程序不同,后台程序更多追求的是稳定、结构化,极少更改,所以应该认真地设计和管理。其实,在设计台程序时,投入大量时间是值得的,“现在栽树,以后乘凉”,在以后的设计工作中将可以轻松地使用现在编写的后台程序。

  2.灵活使用包含文件

  正如前面所说的那样,后台程序应当安排在一系列的包含文件中。包含文件可以通过include语句在需要时动态装入,也可以在php.ini文件中通过使用auto_prepend_file指令预先自动装入。

  如果使用后一种方法的话,虽然取得了一劳永逸的好处,但是也有一些缺点值得我们注意。下面的一段代码向我们展示了解析一个庞大的包含文件需要一定的时间:

require(timing.inc);
ss_timing_start();
include(test.inc);
ss_timing_stop();
echo
.ss_timing_current().

?>

  在上面的代码中,test.inc是一个1000行的包含文件,运行的结果显示,解析这个包含文件花费了0.6秒钟,对于一个大型网站来说,这个速度并不是可以忽略不记的。

  使用包含文件的另外一个缺点是:如果一个文件中的一个语句发生错误,将会使整个网站的PHP程序都无法运行。所以使用起来也及其小心。

  其实,对包含文件稍做处理,即可以使包含文件只在需要时进行解析。下面的代码使abc.inc文件只在程序需要时才作解析:

if ( defined( __LIBA_INC) ) return;
define( __LIBA_INC, 1 );
/*
* 代码...
*/
?>

  3.使用面向对象的编程方法

  PHP也是一种面向对象的语言,面向对象的编程方法是优秀的程序员们非常推崇的一种软件设计方法,在PHP编程中可以充分发挥面向对象语言的优势,对编程中的对象进行封装。在前面的代码中,我们使用了面向对象的方法,例如在管理数据库时,我们将query()函数封装进数据库类中,这极大地方便了代码的管理,增加了程序的可读性。

  三、追求程序速度,而不是编程的速度

  在网站建设中,程序运行速度和网页下载速度都是关系成败的重要因素。作为一名Web程序员,应该更加注意代码的运行速度。下面介绍的几种方法都在不同程度上提高了代码的运行速度。

  1.使用内嵌的HTML代码,而不是PHP的echo语句。

  因为PHP是一门嵌入式Web编程语言,可以将HTML代码和PHP代码相互嵌入。但是很多程序员担心在HTML代码中过多的使用""嵌入PHP代码会多次调用PHP解释器,从而降低了PHP代码的运行速度,所以宁愿使用PHP的echo语句来输出HTML代码,而不直接使用HTML代码。但事实却恰恰相反。每一个PHP页面只调用一次PHP解释器来解释所有的PHP代码,所以,只在需要时才嵌入PHP代码,而大多数的时候直接使用HTML代码输入结果,不但不会降低程序的运行速度,而且因为减少了对echo语句的解析,往往可以提高代码的运行速度。

  下面的一段代码证明了我们的结论。在这段代码中,我们使用了前面介绍的时间测试函数。

  使用str-replace而不是ereg-replace

  习惯使用Perl进行编程的程序员更加愿意使用ereg_replace完成字符串替换工作,因为在PHP中ereg_replace的用法和Perl中模式匹配的用法相近。但是,下面的这段代码证明,使用str_replace 代替 ereg_replace将可以大大提高代码的运行速度。

  测试str_replace和ereg_replace的运行速度

//这段代码测试str_replace的运行速度

emphasis; ?>

for ($i=0; $i<1000; $i++) {
 str_replace(i>, b>, $string). ;
}
?>

//这段代码测试ereg_replace的运行速度

for ($i=0; $i<1000; $i++) {
 ereg_replace(<([/]*)i>, <\1b>, $string).

}
?>

//打印结果
结论

使用str_replace的时间 -
使用ereg_pattern的时间 -

  运行上面的代码,得到的结果是:

  使用str_replace的时间 - 0.089757
  使用ereg_pattern的时间 - 0.248881

  从运行的结果我们可以看出使用str_replace替代ereg_replace作为字符串替换函数,极大地提高了代码的运行速度。

  3.注意字符串的引用

  PHP和其它很多编程语言一样,可以使用双引号("")来引用字符串,也可以使用单引号()。但是在PHP中,如果使用双引号来引用字符串,那么PHP解析器将首先分析字符串中有没有对变量的引用,有变量的话,将对变量进行替换。如果是单引号,则没有如此复杂——直接将单引号包含起来的所有字符串直接显示出来。显然,在PHP编程中,如果使用单引号引用字符串变量要比使用双引号快速一些。

  4.在数据库中避免使用联合操作

  比起其它的Web编程语言来说,PHP的数据库功能十分强大。但是在PHP中数据库的运行仍然是一件十分费时费力的事情,所以,作为一个Web程序员,要尽量减少数据库的查询操作,同时应该为数据库建立适当的索引。另一件值得注意的事情是在用PHP操作数据库时,尽可能不使用多个数据表的联合操作,尽管联合操作可以增强数据库的查询功能,但是却大大增加了服务器的负担。

  为了说明这个问题,我们可以看看下面的这个简单的例子。

  我们在数据库中创建了两个数据表foo和big_foo。在数据表foo中,只有一个字段,包含了从1-1000之间的所有自然数。数据表big_foo同样只有一个字段,但包含了从1-1,000,000之间的全部自然数。所以,从大小上说,big_foo等于foo与它自身进行了联合操作。

$db->query("select * from foo");
0.032273 secs
$db->next_record();
0.00048999999999999 secs
$db->query("insert into foo values (NULL)");
0.019506 secs
$db->query("select * from foo as a, foo as b");
17.280596 secs
$db->query("select * from foo as a, foo as b where a.id > b.id");
14.645251 secs
$db->query("select * from foo as a, foo as b where a.id = b.id");
0.041269 secs
$db->query("select * from big_foo");
25.393672 secs

  从上面操作结果我们可以发现,对于两个有1000条记录的数据表进行联合,其速度并不比对一个1000000条纪录的大型数据表单独进行操作快多少。

  5.注意include与require的区别

  在PHP变成中,include()与require()的功能相同,但在用法上却有一些不同,include()是有条件包含函数,而require()则是无条件包含函数。例如在下面的一个例子中,如果变量$somgthing为真,则将包含文件somefile:

if($something){
 include("somefile");
}

  但不管$something取何值,下面的代码将把文件somefile包含进文件里:

if($something){
 require("somefile");
}

  下面的这个有趣的例子充分说明了这两个函数之间的不同。

$i = 1;
while ($i < 3) {
 require("somefile.$i");
 $i++;
}

  在这段代码中,每一次循环的时候,程序都将把同一个文件包含进去。很显然这不是程序员的初衷,从代码中我们可以看出这段代码希望在每次循环时,将不同的文件包含进来。如果要完成这个功能,必须求助函数include():

$i = 1;
while ($i < 3) {
 include("somefile.$i");
 $i++;
}

  6.注意echo和print的区别

  PHP中echo和print的功能也基本相同,但是两者之间也有细微差别。在PHP代码中可以把print作为一个普通函数来使用,例如执行下面的代码后变量$res的值将为1。

$ret = print "Hello World";

This means that print can be used in some complex expressions, but echo cannot. Similarly, the echo statement in the code runs slightly faster than the print statement because the echo statement does not require any value to be returned.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/317372.htmlTechArticlePHP is an efficient network programming language. Due to its advantages of flexible writing and fast operation, it has quickly become the Web The language of choice for programmers. An authoritative survey not long ago showed that now...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn