7个鲜为人知却超级有用的PHP函数_PHP教程

WBOY
Libérer: 2016-07-13 10:38:58
original
733 Les gens l'ont consulté

PHP有着众多的内置函数,其中大多数函数都被开发者广发使用。但也有一些同样有用却被遗忘在角落,本文将介绍7个鲜为人知功能却非常酷的函数。

1.highlight_string()

当需要在网页中展示PHP代码时,highlight_string()函数就显得非常有用。该函数通过PHP内置定义的颜色,返回函数中代码的高亮显示版本。

Usage:

<?php
highlight_string(&#39;<?php phpinfo(); ?>&#39;);
?>
Copier après la connexion
2.str_word_count()

这个函数可以方便的将输入的字符串参数中的单词个数返回。

Usage:

?php
$str = "How many words do I have?";
echo str_word_count($str); //Outputs 5
?>
Copier après la connexion
3.levenshtein()

当需要跟踪用户的拼写错误时,该函数可以方便的返回两个参数之间的levenshtein(编辑距离)。

Usage:

<?php
$str1 = "carrot";
$str2 = "carrrott";
echo levenshtein($str1, $str2); //Outputs 2
?>
Copier après la connexion
4.get_defined_vars()

这个函数在调试程序的时候非常有用,它会返回包含所有已定义变量的数组,其中包含环境、系统以及用户自定义变量。

Usage:

print_r(get_defined_vars());
Copier après la connexion

5.escapeshellcmd()

该函数用来跳过字符串中的特殊符号,防止恶意用户耍花招破解服务器系统。可以搭配exec()与system()函数使用。

Usage:

<?php
$command = &#39;./configure &#39;.$_POST[&#39;configure_options&#39;];

$escaped_command = escapeshellcmd($command);
 
system($escaped_command);
?>
Copier après la connexion
6.checkdate()

该函数可以用来检测日期参数的有效性。它可以验证输入的每一个参数的合法性。


Usage:

<?php
var_dump(checkdate(12, 31, 2000));
var_dump(checkdate(2, 29, 2001));

//Output
//bool(true)
//bool(false)
?>
Copier après la connexion
7.php_strip_whitespace()

该函数会返回删除了注释与空格后的PHP源码。这对实际代码数量和注释数量的对比很有用。

Usage:

<?php
// PHP comment here

/*
 * Another PHP comment
 */

echo        php_strip_whitespace(__FILE__);
// Newlines are considered whitespace, and are removed too:
do_nothing();
?>
Copier après la connexion

The output:

<?php
 echo php_strip_whitespace(__FILE__); do_nothing(); ?>
Copier après la connexion



==========================================================

PHP微信平台开通啦!
微信搜索“PHPChina”,点击关注按钮,即可获得PPC为您推送的最新最专业的业界信息,更有专题栏目为您献上
【PPC挖掘】:不定时为您献上经典产品与产品人的故事。
【PPC外文】:每日分享一篇外文翻译文章

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/735055.htmlTechArticlePHP有着众多的内置函数,其中大多数函数都被开发者广发使用。但也有一些同样有用却被遗忘在角落,本文将介绍7个鲜为人知功能却非常酷...
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!