Lazy is gold introduces several common PHP functions page 1/2

Source: Internet
Author: User


However, it is not easy to become a PHP programming expert. As many people think, as long as you can write a few simple Code To solve a complicated problem, it is the PHP programming experts who need to consider more other problems. The following three principles are a mature PHP Program Guidelines that should be followed first in the compilation process.
1. Laziness is gold
2. write beautiful code
3. The pursuit of program speed rather than programming speed
1. Laziness is gold
Be a lazy programmer? This is a strange idea! This is because the busiest person in the world may be a computer programmer. But it is because programmers are too busy that they should learn to be lazy during programming.
For a programmer, there are two lazy Methods: First, boldly use the code of someone else's program to integrate the code into your own program or project. The second is to write some useful code to create a function library, which can be easily written in the future, saving a lot of repetitive work, so you can naturally be a little lazy.
Both of these methods are very suitable for PHP programmers.
First of all, PHP is a language born and grown in a free and open environment. There are thousands of programmers around the world who are constantly striving for PHP perfection and are willing to share their talents and code with others. Every day, you can find a lot of excellent program code from some PHP websites, email lists, and newsgroups. In this way, I do not encourage you to wait all day for others to write code for you, but you can "stand on the shoulders of great people" and fully carry forward "ism ", smart Application of others' program code can save you a lot of time. Second, in PHP, you can easily create your own function library, which saves you a lot of trouble when writing programs later.
Below I will introduce several general functions, some of which are open from the Internet. Source code Items, some of which are selected from the mail list. If you can add them to your own function library, sooner or later you will find yourself benefiting.

1. Common Database processing functions

Compared with other CGI functions, one of the advantages of PHP is its powerful database processing capability. However, in PHP, some specific functions are used for different databases for special processing, and common database processing functions are missing. This greatly reduces the portability of program code, which also brings a lot of inconvenience to beginners.
On the Internet, many programmers solve this problem through encapsulation. They have compiled unified functions to deal with any popular databases-MySQL, which is popular in the Linux World, and sqlserver, which is widely used on Windows platforms. I personally like to use these functions, because some simple functions such as "query" and "next_record" can be used directly, you do not need to consider the complex things such as database connection and database handle, and do not need to consider what type of database is used.
If you need these functions, you can access the following urls:
Http://phplib.netuse.de/
Http://phpclasses.UpperDesign.com/browse.html/package/20
Http://phpdb.linuxbox.com/

2. Variable debugging Functions

Debugging PHP programs has always been a headache. It does not have an integrated compilation and debugging environment like other advanced languages such as VB, and do not want Perl to run in a Linux or DOS environment. In fact, we can use echo statements flexibly to debug PHP.
The following functions allow you to view the types and values of any variables 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 ;";
}
$ Str. = $ var. =>;
$ Str. = ss_as_string ($ Val, $ column + 1 )."
N ";
}
For ($ I = 0; $ I <$ column; $ I ++ ){
$ Str. = "& 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 ;";
}
Global $ var;
$ Str. = $ var. =>;
$ Str. = ss_as_string ($ var, column + 1 )."
N ";
}
For ($ I = 0; $ I <$ column; $ I ++ ){
$ Str. = "& 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 .")";
}
}
When necessary, simply add the following code to the program to view the types and values of the variables used in the Program (including arrays and objects:
Echo ss_as_string ($ my_variable );
Using the following statement, we can directly view the values of all the variables in the program:
Echo ss_as_string ($ globals );

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.