PHP Common functions

Source: Internet
Author: User
Tags fread readfile urlencode


The UrlEncode () function works by first converting Chinese characters to 16, and then adding an identifier% to each character.

The UrlDecode () function, in contrast to the UrlEncode () function, is used to decode an encoded URL string by converting a hexadecimal string to a Chinese character

PHP common functions;

1. Sort (); Sorts the elements in an array by letter (or number) in ascending order:

Cases:
<?php
$cars =array ("Volvo", "BMW", "SAAB");
Sort ($cars);


$numbers =array (3,5,1,22,11);
Sort ($numbers);

The result is: 1,3,5,11,22


?>


2. Rsort ();//descending sorting of arrays;

Instance

<?php
$cars =array ("Volvo", "BMW", "SAAB");
Rsort ($cars);
?>


<?php
$numbers =array (3,5,1,22,11);
Rsort ($numbers);
?>


3. Asort ();//The associative array is sorted in ascending order by value:

Cases:
<?php
$age =array ("Bill" = "+", "Steve" = "Notoginseng", "Peter" and "43");
Asort ($age);
?>

4. Ksort (); Sort ascending order according to key array;

Cases:
<?php
$age =array ("Bill" = "+", "Steve" = "Notoginseng", "Peter" and "43");
Ksort ($age);
?>


5. The ReadFile () function reads the file and writes it to the output buffer.
Cases:
<?php
echo ReadFile ("Webdictionary.txt");
?>

6. A better way to open a file is through the fopen () function. This function gives you more options than the ReadFile () function.
The first parameter of fopen () contains the file name that is opened, and the second parameter specifies the mode in which the file is opened. If the fopen () function fails to open the specified file, the following example generates a message:

Cases:
<?php
$myfile = fopen ("Webdictionary.txt", "R") or Die ("Unable to open file!");
Echo fread ($myfile, FileSize ("Webdictionary.txt"));
Fclose ($myfile);
?>

Pattern description
R opens the file as read-only. The file pointer starts at the beginning of the file.
W Open File is write-only. Delete the contents of the file or create a new file if it does not exist. The file pointer starts at the beginning of the file.
A open file is write-only. The existing data in the file is retained. The file pointer starts at the end of the file. Creates a new file if the file does not exist.
X create a new file as write-only. Returns FALSE and an error if the file already exists.
r+ Open the file as read/write, and the file pointer starts at the beginning of the file.
w+ Open the file for read/write. Delete the contents of the file or create a new file if it does not exist. The file pointer starts at the beginning of the file.
A + opens the file for read/write. The data already in the file will be retained. The file pointer starts at the end of the file. Creates a new file if it does not exist.
x+ Create a new file for read/write. Returns FALSE and an error if the file already exists.


7. The fread () function reads the open file.

The first parameter of Fread () contains the file name of the file to be read, and the second parameter specifies the maximum number of bytes to be read.

The following PHP code reads the "webdictionary.txt" file to the end:

Fread ($myfile, FileSize ("Webdictionary.txt"));

8. The fclose () function is used to close open files.

Note: It is a good programming habit to close all of the files after they are exhausted. You do not want to open a file that consumes your server resources.

Fclose () requires the name of the file to be closed (or a variable that has a file name):

<?php
$myfile = fopen ("Webdictionary.txt", "R");
Some code to be executed ....
Fclose ($myfile);
?>



9. The fgets () function is used to read a single line from a file.

The following example outputs the first line of the "Webdictionary.txt" file:
Instance

<?php
$myfile = fopen ("Webdictionary.txt", "R") or Die ("Unable to open file!");
Echo fgets ($myfile);
Fclose ($myfile);
?>

The feof () function checks to see if "End-of-file" (EOF) has been reached.

Feof () is useful for traversing data with unknown lengths.

The following example reads the "Webdictionary.txt" file row by line until End-of-file:
Instance

<?php
$myfile = fopen ("Webdictionary.txt", "R") or Die ("Unable to open file!");
Output single line until End-of-file
while (!feof ($myfile)) {
Echo fgets ($myfile). "<br>";
}
Fclose ($myfile);
?>


The fgetc () function is used to read a single character from a file.

The following example reads "Webdictionary.txt" files verbatim until End-of-file:
Instance

<?php
$myfile = fopen ("Webdictionary.txt", "R") or Die ("Unable to open file!");
Output single character until End-of-file
while (!feof ($myfile)) {
echo fgetc ($myfile);
}
Fclose ($myfile);
?>

PHP Common functions

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.