Embedded language PHP Application skills Seven

Source: Internet
Author: User
Tags getcolor naming convention php tutorial wubi

PHP (PHP training PHP Tutorial) (hypertext Preprocessor) is an HTML-embedded language, but 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.

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 = "link to Sadie website 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? See the following code:

function GetColor ()

{

static $colorvalue;//define a static variable

if ($colorvalue = = "#eeeeee")

$colorvalue = "#F5F5F5";

else $colorvalue = "#eeeeee";

return ($colorvalue);

}

Print ("

n ");//output 10 lines below

for ($i =0; $i <10; $i + +)

{

$bcolor =getcolor ();//Change background color

Print ("

n ");

Print ("

n ");

Print ("

");

}

Print ("

$i

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 ($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 + +; Serial number plus 1

Working with form data

}

?>

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.

Connect MySQL Database code slightly

$res =mysql_query ("Update download set downnum= (downnum+1) where id=" $id "", $db);

Header ("Location: $filename");

?>

Embedded language PHP Application skills Seven

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.