Embedded language PHP Application tips seven _php Tutorials

Source: Internet
Author: User
Tags getcolor wubi
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:

 
  
  
  1. 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.

 
 
    1. $string = "Connect to the Sadie site point" ;
    2. //Note: A space or carriage return is required after the connection.
    3. $string = Eregi_replace ("/HTTP ([^, rn]*)", " ", $ String);
    4. $string = Eregi_replace ("ftp://([^, rn]*)", " ", $ String);
    5. Print $string ;
    6. ?>

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:

 
 
  1. function GetColor ()
  2. {
  3. Static $colorvalue; //define a static variable
  4. if ($colorvalue= ="#eeeeee")
  5. $colorvalue = "#F5F5F5" ;
  6. Else $colorvalue="#eeeeee";
  7. return ($colorvalue);
  8. }
  9. Print (");//Output 10 lines below n"
  10. for ($i=0; $i <10; $i ++)
  11. {
  12. $bcolor =getcolor (); //Change background color
  13. Print ("n");
  14. Print (" $i n");
  15. Print ("");
  16. }
  17. Print (" n");
  18. ?>

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.

 
  
  
  1. if (! Empty Empty ($GLOBALS["Foodtails"]["globaldefine " return ;
  2. $GLOBALS ["Foodtails"] ["Globaldefine"] = True ;
  3. 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.

 
  
  
  1. if ($lastAction>$userLastAction) {
  2. $userLastAction
  3. }

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.

 
  
  
  1. $res =mysql_query ("Update download set downnum= (downnum+1) where id="$id"" , $db);
  2. 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 ...

  • 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.