PHP4 new function highlights

zhaozj2021-02-11  252

First, the output information control function

These functions allow you to control your script output. You can use many different situations, especially when your script has already output information, you need to send file header. The output control function does not use header () or setcookie. ) The sending file header information has an impact, only the data blocks similar to ECHO () and PHP code.

Example 1. Control output

Ob_start ();

Echo "Hellon";

SetCookie ("Cookiename", "cookiedata");

OB_END_FLUSH ();

?>

In the above example, the output content using ECHO () will be saved in the output buffer until OB_END_FLUSH () is called. This means that the content calling setCookie () is successfully stored in the cookie Not causing errors. (Under normal circumstances, you can't send file header information to user browsers after having data has been sent.)

Related function header () and setcookie ().

Han number list

Flush - Refresh Output Buffer

Save the content in the output buffer will be sent to the browser

OB_Start - Open Output Buffer

Such all output information is not sent directly to the browser, but is saved inside the output buffer.

OB_GET_CONTENTS - Returns the contents of the output buffer

If you want to process the contents of the output, you can call this function to keep a backup

OB_GET_LENGTH - Returns the content length of the output buffer

OB_END_FLUSH - End (Send) Output Buffer content, turn off output buffer

OB_END_CLEAN - Delete (Abandon) Output Buffer, Turn off Output Buffer

If your program finds a problem with the output content, you can give up all output content, prevent some secret information from leaking

OB_IMPLICIT_FLUSH - Open or close Direct Refresh

After opening, each script output is sent directly to the browser, no longer need to call Flush ()

Second, get the current directory

This is a new directory function of PHP4!

String getCwd (void)

Returns a string for the current script path!

Third, solve the script timeout

There is a function of setting a script execution time in the configuration / information of the PHP, and the specific situation is as follows:

SET_TIME_LIMIT

Configure this page to execute time.

Syntax: void set_time_limit (int seconds);

Return value: no

Function type: PHP system function

Content description

This function is used to configure this page to execute time. The default is 30 seconds, in the Max_execution_time variable configuration in php.ini, if it is configured to be 0, no longer time. The calculation is started when the function is executed. For example, if the default is 30 seconds, 25 seconds have been executed before the function is executed, and this function is changed to 20 seconds, then the maximum execution time is 45 seconds.

Use example:

My article search function is often made of timeout errors due to the increase in the number of articles. I change the script to 200 seconds.

SET_TIME_LIMIT (200);

?>

Fourth, array traversal

· Foreach

In PHP4, a loop statement Foreach has added, it is very like Perl and other languages, you can give it an array to remove the value of the array. It has the following two syntax, the second syntax is more important, but can be used as an extension of the first syntax.

Foreach (Array_Expression As $ Value) Statement

Foreach (array_expression as $ key => $ value) Statement's first form of loop, which will assign the value of the current element to $ Value on each loop, move the internal pointer of the array, so in the next loop When you see the next element.

The second form of loops is the same, and the difference is that it assigns the index value of the current element to the variable $ KEY at each loop.

Note: When Foreach first starts executing, it will reset the internal pointer of the array to the first element of the array. It means that you don't have to call Reset () before using foreach.

Note: Foreach's function is replicated, not the array it itself, so it does not change the array pointer

The functions of the examples are the same:

RESET ($ ARR);

While (List (, $ Value) = Each ($ arr)) {

Echo "Value: $ Value

"

}

Foreach ($ arr as $ value) {

Echo "Value: $ Value

"

}

?>

The features of the examples are also the same:

RESET ($ ARR);

While (List ($ key, $ value) = Each ($ arr)) {

Echo "Key: $ Key; Value: $ VALUE

"

}

Foreach ($ arr as $ key => $ value) {

Echo "Key: $ Key; Value: $ VALUE

"

}

?>

The following example will explain the usage of Foreach:

/ * Foreach EXAMPLE 1: Value ONLY * /

$ a = array (1, 2, 3, 17);

Foreach ($ A $ V) {

Print "Current Value of $ A: $ V.

"

}

/ * Foreach Example 2: Key and Value * /

$ a = array (

"one" => 1,

"Two" => 2,

"three" => 3,

"seventeen" => 17

);

Foreach ($ A $ K => $ V) {

Print "$ A [$ K] => $ V.

"

}

?>

转载请注明原文地址:https://www.9cbs.com/read-4240.html

New Post(0)