PHP Master of the road (i)

Source: Internet
Author: User
Tags add explode functions sql mysql in variables php and variable
PHP is an efficient network programming language, because it has the advantages of flexible writing, fast running and so on, quickly become the preferred language of web programmers. A recent authoritative survey showed that 31.6% of websites now use PHP as the primary server-side programming language.
But it's not easy to be a master of PHP programming. Not as many people imagine, as long as you can quickly write a few simple code to solve a complex problem is the Master of PHP programming, the real PHP master will need to consider more other issues. The following three guidelines are guidelines that a mature PHP programmer should follow first in programming.
1. Laziness is gold
2. Write Beautiful Code
3. Pursue the speed of the program, not the speed of programming
One, laziness is gold
Do you want to be a lazy programmer? That's a strange idea! Because the busiest person in the world is probably a computer programmer. But it's because programmers are so busy that they should learn to slack off when they're programmed.
For a programmer, there are two ways to be lazy: first, boldly use someone else's program code, and incorporate that code into your own program or project. The second is to write some useful code to build a function library, in the future to write programs can be conveniently picked, save a lot of duplication of labor, natural can be lazy a little.
Both of these lazy methods are perfect for PHP programmers.
First, PHP is a language of birth and growth in a free and open environment. Around the world, there are tens of thousands of programmers who have been struggling for the perfection of PHP and are willing to share their ingenuity and code with others. You can find a lot of good program code from PHP websites, mailing lists and newsgroups every day. In this way, I am not encouraging you to wait for others to write code for you, but you can "stand on the shoulders of great men", fully develop "copycat", smart application of other people's program code can save you a lot of time. Second, in PHP, you can easily build your own library of functions, which can save you a lot of trouble when you write your program later.
The following author introduces a few general functions, some of which come from some open source projects on the Internet, some are selected from the mailing list. If you can add them to your own library of functions, sooner or later you will find yourself benefiting.
1. Common Database processing functions
One of the advantages of PHP, compared to other CGI functions, is that it has a powerful database processing capability. However, in PHP, some specific functions are used for different databases, and there is a lack of common database processing functions. This greatly reduces the program code portability, which also for beginners programming friends brought a lot of inconvenience.
On the web, many programmers solve this problem by encapsulating classes. They write a unified function to handle any popular database?? Whether it's a popular MySQL in the Linux World or a widely popular SQL Server on the Windows platform. Personally, I like to use these functions, because you can directly use some simple functions such as "Query", "Next_record", and do not need to consider the database connection, database handle these complex things, and do not need to consider what kind of database.
If you need these functions, you can access them by visiting several of the following URLs:
http://phplib.netuse.de/
Http://phpclasses.UpperDesign.com/browse.html/package/20
http://phpdb.linuxbox.com/
2. Variable Debug function
PHP program debugging has always been a headache, it is not like VB and other high-level languages such as the integration of the compilation debugging environment, and do not want to Perl that can be in Linux or DOS environment directly run. In fact, we can complete the debugging of PHP by using the Echo statement flexibly.
The following functions allow you to view 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. = "    ";
}
$str. = $var. ==>;
$str. = 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 (the list (, $var) = each ($object->persistent_slots)) {
for ($i = 0; $i < $column; $i++) {
$str. = "    ";
}
Global $$var;
$str. = $var. ==>;
$str. = Ss_as_string ($$var, column+1). "
n ";
}
for ($i = 0; $i < $column; $i++) {
$str. = "    ";
}
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. ")";
}
}
When needed, simply add the following code to the program to view the types and values of variables (including arrays and objects) used in the program:
echo ss_as_string ($my_variable);
Using the following statement, we can directly view the values of all variables in the program:
echo ss_as_string ($globals);
3. Functions to control log information
Another important way to debug a PHP program is to view log information. If you can easily control the log information level and log information display content, will bring more convenience to the program debugging. The following functions can be easily implemented with this function.
$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]) {
Do not display log information
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);
}
In the above function, there are four log-level variables. When running a PHP program, log information can be recorded and displayed only if the log level is below the preset level value. For example, add one of the following statements to your program:
Ss_log_set_level (INFO);
Then, when running the PHP program, only the error and info level log information can be recorded and displayed, debug-level information is ignored. In addition, we can also set the display of the information content, the statement is as follows:
Ss_log (Error, "Testing level Error");
Ss_log (Info, "testing level info");
Ss_log (Debug, "Testing Level Debug");
You can also use the following statement to clear the log information at any time:
Ss_log_reset ();
4. Speed Test function

To optimize the code, we need a way to test the running time of the code to select the best code. The following function can test the time that is 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 it's easy to check the execution time of any piece of code, even if we can use multiple timers at the same time, just use a few of these functions to set different parameters as the name of the timer.
5. Debugging and optimizing the operation of the database
For a database, running speed is critical. Although many books and articles teach a quick way to run a database, all methods have to be tested in practice. Here we will combine the query () function in the Phplib function library with the functions described above, and write the new query () function, which increases the monitoring function of running time compared to the previous 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;
} (to be continued)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.