The Road to PHP Master (1)
php is an efficient network programming language. Due to its advantages of flexible writing and fast running, it has quickly become the preferred language of 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 programming, not programming speed
1. Laziness is gold
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. Universal database processing function
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 have written 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 PHP programs has always been a headache. It does not have an integrated compilation and debugging environment like high-level languages such as VB, nor does it like Perl. It can be run directly in Linux or DOS environment. 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 < ss_as_string ($val, $column+1)."
n";
}
for ($i = 0; $i < $column; $i++){
$str .= " ";
}
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.速度测试函数
In order to optimize the code, we need a method that can test the running time of the code to select the optimal code. The following function can test the time required to run the code:
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;
}
Now you can easily check the execution time of any piece of code. We can even use multiple timers at the same time. We only need to set different parameters when using the above functions as The name of the timer will do.
5. Debugging and optimizing database operations
For databases, running speed is crucial. Although many books and articles teach methods for running databases quickly, all methods must be tested in practice. Next, we will combine the query() function in the PHPLib function library and the functions introduced above to write a new query() function. Compared with the original function, this function adds a running time monitoring function.
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;
The above is the content of the PHP Master’s Road (1). For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
