PHP Application Tips Seven

Source: Internet
Author: User
Tags arrays getcolor

PHP (hypertext Preprocessor) is a kind of HTML embedded language, but also is the 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, stroke processing functions, plus it is a free system, so that the cost and benefit ratio, almost equal to infinity.
The following is I use PHP to do the site, summed up some tips, take out to treat everyone.
1, to determine whether a function is supported
Because we can use PHP to increase or reduce the PHP support of the module, so sometimes we use PHP before, always will 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 ')) {
Die ("This host does not currently support the GD Graphics module");
In the same way, we can use similar code to test whether modules such as MSSQL,OCI are supported.
2, in the string to change the URL into a hyperlink
When submitting a form in a Web page, often some URLs, such as a personal home page, appear in the text of the submission, and if you automatically turn it into a hyperlink when you display it, it would be nice to be like a hyperlink when you edit a document in Word. The following code is a good implementation of its function.
$string = "Connection to the site";
Note: You need to have a space or a carriage return after the connection.
$string = Eregi_replace ("http://([^, rn]*)", "<a Href= tarrget=_blank> </a>", $string);
$string = Eregi_replace ("ftp://([^, rn]*)", "<a Href= target=_blank> </a>", $string);
Print $string;
3. Use PHP to handle multiple check boxes with the same name
If you have more than one check box with the same name in a form, there is only one value when you submit to PHP, not a comma-separated list of values, like an ASP. The solution is to take advantage of arrays. After the name of the check box is added [], for example: <input type= "checkbox" Name= "pp" value= "1" > Change to: <input type= "checkbox" Name= "pp[" "value=" 1 ">. This way, PHP will get an array called pp. In the submitted form, Count (PP) is used to determine the number of the array, and then the arrays are processed separately.
The same is true for multiple selection problems with the dropdown box.
4, the use of static to achieve the color of the table interlaced display
We use PHP to query the data from the database, and output the results to the browser, if the result has many rows, the table bgcolor (background color) If all is monochrome, the viewer will feel uncomfortable. So what do you do to make the rows in the table different colors? Take a look at the following code:
function GetColor ()
static $colorvalue;//define a statically variable
if ($colorvalue = = "#eeeeee")
$colorvalue = "#F5F5F5";
else $colorvalue = "#eeeeee";
return ($colorvalue);
Print ("<table border=1>n");//output 10 lines below
For ($i =0 $i <10; $i + +)
$bcolor =getcolor ()//Change background color
Print ("<tr bgcolor= $bcolor >n");
Print ("<td> $i </td> n");
Print ("</tr>");
Print ("</table> n");
A static variable $colorvalue defined in this program means that after the function call is finished, the variable $colorvalue also retains the value and does not disappear. When the GetColor () function is called again, the value of the variable $colorvalue is the $colorvalue value at the end of the last function call.
5, in PHP to avoid repeated references in the way
As you know, in C, we can use #define to define a macro name 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, if you do not take action, C will be cited 2 times. This may lead to some strange problems. Workaround: Define a global variable and solve the problem by checking whether the variable is defined. The approach is simple, like C.
Related Article

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