PHP(hypertext Preprocessor) is an HTML embedded language, but also the most popular web programming language at present. It supports a variety of back-end databases, with almost all of the current database systems covered. At the same time it contains the general language of mathematical operations, time processing, file system, string processing, travel processing and other functions, plus it is a free system, making cost and benefit ratio, almost equal to infinity.
Here are my web site with PHP, summed up a few tips, come out to treat everyone.
1. Determine if a function is supported
Since in PHP we can flexibly use to increase or decrease the PHP support module, so sometimes we use PHP before, always first determine whether a module is loaded, for example, to see if the GD graphics module is supported, you can use the following code:
- if (!function_exists ("imagecreate"))
Similarly, we can use similar code to test whether modules such as MSSQL,OCI are supported.
2. Change the URL into a hyperlink in a string
When submitting a form in a Web page, it's often a good thing to have URLs in the text of the submission, such as a profile, or automatically turn it into a hyperlink when it's displayed, just like when you edit a document with Word. The following code is a good implementation of its function.
- $string = "Connect to the Sadie site point" ;
- //Note: A space or carriage return is required after the connection.
- $string = Eregi_replace ("/HTTP ([^, rn]*)", " ", $ String);
- $string = Eregi_replace ("ftp://([^, rn]*)", " ", $ String);
- Print $string ;
- ?>
3. Use PHP to process multiple check boxes with the same name
If there is more than one check box with the same name in a form, there is only one value when committing to PHP, not a comma-delimited value like ASP. The workaround is to use the array. Add the name of the check box followed by [], for example: to:. This way, PHP will get an array called pp. In the submitted form, Count (PP) is used to determine the number of arrays, and then the array is processed separately.
The same applies to the multi-select problem of the dropdown box.
4. Using static to realize the color interlaced display of the table
We use PHP to query the data from the database, and output the results to the browser, if the result has a lot of rows, the table bgcolor (background color) If all is monochrome, the viewer will feel uncomfortable. So what do you do to make the table rows different colors? Take a look at the following code:
- function GetColor ()
- {
- Static $colorvalue; //define a static variable
- if ($colorvalue= ="#eeeeee")
- $colorvalue = "#F5F5F5" ;
- Else $colorvalue="#eeeeee";
- return ($colorvalue);
- }
- Print (");//Output 10 lines below
- for ($i=0; $i <10; $i ++)
- {
- $bcolor =getcolor (); //Change background color
- Print ("n");
- Print (" $i n");
- Print ("");
- }
- Print (" n");
- ?>
Description
A static variable is defined in this program $colorvalue meaning that after the function call ends, this variable $colorvalue also retains the value and does not disappear. When you call the GetColor () function again, the value of the variable $colorvalue is the value $colorvalue at the end of the last function call.
5, in PHP to avoid repeated reference method
As you know, in C, we can define a macro name with a # define, by checking whether the macro name is defined to determine whether the header file is referenced. There are similar problems in PHP, such as: A refers to B,c, b refers to C, and if no action is taken, C is referenced 2 times. This can lead to some strange problems. Workaround: Define a global variable and resolve the problem by checking to see if the variable is defined. The method is simple, similar to C. Just this global variable I recommend using the ["User_packages"] ["Headfilename"] naming convention.
- if (! Empty Empty ($GLOBALS["Foodtails"]["globaldefine " return ;
- $GLOBALS ["Foodtails"] ["Globaldefine"] = True ;
- class foodtails;
In addition, require_once "headfiles.php" is used as much as possible in the main program; To avoid duplicate references.
6. How to avoid repeated submissions of forms
When we do the website, often for some irrigation articles and worry. Sometimes, due to network conditions and other reasons, users do not know if the submission is successful, they will also submit the same form again, resulting in a duplicate submission of the form. There is an easy way to avoid repeating submissions for the same form. First, define a session variable to hold the commit sequence number of a form. Here I am defined as "$userLastAction". Then add a hidden variable to the form, set the value to $userlastaction+1:> Finally, determine whether the form has been submitted before processing the commit.
- if ($lastAction>$userLastAction) {
- $userLastAction
- }
The main principle of this technique is not to allow the user back to submit again, that is, back to modify the submission is not allowed, but still can not avoid ctrl-c/ctrl-v irrigation method.
7, download the number of downloaded files backfill
When we download software, we often see the number of download statistics for this software, which makes it convenient for webmasters to analyze the popularity of the software. The implementation of the principle is: in the back-end database to store the software's unique identity and download number, when users download software, first update the corresponding software download the number of downloads is 1, and then to download the official start to download files. Take a look at the following implementation code:
A, first build a data table download (back end with MySQL)
The download table contains the following two fields:
ID The unique identity of the downloaded file
Downnum download count, default value is 0
b, to achieve
Assume that it has been removed from the database: Description file name Wubi font; Download file name: wbzx.zip; ID value 2
The super-link code generated after processing is:
Wubi Font
The following code download.php handles the cumulative download count and locates the downloaded file based on the file name passed.
- $res =mysql_query ("Update download set downnum= (downnum+1) where id="$id"" , $db);
- Header ("Location: $filename");
http://www.bkjia.com/PHPjc/445806.html www.bkjia.com true http://www.bkjia.com/PHPjc/445806.html techarticle PHP (hypertext preprocessor) is a kind of HTML embedded language, is also the most popular web programming language. It supports a variety of backend databases, with almost all of the current covered ...