PHP4 new functions

Source: Internet
Author: User
These functions allow you to control the content output by your script. it can be used in many different situations, especially when your script has output information, you need to send a new file header. the output control function does not affect the header information sent using header () or setcookie (). it only applies to data blocks similar to echo () and PHP code. example 1. these functions allow you to control the content output by your script. it can be used in many different situations, especially when your script has output information, you need to send a new file header. the output control function does not affect the header information sent using header () or setcookie (). it only applies to data blocks similar to echo () and PHP code.

Example 1. control output


Ob_start ();
Echo "Hellon ";

Setcookie ("cookiename", "cookiedata ");

Ob_end_flush ();

?>

In the preceding example, the output content using echo () is saved in the output buffer until ob_end_flush () is called (). it makes sense that the content that calls setcookie () is successfully stored in the cookie without causing errors. (normally, you cannot send the file header information to your browser after the data has been sent .)

Related function header () and setcookie ().

Korea list
Flush-refresh the output buffer
The content stored in the output buffer is sent to the browser.

Ob_start-open the output buffer
In this way, all output information is not directly sent to the browser, but saved in the output buffer.

Ob_get_contents-return the content of the output buffer
If you want to process the output content in the future, you can call this function to keep a backup.

Ob_get_length-return the content length of the output buffer

Ob_end_flush-end (SEND) output buffer content, disable the output buffer

Ob_end_clean-delete (discard) the output buffer and disable the output buffer.
If your program finds a problem with the output content, you can discard all output content to prevent leakage of some confidential information.

Ob_implicit_flush-refresh directly when it is enabled or disabled
After the script is opened, the output of each script is directly sent to the browser, and you do not need to call flush ()


II. obtain the current directory


This is the new directory function of php4!

String getcwd (void)

Returns the string of the current script path!

III. fixed script timeout

In php configuration/information, there is a function for setting the script execution time, as shown below:

Set_time_limit
Configure the longest execution time for this page.

Syntax: void set_time_limit (int seconds );

Return value: None

Function types: PHP system functions

Description

This function is used to configure the longest execution time of the page. The default value is 30 seconds. in php. ini, the max_execution_time variable is configured. if it is set to 0, the maximum time is not limited. Computing starts only when the function is executed. For example, if the default value is 30 seconds, and 25 seconds have been executed before the function is executed, and the function is changed to 20 seconds, the maximum execution time of the page is 45 seconds.

Instance used:
Because of the increase in the number of articles, my article search function often produces time-out errors. After I change the script execution time to 200 seconds, the situation is greatly mitigated!
Set_time_limit (200 );
?>


IV. array traversal

· Foreach
In PHP4, a loop statement foreach is added, which is like perl and other languages. you can give it an array to retrieve the array value. It has the following two syntaxes. The second syntax is secondary, but can be used as an extension of the first syntax.
Foreach (array_expression as $ value) statement
Foreach (array_expression as $ key => $ value) statement
In the first loop, it allocates the value of the current element to $ value and moves the internal pointer of the array backward. Therefore, in the next loop, you will see the next element.
The second loop is the same as the first one. The difference is that it assigns the index value of the current element to the variable $ key in each loop.
Note: When foreach starts execution for the first time, it will reset the internal pointer of the array to the first element of the array, which means that before using foreach, you do not have to call reset () again ().
Note: The foreach function is to copy instead of the array itself, so it does not change the array pointer.
The functions of the following examples are the same:
Reset ($ arr );
While (list (, $ value) = each ($ arr )){
Echo "Value: $ value

";
}

Foreach ($ arr as $ value ){
Echo "Value: $ value

";
}
?>
The functions of the following examples are 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 describes the usage of foreach:
/* Foreach example 1: value only */
$ A = array (1, 2, 3, 17 );
Foreach ($ a as $ 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 as $ k => $ v ){
Print "$ a [$ k] => $ v.
";
}

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.