PHP tutorial. experience and skills (medium)

Source: Internet
Author: User

Introduction: This is a PHP tutorial. A detailed page of experience and skills (in middle). It introduces PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 326952 'rolling = 'no'>

Ten advanced PHP skills (medium)

3. Files are our friends

Regardless of the size of the website you are developing, you should be aware of the importance of code reuse, whether the code is HTML or PHP code. For example, you must change the footer containing the copyright information at least once a year. If your website contains 1000 pages, modifying the footer once a year is also annoying.

In PHP, there are at least a few functions that can help you reuse code. The functions used depend on the code you reuse. The main functions are:

* Include () and include_once ()

* Require () and require_once ()

The include () function contains and computes the given file. For example:

Include ('/home/ME/myfile ');

Any code in the include file is executed within the scope of the Code that appears in include (). You can use include () and fopen () together to include static files on your own server, include the target file on another server.

Include_once () has the same function as include (). The difference between them is that it checks whether the code in a file is included in an existing script. If the Code already exists, it will not be included again.

The require () function replaces itself with the content of a given file, which occurs during PHP engine code compilation rather than during execution, unlike include () the calculation is performed first. The require () function is more used in static elements, while the include () function is more used in dynamic elements. Similar to include_once (), require_once () first checks whether the given code has been inserted. If the Code already exists, it is no longer inserted.

To learn more about the content, I prefer to use the require function in copyright information, static text, and other elements that do not contain variables, or those that depend on other scripts being executed. For example:

<HTML>

<Head> <title> something </title>

<Body>

[A lot of content]

<?

// Insert copyright

Require ('/home/ME/mycopyright ');

?>

</Body>

</Html>

On the other hand, I often use include () to control many functions at the beginning of a file:

<?

// Obtain the function library

Include ('/home/ME/myfunctions ');

// Do PHP things with my functions?>

<HTML>

<Head> <title> something </title>

<Body>

[A lot of content]

</Body>

</Html>

The next question should be "Where are the include and require files ?", A simple answer to this question is, "anywhere in the system ." If your code contains a database connection with a user name and password, you will not put them in the root directory of the document to be open to everyone.

Stored ded or required files can be stored anywhere on the system, as long as users on the system where PHP runs can access these files, you can make these files have any suffix, or do not use a suffix.

Using include () and require () to externalize the elements on the website is a common phenomenon, and it brings great convenience to you when you need to upgrade the website.

Iv. php and file system maintenance

PHP has many functions related to the file system. These functions can not only open files, but also display contents in directories, move files, and other functions, many people even use PHP to develop Internet-based file resource managers.

File Path explanation: in Windows, you can use the/and \ symbols in the path, while in other operating systems, you can only use the/symbol. For the sake of consistency, we use the/symbol in a unified manner.

The following script example shows a directory list with annotations included in the Code:

<? /* Store the full path name of the directory to be read into a variable named $ dir_name. */

$ Dir_name = "/home/ME /";

/* Create a handle whose value is the result of opening a given directory */

$ Dir = opendir ($ dir_name );

/* Create a text block to place the list element (File Name )*/

$ File_list = "<ul> ";

/* Use a while statement to read all the elements in the opened directory. If the file name is not ".." or "..", the name in the list is displayed */

While ($ file_name = readdir ($ DIR )){

If ($ file_name! = ".") & ($ File_name! = "..")){

$ File_list. = "<li> $ file_name ";

}

}

$ File_list. = "</ul> ";

/* Close the opened directory and end the PHP module */

Closedir ($ DIR );

?>

<! -- Start your HTML -->

<HTML>

<Head>

<Title> directory listing </title>

</Head>

<Body>

<! -- Use PHP to print the name of the directory you read -->

<P> files in: <? Echo "$ dir_name";?> </P>

<! -- Use PHP to print the Directory Listing -->

<? Echo "$ file_list";?>

</Body>

</Html>

Now we have a directory list. Note that to read a file (which will be explained later) or directory, users on the PHP running system must have at least the permission to read the file.

The following is an example of how to copy an object:

<? /* Assign the full path of the original file to a variable named $ original, assign the full path of the copied file to a variable named $ copied */

$ Original = "/home/ME/mydatabasedump ";

$ Copied = "/archive/mydatabasedumo_1010 ";

/* Use the copy () function to copy the original file. If the copy is not completed, an error message is displayed */

@ Copy ($ original, $ copied) or die ("couldn't copy file .");

?>

This example is a prototype of a file backup system. When this script runs, it copies the file to a different location for saving. You can modify the daemon to execute it at the specified time of the day without user intervention.

If you have installed Lynx on the system, you can create a daemon entry to access this file. Accessing this file will run this script and create a copy file, the following example runs the script at five o'clock A.M. and then closes Lynx:

0 5 *** [username] Lynx-dump http: // localhost/copyfile. php 1>/dev/null 2> & 1

If you are running a CGI version of PHP, you can skip the lynx section and directly call the binary file:

0 5 *** [username] PHP/path/to/copyfile. php 1>/dev/null 2> & 1

5. Rich Array Functions

PHP 4.0 adds 30 new functions related to the number of groups. Some common functions can determine whether an array contains an element and count the elements in an array, add or delete elements in the array or sort the elements in the array.

If there is a large array and you need to find out whether it contains a specific element, you can use in_array (). The following example shows "not found in this array", because Albert is searched in an array named $ namesarray, and such an element does not exist in the $ namesarray array.

<? $ Namesarray = array ("Joe", "Jane", "Bob", "Mary", "Paul", "Eddie", "John ");

$ Lookingfor = "Albert ";

If (in_array ($ lookingfor, $ namesarray )){

Echo "you 've got it! ";

} Else {

Echo "not found in this array! ";

}

?>

If you change the value of $ lookingfor to Mary, you will get "you 've got it !" Because Mary is an element in the $ namesarray array.

To count the number of elements in an array, simply use the count () function:

<? $ Namesarray = array ("Joe", "Jane", "Bob", "Mary", "Paul", "Eddie", "John ");

$ COUNT = count ($ namesarray);?>

The returned $ Count value is 7.

You can add elements at the beginning or end of an array, or use array_merge () to create a new array containing two or more elements in the array, the order of elements is arranged in the specified order. If the original array is out of order, it needs to be re-ordered after merging.

We can first use array_push () to add an element at the end of the array:

<? /* Create an array */

$ Fruitarray = array ("apple", "orange", "banana", "Kiwi", "Pear ");

/* Add elements to the array */

Array_push ($ fruitarray, "grape", "pineapple", "tomato ");

/* Display each element and its serial number */

While (List ($ key, $ value) = each ($ fruitarray )){

Echo "$ key: $ value <br> ";

}

?>

Run the above program and you will get the following results:

0: Apple

1: Orange

2: banana

3: Kiwi

4: Pear

5: Grape

6: pineapple

7: Tomato

If you need to add elements at the beginning of the array, the code is similar to the above Code. The only difference is that array_unshift () must be used instead of array_push ().

<?

/* Create an array */

$ Fruitarray = array ("apple", "orange", "banana", "Kiwi", "Pear ");

/* Add elements to the array */

Array_unshift ($ fruitarray, "grape", "pineapple", "tomato ");

/* Display each element and its serial number */

While (List ($ key, $ value) = each ($ fruitarray )){

Echo "$ key: $ value <br> ";

}

?>

Run the above program and you will get the following results:

0: Grape

1: pineapple

2: Tomato

3: Apple

4: Orange

5: banana

6: Kiwi

7: Pear

The array_merge () function combines two or more arrays into an array.

<? /* Create the first array */

$ Fruitarray = array ("apple", "orange", "banana", "Kiwi", "Pear ");

/*/Create the second array */

$ Vegarray = array ("carrot", "green beans", "asparagus", "artichoke", "Corn ");

/* Combine the two numbers into an array */

$ Goodfoodarray = array_merge ($ fruitarray, $ vegarray );

/* Display each element and its serial number */

While (List ($ key, $ value) = each ($ goodfoodarray )){

Echo "$ key: $ value <br> ";

}

?>

Run the above script to get the following results:

0: Apple

1: Orange

2: banana

3: Kiwi

4: Pear

5: carrot

6: green beans

7: Asparagus

8: Artichoke

9: Corn

Now we know how to add elements and merge arrays. Let's take a look at how to delete elements from an array. You can use the array_pop () function to delete an element from the end of an array. The array_shift () function can be used to delete an element from the beginning of an array. Although an element is deleted from the Array Using array_pop () or array_shift (), you can use this element as a variable.

Use array_pop () to delete an element from the end of an array:

<?

/* Create an array */

$ Fruitarray = array ("apple", "orange", "banana", "Kiwi", "Pear ");

/* Delete an element from the end of the array */

$ Popped = array_pop ($ fruitarray );

/* Display the content of the deleted array and the elements you deleted */

While (List ($ key, $ value) = each ($ fruitarray )){

Echo "$ key: $ value <br> ";

}

Echo "<br> and finally, in $ popped: $ popped ";

?>

Run the above script to get the following results:

0: Apple

1: Orange

2: banana

3: Kiwi

And finally, in $ popped: Pear

Let's discuss an example of deleting an element from the end of an array:

<?

/* Create an array */

$ Fruitarray = array ("apple", "orange", "banana", "Kiwi", "Pear ");

/* Delete an element from the beginning of an array */

$ Shifted = array_shift ($ fruitarray );

/* Display the content of the deleted array and the elements you deleted */

While (List ($ key, $ value) = each ($ fruitarray )){

Echo "$ key: $ value <br> ";

}

Echo "<br> and finally, in $ shifted: $ shifted ";

?>

Run the preceding script and the following result is displayed:

0: Orange

1: banana

2: Kiwi

3: Pear

And finally, in $ shifted: Apple

There are also several functions that can sort the elements in the array, but here we will only briefly introduce the basic sorting function to explain the sorting process:

<? /* Create an array */

$ Fruitarray = array ("apple", "orange", "banana", "Kiwi", "Pear ");

/* Sort the array */

Sort ($ fruitarray );

/* Display each element and its serial number */

While (List ($ key, $ value) = each ($ fruitarray )){

Echo "$ key: $ value <br> ";

}

?>

Run the preceding script to obtain the following display result:

0: Apple

1: banana

2: Kiwi

3: Orange

4: Pear

"Php tutorial. experience and skills (medium)" more articles

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/326952.html pageno: 13.

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.