찾다
php教程php手册10个不常见却非常有用的PHP函数

10个不常见却非常有用的PHP函数

Jun 13, 2016 am 10:45 AM
phpsys기능할 수 있다용적시스템얻다

 

1. sys_getloadavg()函数

 

sys_getloadavt()可以获得系统负载情况。该函数返回一个包含三个元素的数组,每个元素分别代表系统再过去的1、5和15分钟内的平均负载。

 

与其让服务器因负载过高而宕掉,不如在系统负载很高时主动die掉一个脚本,sys_getloadavg()就是用来帮你实现这个功能的。 不过很遗憾,该函数在windows下无效。

 

2. pack()函数

 

Pack()能将md5()返回的32位16进制字符串转换为16位的二进制字符串,可以节省存储空间。

 

3. cal_days_in_month()

 

cal_days_in_month()能够返回指定月份共有多少天。

 

4. _()函数

 

WordPress开发者经常能见到这个函数,还有_e()。这两个函数功能相同,与gettext()函数结合使用,能实现网站的多语言化。具体可参见PHP手册的相关部分介绍。

 

5. get_browser()函数

 

在发送页面前先看看用户的浏览器都能做些什么是不是挺好?get_browser()能获得用户的浏览器类型,以及浏览器支持的功能,不过首先你需要一个php_browscap.ini文件,用来给函数做参考文件。

 

要注意,该函数对浏览器功能的判断是基于该类浏览器的一般特性的。例如,如果用户关闭了浏览器对JavaScript的支持,函数无法得知这一点。但是在判断浏览器类型和OS平台方面,该函数还是很准确的。

 

6. debug_print_backtrace()函数

 

这是一个调试用的函数,能帮助你发现代码中的逻辑错误。要理解这个函数,还是直接看个例子吧:

 

 

$a = 0;

function iterate() {

global $a;

if( $a

recur();

echo $a . “, “;

}

function recur() {

global $a;

$a++;

// how did I get here?

echo “\n\n\n”;

debug_print_backtrace();

if( $a

iterate();

}

iterate();

# OUTPUT:

#0 recur() called at [C:\htdocs\php_stuff\index.php:8]

#1 iterate() called at [C:\htdocs\php_stuff\index.php:25]

#0 recur() called at [C:\htdocs\php_stuff\index.php:8]

#1 iterate() called at [C:\htdocs\php_stuff\index.php:21]

#2 recur() called at [C:\htdocs\php_stuff\index.php:8]

#3 iterate() called at [C:\htdocs\php_stuff\index.php:25]

#0 recur() called at [C:\htdocs\php_stuff\index.php:8]

#1 iterate() called at [C:\htdocs\php_stuff\index.php:21]

#2 recur() called at [C:\htdocs\php_stuff\index.php:8]

#3 iterate() called at [C:\htdocs\php_stuff\index.php:21]

#4 recur() called at [C:\htdocs\php_stuff\index.php:8]

#5 iterate() called at [C:\htdocs\php_stuff\index.php:25]

 

7. metaphone()函数

 

这个函数返回单词的metaphone值,相同读音的单词具有相同的metaphone值,也就是说这个函数可以帮你判断两个单词的读音是否相同。不过对中文就无效了。。。

 

8. natsort()函数

 

natsort()能将一个数组以自然排序法进行排列,直接看个例子吧:

 

 

 

 

$items = array(

“100 apples”, “5 apples”, “110 apples”, “55 apples”

);

// normal sorting:

sort($items);

print_r($items);

# Outputs:

# Array

# (

# [0] => 100 apples

# [1] => 110 apples

# [2] => 5 apples

# [3] => 55 apples

# )

natsort($items);

print_r($items);

# Outputs:

# Array

# (

# [2] => 5 apples

# [3] => 55 apples

# [0] => 100 apples

# [1] => 110 apples

# )

 

 

9. levenshtein()函数

 

Levenshtein()告诉你两个单词之间的“距离”。它告诉你如果想把一个单词变成另一个单词,需要插入、替换和删除多少字母。

 

看个例子吧:

 

 

 

 

$dictionary = array(

“php”, “javascript”, “css”

);

$word = “japhp”;

$best_match = $dictionary[0];

$match_value = levenshtein($dictionary[0], $word);

foreach($dictionary as $w) {

$value = levenshtein($word, $w);

if( $value

$best_match = $w;

$match_value = $value;

}

}

echo “Did you mean the ‘$best_match’ category?”;

 

 

10. glob()函数

 

glob()会让你觉得用opendir(), readdir()和closedir()来寻找文件非常蠢。

 

 

 

 

foreach (glob(“*.php”) as $file)

echo “$file\n”;

 

摘自 lsoftwolf blog

성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

Video Face Swap

Video Face Swap

완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

뜨거운 도구

Atom Editor Mac 버전 다운로드

Atom Editor Mac 버전 다운로드

가장 인기 있는 오픈 소스 편집기

에디트플러스 중국어 크랙 버전

에디트플러스 중국어 크랙 버전

작은 크기, 구문 강조, 코드 프롬프트 기능을 지원하지 않음

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

안전한 시험 브라우저

안전한 시험 브라우저

안전한 시험 브라우저는 온라인 시험을 안전하게 치르기 위한 보안 브라우저 환경입니다. 이 소프트웨어는 모든 컴퓨터를 안전한 워크스테이션으로 바꿔줍니다. 이는 모든 유틸리티에 대한 액세스를 제어하고 학생들이 승인되지 않은 리소스를 사용하는 것을 방지합니다.

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.