W3school PHP Tutorial (2) PHP advanced

Source: Internet
Author: User
1 Date () 1.1 Date () function syntax: date (format, timestamp) format: required. The format of the specified timestamp. Timestamp: Optional. The specified timestamp. The default value is the current date and time. 1.2 Time stamp (Timestamp) number of seconds since 1970.1.1 (00: 00: 00GMT) 1.3 Format date

1 Date ()

1.1 Date () function

Syntax: date (format, timestamp)

Format: required. The format of the specified timestamp.

Timestamp: Optional. The specified timestamp. The default value is the current date and time.

1.2 Timestamp)

Number of seconds since 1970.1.1 (00: 00: 00GMT)

1.3 format a date

The format parameter of the date () function specifies how to format the date/time.

D: Day in the month (01-31)

M: current month, in numbers (01-12)

Y: current year (four digits)

"; Echo date (" Y-m-d ") ;?> 1.4 Add timestamp

The timestamp parameter of the date () function specifies a timestamp

1.5 mktime () function

The mktime () function returns a Unix timestamp for a specified date.

Syntax: mktime (hour, minute, second, month, day, year, is_dst)

2. reference a file

2.1 include () functions

2.2 require () function

Unlike include (), the include () function generates a warning (but the script continues to execute), and The require () function generates a fatal error (fatal error) (The execution will be stopped when an error occurs)

 


3. file processing

3.1 open the file fopen () function

The first parameter contains the name of the file to be opened.

The second parameter specifies the mode used to open the file.

Mode
Description
 
R
Read-only. Start at the beginning of the file.
 
R +
Read/write. Start at the beginning of the file.
 
W
Write-only. Open and clear the file content. if the file does not exist, create a new file.
 
W +
Read/write. Open and clear the file content. if the file does not exist, create a new file.
 
A
Append. Open and write to the end of the file. if the file does not exist, create a new file.
 
A +
Read/append. Keep the file content by writing content to the end of the file
 
X
Write-only. Create a new file. If the file already exists, false is returned.
 
X +
Read/write. Create a new file. If the file already exists, false and an error are returned. If fopen () cannot open the specified file, 0 (false) is returned)
 

3.2 close the file fclose () function

3.3 check the End-of-file feof () function

3.4 fgets () function for reading files row by row

After this function is called, the file pointer is moved to the next row.

";}Fclose ($ file) ;?> 3.5 read file fgetc () function by character

After this function is called, the file pointer is moved to the next character.

4. file Upload
4.1 create a form

4.2 Upload a global array of scripts $ _ FILES

0) {echo "Error:". $ _ FILES ["file"] ["error"]."
";} Else {echo" Upload: ". $ _ FILES [" file "] [" name "]."
"; Echo" Type: ". $ _ FILES [" file "] [" type "]."
& Quot; echo & quot; Size: & quot;. ($ _ FILES [& quot; file & quot;] [& quot; size & quot;]/1024). & quot; Kb
"; Echo" Stored in: ". $ _ FILES [" file "] [" tmp_name "] ;}?> The first parameter of the $ _ FILES array is the input name of the form, and the second subscript can be:

Name: name of the uploaded file

Type: type of the file to be uploaded

Size: The size of the uploaded file.

Tmp_name: name of the temporary copy of the file stored on the server

Error: error code caused by file upload

4.3 Upload restrictions

The file size must be smaller than 20kb:

0) {echo "Error:". $ _ FILES ["file"] ["error"]."
";} Else {echo" Upload: ". $ _ FILES [" file "] [" name "]."
"; Echo" Type: ". $ _ FILES [" file "] [" type "]."
& Quot; echo & quot; Size: & quot;. ($ _ FILES [& quot; file & quot;] [& quot; size & quot;]/1024). & quot; Kb
"; Echo" Stored in: ". $ _ FILES [" file "] [" tmp_name "];} else {echo" Invalid file ";}}?>
4.4 save the uploaded file

Copy the temporary copy of the uploaded file created in the temporary folder in the preceding example to another location to prevent it from disappearing at the end of the script.

0) {echo "Error:". $ _ FILES ["file"] ["error"]."
";} Else {echo" Upload: ". $ _ FILES [" file "] [" name "]."
"; Echo" Type: ". $ _ FILES [" file "] [" type "]."
& Quot; echo & quot; Size: & quot;. ($ _ FILES [& quot; file & quot;] [& quot; size & quot;]/1024). & quot; Kb
"; Echo" Temp file: ". $ _ FILES [" file "] [" tmp_name "]."
"; If (file_exists (" upload /". $ _ FILES ["file"] ["name"]) {echo $ _ FILES ["file"] ["name"]. "already exists. ";} else {move_uploaded_file ($ _ FILES [" file "] [" tmp_name "]," upload /". $ _ FILES ["file"] ["name"]); echo "Stored in :". "upload /". $ _ FILES ["file"] ["name"] ;}} else {echo "Invalid file" ;}?>

5 Cookies
5.1 Create a cookie setcookie () function

Syntax: setcookie (name, value, expire, path, domain );

5.2 retrieve the cookie value

Use the isset () function to check whether the cookie has been set:

";} Else {echo" Welcome guest!
";}?> 5.3 delete a cookie

6 Session variables
6.1 start a session

6.2 store session variables

6.3 terminate a session

The 7 mail () function is used to send emails from scripts.
Syntax: mail (to, subject, message, headers, parameters)

Parameters
Description
 
To
Required. Specifies the email recipient.
 
Subject
Required. Specifies the subject of the email. Note: this parameter cannot contain any new line characters.
 
Message
Required. Defines the information to be sent. LF () should be used to separate the rows.
 
Headers
Optional. Specify additional titles, such as From, Cc, and Bcc. CRLF () should be used to separate additional titles.
 
Parameters
Optional. Specify additional parameters for the email sending program.
 

7.1 Example

?>

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.