Home Backend Development PHP Tutorial PHP中鲜为人知的10个函数_php技巧

PHP中鲜为人知的10个函数_php技巧

May 17, 2016 am 08:48 AM
php function

PHP里有非常丰富的内置函数,很多我们都用过,但仍有很多的函数我们大部分人都不熟悉,可它们却十分的有用。这篇文章里,我列举了一些鲜为人知但会让你眼睛一亮的PHP函数。

levenshtein()

你有没有经历过需要知道两个单词有多大的不同的时候,这个函数就是来帮你解决这个问题的。它能比较出两个字符串的不同程度。

用法:

复制代码 代码如下:

$str1 = "carrot";
$str2 = "carrrott";
echo levenshtein($str1, $str2); //Outputs 2
?>


get_defined_vars()

这是一个在debug调试时非常有用的函数。这个函数返回一个多维数组,里面包含了所有定义过的变量。

用法:

复制代码 代码如下:

print_r(get_defined_vars());
?>


php_check_syntax()

这个函数非常的有用,可以用来检查PHP的语法是否正确。出于技术上的原因,从PHP 5.05开始,这个函数被删除了。

用法:

复制代码 代码如下:

$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()

这个函数用来拒绝浏览器端用户终止执行脚本的请求。正常情况下客户端的退出会导致服务器端脚本停止运行。

用法:

复制代码 代码如下:

ignore_user_abort();
?>


highlight_string()

当你想把PHP代码显示到页面上时,highlight_string()函数就会显得非常有用。这个函数会把你提供的PHP代码用内置的PHP语法突出显示定义的颜色高亮显示。这个函数有两个参数,第一个参数是一个字符串,表示这个字符串需要被突出显示。第二个参数如果设置成TRUE,这个函数就会把高亮后的代码当成返回值返回。

用法:

复制代码 代码如下:

highlight_string('');
?>


highlight_file

这是一个非常有用的PHP函数,它能返回指定的PHP文件,并按照语法语义用高亮颜色突出显示文件内容。其中的突出显示的代码都是用HTML标记处理过的。

用法:

复制代码 代码如下:

highlight_file("php_script.php");
?>


php_strip_whitespace

这个函数也跟前面的show_source()函数相似,但它会删除文件里的注释和空格符。

用法:

复制代码 代码如下:

echo php_strip_whitespace("php_script.php");
?>


get_browser

这个函数会读取browscap.ini文件,返回浏览器兼容信息。

用法:

复制代码 代码如下:

echo $_SERVER['HTTP_USER_AGENT'];
$browser = get_browser();
print_r($browser);
?>


memory_get_usage(),memory_get_peak_usage(),getrusage()

这些函数用来获取内存和CPU使用情况,memory_get_usage()函数返回内存使用量,memory_get_peak_usage()函数返回内存使用峰值,getrusage()返回CUP使用情况,在调试PHP代码性能时,这些函数会给你提供一些有用信息。但有一点请注意,在这些函数中Window上无效。

用法:

复制代码 代码如下:

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()

这两个函数用来压缩和解压字符串数据。它们的压缩率能达到50% 左右。另外的函数 gzencode() 和 gzdecode() 也能达到类似结果,但使用了不同的压缩算法。

用法:

复制代码 代码如下:

$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 orci,
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 in, 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);
?>

你是否也想到了还有其它很有用的函数?请在评论里分享出来!
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to optimize the lazy loading effect of images through php functions? How to optimize the lazy loading effect of images through php functions? Oct 05, 2023 pm 12:13 PM

How to optimize the lazy loading effect of images through PHP functions? With the development of the Internet, the number of images in web pages is increasing, which puts pressure on page loading speed. In order to improve user experience and reduce loading time, we can use image lazy loading technology. Lazy loading of images can delay the loading of images. Images are only loaded when the user scrolls to the visible area, which can reduce the loading time of the page and improve the user experience. When writing PHP web pages, we can optimize the lazy loading effect of images by writing some functions. Details below

How to reduce memory usage through php functions? How to reduce memory usage through php functions? Oct 05, 2023 pm 01:45 PM

How to reduce memory usage through PHP functions. In development, memory usage is a very important consideration. If a large amount of memory is used in a program, it may cause slowdowns or even program crashes. Therefore, reasonably managing and reducing memory usage is an issue that every PHP developer should pay attention to. This article will introduce some methods to reduce memory usage through PHP functions, and provide specific code examples for readers' reference. Use the unset() function to release variables in PHP. When a variable is no longer needed, use

PHP function introduction—curl_multi_getcontent(): Get the content of a cURL session PHP function introduction—curl_multi_getcontent(): Get the content of a cURL session Jul 26, 2023 pm 03:01 PM

PHP function introduction—curl_multi_getcontent(): Get the content of a cURL session. In PHP development, we often need to request data from other servers through the network. And cURL (ClientURL) is a powerful PHP extension library used for network communication in PHP. cURL provides a series of functions, one of which is curl_multi_getcontent(), which is used to obtain the content of a cURL session.

PHP Deprecated: Function ereg_replace() is deprecated - Solution PHP Deprecated: Function ereg_replace() is deprecated - Solution Aug 18, 2023 am 10:48 AM

PHPDeprecated: Functionereg_replace()isdeprecated-Solution When developing in PHP, we often encounter the problem of some functions being declared deprecated. This means that in the latest PHP versions, these functions may be removed or replaced. One common example is the ereg_replace() function. ereg_replace

Summary of methods for implementing image editing and processing functions using PHP image processing functions Summary of methods for implementing image editing and processing functions using PHP image processing functions Nov 20, 2023 pm 12:31 PM

PHP image processing functions are a set of functions specifically used to process and edit images. They provide developers with rich image processing functions. Through these functions, developers can implement operations such as cropping, scaling, rotating, and adding watermarks to images to meet different image processing needs. First, I will introduce how to use PHP image processing functions to achieve image cropping function. PHP provides the imagecrop() function, which can be used to crop images. By passing the coordinates and size of the cropping area, we can crop the image

Introduction to PHP functions: strtr() function Introduction to PHP functions: strtr() function Nov 03, 2023 pm 12:15 PM

PHP function introduction: strtr() function In PHP programming, the strtr() function is a very useful string replacement function. It is used to replace specified characters or strings in a string with other characters or strings. This article will introduce the usage of strtr() function and give some specific code examples. The basic syntax of the strtr() function is as follows: strtr(string$str, array$replace) where $str is the original word to be replaced.

How performant are PHP functions? How performant are PHP functions? Apr 18, 2024 pm 06:45 PM

The performance of different PHP functions is crucial to application efficiency. Functions with better performance include echo and print, while functions such as str_replace, array_merge, and file_get_contents have slower performance. For example, the str_replace function is used to replace strings and has moderate performance, while the sprintf function is used to format strings. Performance analysis shows that it only takes 0.05 milliseconds to execute one example, proving that the function performs well. Therefore, using functions wisely can lead to faster and more efficient applications.

Comparing PHP functions to functions in other languages Comparing PHP functions to functions in other languages Apr 10, 2024 am 10:03 AM

PHP functions have similarities with functions in other languages, but also have some unique features. Syntactically, PHP functions are declared with function, JavaScript is declared with function, and Python is declared with def. In terms of parameters and return values, PHP functions accept parameters and return a value. JavaScript and Python also have similar functions, but the syntax is different. In terms of scope, functions in PHP, JavaScript and Python all have global or local scope. Global functions can be accessed from anywhere, and local functions can only be accessed within their declaration scope.

See all articles