Experience Summary PHP application tips _php Tutorial

Source: Internet
Author: User
Tags getcolor
After a long period of work accumulation, summed up some of the PHP application skills, here to share to everyone. PHP (hypertext Preprocessor) is a kind of HTML embedded language, is also the most popular web programming language. 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.

1, PHP application skills to determine whether 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 ')) {
  2. Die (' This host does not currently support GD graphics module ');
  3. }
  4. ?>

Similarly, we can use similar code to test whether modules such as MSSQL,OCI are supported.

2, PHP application skills in the string to change the URL into a hyperlink

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 Sadie http://www.ccidnet.com Site" ;
  2. Note: A space or carriage return is required after the connection.
  3. $ string = Eregi_replace ("HTTP//([^, rn]*)", " href=\0 tarrget=_blank >\0 ", $string);
  4. $ string = Eregi_replace ("ftp://([^, rn]*)", " href=\0 target=_blank >\0 ", $string);
  5. Print $string;
  6. ?>

3, PHP application skills in PHP processing 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:Switch。 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, PHP application techniques using static implementation of the table color interlaced display

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 ("border=1>n");//output 10 lines below
  10. For ($i=0; $i< ; $i + +)
  11. {
  12. $ Bcolor = GetColor ();//change background color
  13. print (" bgcolor= $bcolor >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 that the variable is defined. The approach is simple, similar to C. Just this global variable I recommend using [' User_packages '] [' headfilename '] naming rules.

 
  
  
  1. if (!empty ($GLOBALS [' foodtails '] [' globaldefine ']) return;
  2. $GLOBALS [' foodtails '] [' globaldefine '] = true;
  3. Class Foodtails {...
  4. };
  5. ?>

In addition, require_once "headfiles.php" is used as much as possible in the main program; To avoid duplicate references.


http://www.bkjia.com/PHPjc/446510.html www.bkjia.com true http://www.bkjia.com/PHPjc/446510.html techarticle after a long period of work accumulation, summed up some of the PHP application skills, here to share to everyone. PHP (hypertext Preprocessor) is a kind of HTML embedded language, it is now more popular ...

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