10 little-known but very useful functions in PHP

Source: Internet
Author: User
Tags html tags memory usage php file php code cpu usage

PHP has very rich built-in functions, many we have used, but there are still a lot of functions we are not familiar with, but they are very useful. In this article, I've listed some little-known PHP functions that will brighten your eyes.

Levenshtein ()

Have you ever experienced the need to know how different two words are, this function is to help you solve the problem. It can compare the different degrees of two strings.

Usage:

$str 1 = "carrot";

$str 2 = "Carrrott";

Echo Levenshtein ($str 1, $str 2); Outputs 2

?>

Get_defined_vars ()

This is a very useful function when debug debugging. This function returns a multidimensional array that contains all the variables that have been defined.

Usage:

Print_r (Get_defined_vars ());

?>

Php_check_syntax ()

This function is very useful and can be used to check if the syntax of PHP is correct. For technical reasons, starting with PHP 5.05, this function was removed.

Usage:

$error _message = "";

$filename = "./php_script.php";

if (!php_check_syntax ($filename, & $error _message)) {

echo "Errors were found in the file $filename: $error _message";

} else {

echo "The file $filename contained no syntax errors";

}

?>

Ignore_user_abort ()

This function is used to deny the browser-side user the request to terminate the execution of the script. A client exit normally causes the server-side script to stop running.

Usage:

Ignore_user_abort ();

?>

Highlight_string ()

The highlight_string () function is useful when you want to display the PHP code on the page. This function will highlight the defined colors in the built-in PHP syntax for the PHP code you provide. This function has two arguments, and the first argument is a string that indicates that the string needs to be highlighted. If the second argument is set to True, the function returns the highlighted code as the return value.

Usage

Highlight_string (" );

?>

Highlight_file

This is a very useful php function, it can return the specified PHP file, and in accordance with syntax semantics highlighting the contents of the file with a highlight color. The highlighted code is processed with HTML tags.

Usage:

Highlight_file ("php_script.php");

?>

Php_strip_whitespace

This function is similar to the previous show_source () function, but it deletes the annotations and spaces in the file.

Usage:

Echo php_strip_whitespace ("php_script.php");

?>

Get_browser

This function reads the Browscap.ini file and returns browser-compatible information.

Usage:

echo $_server[' http_user_agent '];

$browser = Get_browser ();

Print_r ($browser);

?>

Memory_get_usage (), Memory_get_peak_usage (), Getrusage ()

These functions are used to obtain memory and CPU usage, the Memory_get_usage () function returns memory usage, the Memory_get_peak_usage () function returns the peak memory usage, getrusage () returns the cup usage, These functions give you some useful information when debugging PHP code performance. Note, however, that the windows on these functions are not valid.

Usage:

echo "Initial:". Memory_get_usage (). "Bytes n";

echo "Peak:". Memory_get_peak_usage (). "Bytes n";

$data = Getrusage ();

echo "User time:".

($data [' ru_utime.tv_sec '] +

$data [' ru_utime.tv_usec ']/1000000);

echo "System time:".

($data [' ru_stime.tv_sec '] +

$data [' ru_stime.tv_usec ']/1000000);

?>

Gzcompress (), gzuncompress ()

These two functions are used to compress and decompress string data. Their compression rate can reach about 50%. Other functions Gzencode () and Gzdecode () can achieve similar results, but use different compression algorithms.

Usage:

$string =

"Lorem ipsum dolor sit amet, consectetur

Adipiscing elit. Nunc ut elit id mi ultricies

Adipiscing. Nulla Facilisi. Praesent Pulvinar,

Sapien vel feugiat vestibulum, nulla dui pretium,

Non ultricies elit lacus quis ante. Lorem ipsum dolor

Sit amet, consectetur adipiscing elit. Aliquam

Pretium ullamcorper Urna quis iaculis. Etiam ac Massa

Sed turpis tempor luctus. Curabitur sed nibh eu elit

Mollis Congue. Praesent ipsum diam, Consectetur vitae

Ornare A, aliquam a nunc. In ID magna pellentesque

Tellus posuere adipiscing. Sed non mi metus, at Lacinia

Augue. Sed magna Nisi, ornare in mollis, mollis

Sed nunc. Etiam at Justo in Leo Congue mollis.

Nullam in Neque eget metus hendrerit scelerisque

EU non enim. Ut malesuada lacus eu nulla bibendum

ID euismod urna sodales. ";

$compressed = gzcompress ($string);

$original = gzuncompress ($compressed);

?>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.