PHP4 new Function Collection _php

Source: Internet
Author: User
Keywords collection function output foreach content buffer value
Tags setcookie
These functions allow you to control the content of your script output. It can be used in many different situations, especially if your script has output information that requires a new file header to be sent. The output control function does not affect the header information sent with header () or Setcookie (), only those that are similar to The data blocks of Echo () and PHP code are useful.

Example 1. Control output


Ob_start ();
echo "Hellon";

Setcookie ("CookieName", "Cookiedata");

Ob_end_flush ();

?>

In the example above, the output using echo () will be saved in the output buffer until Ob_end_flush () is called. What makes sense is that the content of the call to Setcookie () is successfully stored in the cookie without causing an error. (Under normal circumstances, you can not send the file header information to the user's browser after the data has been sent.)

The associated function header () and Setcookie ().

List of Korean series
flush-Flush Output Buffers
The content that is saved in the output buffer is sent to the browser

ob_start-opening the output buffer
In this way, all output information is not sent directly to the browser, but is stored in the output buffer.

ob_get_contents-returns the contents of the output buffer
If you want to process the output later, 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) the contents of the output buffer, close the output buffer

ob_end_clean-Delete (Discard) The contents of the output buffer, close the output buffer
If your program finds a problem with the output, you can discard any output and prevent the disclosure of some secret information.

ob_implicit_flush-turn direct refresh on or off
Once opened, each script output is sent directly to the browser, eliminating the need to call flush ()


Second, get the current directory


This is the new directory function of PHP4!

String getcwd (void)

Returns a string for the current script path!

Third, resolve the script timeout

In the PHP configuration/information There is a function to set the execution time of the script, as follows:

Set_time_limit
Configures the page for the longest execution time.

Syntax: void set_time_limit (int seconds);

return value: None

Function type: PHP system function

Content Description

This function is used to configure the longest execution time of the page. The default value is 30 seconds, and the max_execution_time variable configuration in php.ini, if configured to 0, is not limited to the longest time. The calculation is only started when the function is executed. For example, if the default 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 for the page is 45 seconds.

Usage examples:
My article search function due to the increase in the number of articles, often produce a time-out error, I changed the script execution time to 200 seconds after the situation greatly relieved!
Set_time_limit (200);
?>


Four, array traversal

· Foreach
In PHP4, there is a new loop statement, foreach, which is much like Perl and other languages, and you can give it an array to take out the array's values. It has the following two syntax, the second syntax is minor, but can be used as an extension of the first syntax.
foreach (array_expression as $value) statement
foreach (array_expression as $key = $value) statement
The first form of the loop, which assigns the value of the current element to the $value on each loop, and moves the inside pointer of the array backwards, so you'll see the next element in the next loop.
The second form of the loop is the same as the first one, and the difference is that it assigns the index value of the current element to the variable $key in each loop.
Note: When foreach first starts executing, it re-sets the inner pointer of the array to the first element of the array, meaning that you don't have to call reset () until you use foreach.
Note: The function of foreach is to copy, not array itself, so it does not change the array pointer
The following examples have the same functionality:
Reset ($arr);
while (list (, $value) = each ($arr)) {
echo "Value: $value

";
}

foreach ($arr as $value) {
echo "Value: $value

";
}
?>
The following examples also have the same functionality:
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 illustrates the use 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,
"Both" = 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.