PHP have lots of built-in functions, and most developers know many of them. But a few functions are not very well known, but are super useful. In this article, I have compiled little known but really cool PHP functions.

highlight_string()

When displaying PHP code on a website, the highlight_string() function can be really helpful: It outputs or returns a syntax highlighted version of the given PHP code using the colors defined in the built-in syntax highlighter for PHP.

Usage:

<?php
highlight_string('<?php phpinfo(); ?>');
?>

Documentation: http://php.net/manual/en/function.highlight-string.php

str_word_count()

This handy function takes a string as a parameter and return the count of words, as shown in the example below.

Usage:

?php
$str = "How many words do I have?";
echo str_word_count($str); //Outputs 5
?>

Documentation: http://php.net/manual/en/function.str-word-count.php

levenshtein()

Ever find the need to determine how different (or similar) two words are? Then levenshtein() is just the function you need. This function can be super useful to track user submitted typos.

Usage:

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

Documentation: http://php.net/manual/en/function.levenshtein.php

get_defined_vars()

Here is a handy function when debugging: It returns a multidimensional array containing a list of all defined variables, be them environment, server or user-defined variables, within the scope that get_defined_vars() is called.

Usage:

print_r(get_defined_vars());

Documentation: http://php.net/manual/en/function.get-defined-vars.php

escapeshellcmd()

escapeshellcmd() escapes any characters in a string that might be used to trick a shell command into executing arbitrary commands. This function should be used to make sure that any data coming from user input is escaped before this data is passed to the exec() or system() functions, or to the backtick operator.

Usage:

<?php
$command = './configure '.$_POST['configure_options']; $escaped_command = escapeshellcmd($command); system($escaped_command);
?>

Documentation: http://php.net/manual/en/function.escapeshellcmd.php

checkdate()

Checks the validity of the date formed by the arguments. A date is considered valid if each parameter is properly defined. Pretty useful to test is a date submitted by an user is valid.

Usage:

<?php
var_dump(checkdate(12, 31, 2000));
var_dump(checkdate(2, 29, 2001)); //Output
//bool(true)
//bool(false)
?>

Documentation: http://php.net/checkdate

php_strip_whitespace()

Returns the PHP source code in filename with PHP comments and whitespace removed. This is similar to using php -w from the commandline.

Usage:

<?php
// PHP comment here /*
* Another PHP comment
*/ echo php_strip_whitespace(__FILE__);
// Newlines are considered whitespace, and are removed too:
do_nothing();
?>

The output:

<?php
echo php_strip_whitespace(__FILE__); do_nothing(); ?>

Documentation: http://www.php.net/manual/en/function.php-strip-whitespace.php

转:7个鲜为人知却超实用的PHP函数的更多相关文章

  1. 7个鲜为人知却超实用的PHP函数

    PHP有许多内置函数,其中大多数函数都被程序员广泛使用.但也有一些函数隐藏在角落,本文将向大家介绍7个鲜为人知,但用处非常大的函数. 没用过的程序员不妨过来看看. 1.highlight_string ...

  2. 7个鲜为人知却超实用的PHP函数--转(柒捌玖零)

    PHP有许多内置函数,其中大多数函数都被程序员广泛使用.但也有一些函数隐藏在角落,本文将向大家介绍7个鲜为人知,但用处非常大的函数. 没用过的程序员不妨过来看看. 1.highlight_string ...

  3. C++编程技术之 异常处理(上)

    增强错误恢复能力是提高代码健壮性的最有力途径之一 之所以平时编写代码的时候不愿意去写错误处理,主要是由于这项工作及其无聊并可能导致代码膨胀,导致的结果就是本来就比较复杂的程序变得更加复杂.当然了,前面 ...

  4. 我们为什么要看《超实用的Node.JS代码段》

    不知道自己Node.JS水平如何?看这张图 如果一半以上的你都不会,必须看这本书,一线工程师用代码和功能页面来告诉你每一个技巧点. 都会一点,但不知道如何检验自己,看看本书提供的面试题: 1.     ...

  5. 关于编程一些鲜为人知的真相 csdn

    <关于编程一些鲜为人知的真相>一文讲了一些编程的真相:如果把所有项目的生命周期平均一下,那么一个程序员大概10-20%的时间用来写代码,并且大多数程序员可能每天大约只有10-12行代码会进 ...

  6. Hover.css:一组超实用的 CSS3 悬停效果和动画

    Hover.css 是一套基于 CSS3 的鼠标悬停效果和动画,这些可以非常轻松的被应用到按钮.LOGO 以及图片等元素.所有这些效果都是只需要单一的标签,必要的时候使用 before 和 after ...

  7. 优秀工具推荐:超实用的 CSS 库,样板和框架

    当启动一个新的项目,使用 CSS 框架或样板,可以帮助您节省大量的时间.在这篇文章中,我编译整理了我最喜欢的 CSS 样板,框架和库,帮助你在建立网站或应用程序时更加高效. 您可能感兴趣的相关文章 精 ...

  8. 一些鲜为人知却非常实用的数据结构 - Haippy

    原文:http://www.udpwork.com/item/9932.html 作为程序猿(媛),你必须熟知一些常见的数据结构,比如栈.队列.字符串.链表.二叉树.哈希,但是除了这些常见的数据结构以 ...

  9. 值得拥有!精心推荐几款超实用的 CSS 开发工具

    当你开发一个网站或 Web 应用程序的时候,有合适的工具,绝对可以帮助您节省大量的时间.在这篇文章中,我为大家收集了超有用的 CSS 开发工具. 对于 Web 开发人员来说,找到有用的 CSS 开发工 ...

随机推荐

  1. Freemarker数字转时间

    使用freemarker模板,展示一个时间字段,数据库保存的是毫秒.在网上找了许多文章,发现都是针对date或者是直接类似"1999-09-09"这样已经成型字符串进行操作的,心中 ...

  2. PreTranslateMessage作用和用法

    PreTranslateMessage作用和用法  PreTranslateMessage是消息在送给TranslateMessage函数之前被调用的,绝大多数本窗体的消息都要通过这里,比較经常使用, ...

  3. Adblock Plus完美过滤视频网站广告、无黑屏!及屏蔽非本站脚本的Adblock Plus过滤器语法之探讨

    测试用浏览器:Firefox 24.订阅的Adblock Plus过滤规则有默认的 ChinaList + EasyList,和国内视频广告规则[Yge.me],其网址:http://i.yge.me ...

  4. jboss7 Java API for RESTful Web Services (JAX-RS) 官方文档

    原文:https://docs.jboss.org/author/display/AS7/Java+API+for+RESTful+Web+Services+(JAX-RS) Content Tuto ...

  5. [转] Java中的访问控制

    可见/访问性 在同一类中 同一包中 不同包中  同一包子类中  不同包子类中   public  yes  yes  yes  yes  yes  protected   yes  yes  no   ...

  6. java.util.concurrent.atomic 类包详解

    java.util.concurrent包分成了三个部分,分别是java.util.concurrent.java.util.concurrent.atomic和java.util.concurren ...

  7. Day10 - Python协程、异步IO、redis缓存、rabbitMQ队列

    Python之路,Day9 - 异步IO\数据库\队列\缓存   本节内容 Gevent协程 Select\Poll\Epoll异步IO与事件驱动 Python连接Mysql数据库操作 RabbitM ...

  8. RxJava 教程-1 简介 原理 线程控制 变换

    简介 RxJava 是什么? RxJava 在 GitHub 主页上的自我介绍是 RxJava is a Java VM implementation of ReactiveX: a library ...

  9. 错误记录--关于foreach,集合已修改;可能无法执行枚举操作

    集合已修改,可能无法执行枚举操作.今天在使用foreach遍历的时候出现了这样的错误.查了一下,这个是使用foreach的典型的错误问题问题.foreach在遍历取数据的过程中,枚举器只允许读,不允许 ...

  10. Web api 文档以及测试工具配置

    第一步: 创建web api 在nuget 上搜索 webapitestclient (包含预发行版) 然后在 /Areas/HelpPage/Views/Help/Api.cshtml 末尾 添加 ...