A collection of PHP4 new functions

Source: Internet
Author: User
Tags foreach array execution flush header php code reset variable
function one, output information control function








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 already output information and needs to send a new file header. The output control function does not affect file header information sent using header () or Setcookie (), only for blocks of data similar to echo () and PHP code.





Example 1. Control output





<?php





Ob_start ();


echo "Hellon";





Setcookie ("CookieName", "Cookiedata");





Ob_end_flush ();





?>





in the example above, the output with Echo () is saved in the output buffer until the Ob_end_flush () is invoked. What makes sense is that the contents of the call Setcookie () are stored successfully in the cookie without causing an error. (Normally, you cannot send file header information to the user's browser after the data has been sent.)





Correlation function Header () and Setcookie ().





Korean Series Table


flush-Flush Output Buffer

The contents of the
saved in the output buffer are sent to the browser





ob_start-Open Output Buffer


so that 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 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-the contents of the end (send) output buffer, turn off the output buffer





ob_end_clean-Delete (discard) The contents of the output buffer and turn off the output buffer


If your program finds that there is a problem with the output, you can discard all the output and prevent some secret information from leaking





ob_implicit_flush-turn on or off direct refresh

Once
is open, each script output is sent directly to the browser and no longer requires a call to flush ()








second, get the current directory








This is PHP4 's new catalog function!





string getcwd (void)





returns the string for the current script path!





Third, resolve script timeout





in PHP configuration/information has a function to set the execution time of the script, as follows:





Set_time_limit


Configure the page for the longest execution time.





syntax: void set_time_limit (int seconds);





return value: No





function Type: PHP system function





Content Description





This function configures the longest execution time for the page. The default value is 30 seconds, the Max_execution_time variable is configured in PHP.ini, and if configured to 0 it is not limited to the longest time. The calculation begins when the function is executed. For example, if the default is 30 seconds and 25 seconds have been executed before executing to the function, and the function is changed to 20 seconds, the maximum execution time for the page is 45 seconds.





Use Example:


my article search function due to the increase in the number of articles, often produces a timeout error, I changed the script execution time to 200 seconds after the situation greatly alleviated!








;?


Set_time_limit (200);


?>








four, array traversal





· foreach


in PHP4, there is a new loop statement foreach, which is very much like Perl and other languages, and you can give it an array to take out the values of the array. It has the following two syntaxes, 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 inner pointer of the array backwards, so that in the next loop you will see the next element.


the second form of the loop is the same as the first, 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 will reset 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 rather than array itself, so it does not change the array pointer


The following example features are the same:








<?php


Reset ($arr);


while (list (, $value) = each ($arr)) {


echo "Value: $value <br>


";


}





foreach ($arr as $value) {


echo "Value: $value <br>


";


}


?>








The following example features are also the same:








<?php


Reset ($arr);


while ($key, $value) = each ($arr)) {


echo "Key: $key; Value: $value <br>


";


}


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


echo "Key: $key; Value: $value <br>


";


}


?>








The following example will illustrate the use of foreach:








<?php


/* 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.