Home  >  Article  >  Backend Development  >  Laziness is gold. Introducing several common functions in PHP Page 1/2_PHP Tutorial

Laziness is gold. Introducing several common functions in PHP Page 1/2_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:52:57662browse


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 gold
Do 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 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 a This is a headache. 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);

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/318844.htmlTechArticle但是,要成为一名PHP编程高手却并不容易。并不像很多人想象的那样,只要能够飞快地编写几条简单的代码去解决一个复杂的问题就是PHP编...
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