• PHP里10个鲜为人知但却非常有用的函数


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

    levenshtein()

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

    用法:

    <?php

    $str1 = "carrot";

    $str2 = "carrrott";

    echo levenshtein($str1, $str2); //Outputs 2

    ?>

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

    get_defined_vars()

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

    用法:

    <?php

    print_r(get_defined_vars());

    ?>

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

    php_check_syntax()

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

    用法:

    <?php

    $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";

    }

    ?>

    Source: http://www.php.net/manual/en/function.php-check-syntax.php

    ignore_user_abort()

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

    用法:

    <?php

    ignore_user_abort();

    ?>

    Source: http://www.php.net/manual/en/function.ignore-user-abort.php

    highlight_string()

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

    用法

    <?php

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

    ?>

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

    highlight_file

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

    用法:

    <?php

    highlight_file("php_script.php");

    ?>

    Source: http://www.php.net/manual/en/function.highlight-file.php

    php_strip_whitespace

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

    用法:

    <?php

    echo php_strip_whitespace("php_script.php");

    ?>

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

    get_browser

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

    用法:

    <?php

    echo $_SERVER['HTTP_USER_AGENT'];

    $browser = get_browser();

    print_r($browser);

    ?>

    Source: http://www.php.net/manual/en/function.get-browser.php

    memory_get_usage(),memory_get_peak_usage(),getrusage()

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

    用法:

    <?php

    echo "Initial: ".memory_get_usage()." bytes ";

    echo "Peak: ".memory_get_peak_usage()." bytes ";

    $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() 也能达到类似结果,但使用了不同的压缩算法。

    用法:

    <?php

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

    ?>

    你是否也想到了还有其它很有用的函数?请在评论里分享出来!

    来自:http://www.chinaz.com/program/2014/0128/337438.shtml

  • 相关阅读:
    golang的select典型用法
    vscode配置git和提交代码到github教程
    VsCode中好用的git源代码管理插件GitLens
    GoMock框架使用指南
    golang对结构体排序,重写sort
    Go语言开发Prometheus Exporter示例
    golang 字符串拼接性能比较
    golang中的strings.Compare
    各大厂分布式链路跟踪系统架构对比
    NV triton启动方式说明
  • 原文地址:https://www.cnblogs.com/lixiuran/p/3552452.html
Copyright © 2020-2023  润新知