8¸ö±Ø±¸µÄPHP¹¦ÄÜ¿ª·¢

5年以前  |  阅读数:296 次  |  编程语言:PHP 

PHP¿ª¢µÄ³ÌÐoÔ±Ó¦¸ÃÇa³þ£¬PHPÖÐÓкܶaÄÚÖõŦÄÜ£¬ÕÆÎÕÁËËuÃÇ£¬¿ÉÒÔ°iÖuÄaÔÚ×oPHP¿ª¢Ê±¸u¼ÓµÃÐÄÓ¦ÊÖ£¬±¾ÎĽ«ÖÏi8¸o¿ª¢±Ø±¸µÄPHP¹¦ÄÜ£¬¸o¸o¶¼Ç³£ÊµÓã¬Ï£Íu¸÷λPHP¿ª¢ÕßÄܹ»ÕÆÎÕ¡£

1¡¢´«µÝÈÎÒaÊýÁ¿µÄº¯Êý²ÎÊý
ÎÒÃÇÔÚ.NET»oÕßJAVA±a³ÌÖУ¬Ò»°aº¯Êý²ÎÊý¸oÊý¶¼Êǹ̶¨µÄ£¬µ«ÊÇPHPÔÊÐiÄaʹÓÃÈÎÒa¸oÊýµÄ²ÎÊý¡£ÏÂÃaeÕa¸oʾÀýÏoÄaչʾÁËPHPº¯ÊýµÄĬÈϲÎÊý£º


    // Á½¸oĬÈϲÎÊýµÄº¯Êý 
    function foo($arg1 = ¡±, $arg2 = ¡±) { 
    echo ¡°arg1: $arg1\n¡±; 
    echo ¡°arg2: $arg2\n¡±; 
    } 
    foo(¡®hello','world'); 
    /* Êa³o:
    arg1: hello
    arg2: world
    */ 
    foo(); 
    /* Êa³o:
    arg1:
    arg2:
    */ 

ÏÂÃaeÕa¸oʾÀýÊÇPHPµÄ²»¶¨²ÎÊýÓ裬ÆaʹÓõ½ÁË func_get_args()½*¨£º


    // Êǵģ¬ÐβÎÁбiΪ¿Õ 
    function foo() { 
    // È¡µÃËuÓеĴ«Èe²ÎÊýµÄÊý×e 
    $args = func_get_args(); 
    foreach ($args as $k => $v) { 
    echo ¡°arg¡±.($k+1).¡±: $v\n¡±; 
    } 
    } 
    foo(); 
    /* ʲôҲ²»»aÊa³o */ 
    foo(¡®hello'); 
    /* Êa³o
    arg1: hello
    */ 
    foo(¡®hello', ¡®world', ¡®again'); 
    /* Êa³o
    arg1: hello
    arg2: world
    arg3: again
    */


2¡¢Ê¹ÓÃglob()²eÕÒÎļþ
´o²¿ÖPHPº¯ÊýµÄº¯ÊýÃu´Ó×ÖÃaeÉ϶¼¿ÉÒÔÀi½aÆaÓÃ;£¬µ«Êǵ±Äa¿´µ½ glob() µÄʱºo£¬ÄaÒ²Ði²¢²»ÖªµÀÕaÊÇÓÃÀ´×oʲôµÄ£¬Æaʵglob()ºÍscandir() Ò»Ñu£¬¿ÉÒÔÓÃÀ´²eÕÒÎļþ£¬Çe¿´ÏÂÃaeµÄÓ裺


    // È¡µÃËuÓеĺo׺ΪPHPµÄÎļþ 
    $files = glob(¡®*.php'); 
    print_r($files); 
    /* Êa³o:
    Array
    (
    [0] => phptest.php
    [1] => pi.php
    [2] => post_output.php
    [3] => test.php
    )
    */

    Äa»¹¿ÉÒÔ²eÕÒ¶aÖÖºo׺Ãu£º
    // È¡PHPÎļþºÍTXTÎļþ 
    $files = glob(¡®*.{php,txt}', GLOB_BRACE); 
    print_r($files); 
    /* Êa³o:
    Array
    (
    [0] => phptest.php
    [1] => pi.php
    [2] => post_output.php
    [3] => test.php
    [4] => log.txt
    [5] => test.txt
    13.
    )
    */

Äa»¹¿ÉÒÔ¼ÓÉÏÂ*¾¶£º


    $files = glob(¡®../images/a*.jpg'); 
    print_r($files); 
    /* Êa³o:
    Array
    (
    [0] => ../images/apple.jpg
    [1] => ../images/art.jpg
    )
    */

Èç¹uÄaÏeµÃµ½¾ø¶ÔÂ*¾¶£¬Äa¿ÉÒÔµ÷Óà realpath() º¯Êý£º


    $files = glob(¡®../images/a*.jpg'); 
    // applies the function to each array element 
    $files = array_map(¡®realpath',$files); 
    print_r($files); 
    /* output looks like:
    Array
    (
    [0] => C:\wamp\www\images\apple.jpg
    [1] => C:\wamp\www\images\art.jpg
    )
    */

3¡¢»ñÈ¡ÄÚ´aeʹÓÃÇe¿oÐÅÏ¢
PHPµÄÄÚ´ae»ØÊÕ»uÖÆÒѾ­Ç³£Ç¿´o£¬ÄaÒ²¿ÉÒÔʹÓÃPHP½Å±¾»ñÈ¡µ±Ç°ÄÚ´aeµÄʹÓÃÇe¿o£¬µ÷ÓÃmemory_get_usage() º¯Êý»ñÈ¡µ±ÆÚÄÚ´aeʹÓÃÇe¿o£¬µ÷ÓÃmemory_get_peak_usage() º¯Êý»ñÈ¡ÄÚ´aeʹÓõÄaÖµ¡£²Î¿¼´uÂeÈçÏ£º


    echo ¡°Initial: ¡°.memory_get_usage().¡± bytes \n¡±; 
    /* Êa³o
    Initial: 361400 bytes
    */ 
    // ʹÓÃÄÚ´ae 
    for ($i = 0; $i < 100000; $i++) { 
    $array []= md5($i); 
    } 
    // ɾ³ýÒ»°eµÄÄÚ´ae 
    for ($i = 0; $i < 100000; $i++) { 
    unset($array[$i]); 
    } 
    echo ¡°Final: ¡°.memory_get_usage().¡± bytes \n¡±; 
    /* prints
    Final: 885912 bytes
    */ 
    echo ¡°Peak: ¡°.memory_get_peak_usage().¡± bytes \n¡±; 
    /* Êa³o*aÖµ
    Peak: 13687072 bytes
    */

4¡¢»ñÈ¡CPUʹÓÃÇe¿oÐÅÏ¢
»ñÈ¡ÁËÄÚ´aeʹÓÃÇe¿o£¬Ò²¿ÉÒÔʹÓÃPHPµÄ getrusage()»ñÈ¡CPUʹÓÃÇe¿o£¬¸Ã½¨ÔÚwindowsϲ»¿ÉÓá£


    print_r(getrusage()); 
    /* Êa³o
    Array
    (
    [ru_oublock] => 0
    [ru_inblock] => 0
    [ru_msgsnd] => 2
    [ru_msgrcv] => 3
    [ru_maxrss] => 12692
    [ru_ixrss] => 764
    [ru_idrss] => 3864
    [ru_minflt] => 94
    [ru_majflt] => 0
    [ru_nsignals] => 1
    [ru_nvcsw] => 67
    [ru_nivcsw] => 4
    [ru_nswap] => 0
    [ru_utime.tv_usec] => 0
    [ru_utime.tv_sec] => 0
    [ru_stime.tv_usec] => 6269
    [ru_stime.tv_sec] => 0
    )
    */

Õa¸o½a¹¹¿´ÉϳoºÜ»Þɬ£¬³ý*ÇÄa¶ÔCPUºÜÁ˽a¡£ÏÂÃaeÒ»Ð(C)½aÊÍ£º
ru_oublock: ¿eÊa³o²Ù×÷
ru_inblock: ¿eÊaÈe²Ù×÷
ru_msgsnd: ¢Ë͵Ämessage
ru_msgrcv: ÊÕµ½µÄmessage
ru_maxrss: ×i´oפÁo¼¯´oС
ru_ixrss: È«²¿¹²ÏiÄÚ´ae´oС
ru_idrss:È«²¿
ǹ²ÏiÄÚ´ae´oС
ru_minflt: Ò³»ØÊÕ
ru_majflt: ҳʧЧ
ru_nsignals: ÊÕµ½µÄÐźÅ
ru_nvcsw: Ö÷¶¯ÉÏÏÂÎÄÇл»
ru_nivcsw: ±»¶¯ÉÏÏÂÎÄÇл»
ru_nswap: ½»»»Çø
ru_utime.tv_usec: Óû§Ì¬Ê±¼a (microseconds)
ru_utime.tv_sec: Óû§Ì¬Ê±¼a(seconds)
ru_stime.tv_usec: ϵͳÄÚºËʱ¼a (microseconds)
ru_stime.tv_sec: ϵͳÄÚºËʱ¼a?(seconds)

Òª¿´µ½ÄaµÄ½Å±¾ÏuºÄÁ˶aÉÙCPU£¬ÎÒÃÇÐeÒª¿´¿´¡°Óû§Ì¬µÄʱ¼a¡±ºÍ¡°ÏµÍ³ÄÚºËʱ¼a¡±µÄÖµ¡£ÃeºÍ΢Ãe²¿ÖÊÇÖ±ðÌa¹(C)µÄ£¬Äu¿ÉÒÔ°Ñ΢ÃeÖµ³ýÒÔ100Ío£¬²¢°ÑËuÌi¼Óµ½ÃeµÄÖµºo£¬¿ÉÒԵõ½ÓÐСÊý²¿*ÖµÄÃeÊý¡£


    // sleep for 3 seconds (non-busy) 
    sleep(3); 
    $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); 
    /* Êa³o
    User time: 0.011552
    System time: 0
    */
    sleepÊDz»Õ¼ÓÃϵͳʱ¼aµÄ£¬ÎÒÃÇ¿ÉÒÔÀ´¿´ÏÂÃaeµÄÒ»¸oÀý×Ó£º 
    // loop 10 million times (busy) 
    for($i=0;$i<10000000;$i++) { 
    } 
    $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); 
    /* Êa³o
    User time: 1.424592
    System time: 0.004204
    */

Õa»¨ÁË´oÔ¼14ÃeµÄCPUʱ¼a£¬¼¸ºoËuÓеĶ¼ÊÇÓû§µÄʱ¼a£¬ÒoΪûÓÐϵͳµ÷Óá£
ϵͳʱ¼aÊÇCPU»¨*ÑÔÚϵͳµ÷ÓÃÉϵÄÉÏÖ´ÐÐÄÚºËÖ¸ÁiµÄʱ¼a¡£ÏÂÃaeÊÇÒ»¸oÀý×Ó£º


    $start = microtime(true); 
    // keep calling microtime for about 3 seconds 
    while(microtime(true) ¨C $start < 3) { 
    } 
    $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); 
    /* prints
    User time: 1.088171
    System time: 1.675315
    */

ÎÒÃÇ¿ÉÒÔ¿´µ½ÉÏÃaeÕa¸oÀý×Ó¸uºÄCPU¡£
5¡¢»ñȡϵͳ³£Á¿
PHP Ìa¹(C)dz£ÓÐÓõÄϵͳ³£Á¿ ¿ÉÒÔÈÃÄaµÃµ½µ±Ç°µÄÐкŠ(LINE)£¬Îļþ (FILE)£¬Ä¿Â¼ (DIR)£¬º¯ÊýÃu (FUNCTION)£¬ÀaÃu(CLASS)£¬½*¨Ãu(METHOD) ºÍÃu×Ö¿Õ¼a (NAMESPACE)£¬ºÜÏñCÓiÑÔ¡£
ÎÒÃÇ¿ÉÒÔÒÔΪÕaÐ(C)¶«Î÷Ö÷ÒªÊÇÓÃÓÚµ÷ÊÔ£¬µ±Ò²²»Ò»¶¨£¬±ÈÈçÎÒÃÇ¿ÉÒÔÔÚincludeÆaËuÎļþµÄʱºoʹÓÃ?FILE (µ±È»£¬ÄaÒ²¿ÉÒÔÔÚ PHP 5.3ÒÔºoʹÓà DIR )£¬ÏÂÃaeÊÇÒ»¸oÀý×Ó¡£


    // this is relative to the loaded script's path 
    // it may cause problems when running scripts from different directories 
    require_once(¡®config/database.php'); 
    // this is always relative to this file's path 
    // no matter where it was included from 
    require_once(dirname(__FILE__) . ¡®/config/database.php');

ÏÂÃaeÊÇʹÓà LINE À´Êa³oÒ»Ð(C)debugµÄÐÅÏ¢£¬ÕaÑuÓÐÖuÓÚÄaµ÷ÊÔ³ÌÐo£º


    // some code 
    // ¡­ 
    my_debug(¡°some debug message¡±, __LINE__); 
    /* Êa³o
    Line 4: some debug message
    */ 
    // some more code 
    // ¡­ 
    my_debug(¡°another debug message¡±, __LINE__); 
    /* Êa³o
    Line 11: another debug message
    */ 
    function my_debug($msg, $line) { 
    echo ¡°Line $line: $msg\n¡±;
    }

6¡¢Éu³ÉΨһµÄid
ºÜ¶aÅoÓѶ¼ÀuÓÃmd5()À´Éu³ÉΨһµÄ±aºÅ£¬µ«ÊÇmd5()Óм¸¸oȱµa£º1¡¢ÎÞÐo£¬µ¼ÖÂÊý¾Ý¿aÖÐÅÅÐoÐÔÄÜϽµ¡£2¡¢Ì«³¤£¬ÐeÒª¸u¶aµÄ´ae´¢¿Õ¼a¡£ÆaʵPHPÖÐ×Ô´øÒ»¸oº¯ÊýÀ´Éu³ÉΨһµÄid£¬Õa¸oº¯Êý¾ÍÊÇuniqid()¡£ÏÂÃaeÊÇÓÃ*¨£º


    // generate unique string 
    echo uniqid(); 
    /* Êa³o
    4bd67c947233e
    */ 
    // generate another unique string 
    echo uniqid(); 
    /* Êa³o
    4bd67c9472340
    */

¸ÃËa¨ÊǸu¾ÝCPUʱ¼a´ÁÀ´Éu³ÉµÄ£¬ËuÒÔÔÚÏa½uµÄʱ¼a¶ÎÄÚ£¬idÇ°¼¸Î»ÊÇÒ»ÑuµÄ£¬ÕaÒ²½±aidµÄÅÅÐo£¬Èç¹uÄaÏe¸uºÃµÄ±ÜÃaÖظ´£¬¿ÉÒÔÔÚidÇ°¼ÓÉÏǰ׺£¬È磺


    // ǰ׺ 
    echo uniqid(¡®foo_'); 
    /* Êa³o
    foo_4bd67d6cd8b8f
    */ 
    // Óиu¶aµÄiØ 
    echo uniqid(¡±,true); 
    /* Êa³o
    4bd67d6cd8b926.12135106
    */ 
    // ¶¼ÓÐ 
    echo uniqid(¡®bar_',true); 
    /* Êa³o
    bar_4bd67da367b650.43684647
    */

7¡¢ÐoÁл¯
PHPÐoÁл¯¹¦ÄÜ´o¼Ò¿ÉÄÜÓõıȽ϶a£¬Ò²±È½Ï³£¼u£¬µ±ÄaÐeÒª°ÑÊý¾Ý´aeµ½Êý¾Ý¿a»oÕßÎļþÖÐÊÇ£¬Äa¿ÉÒÔÀuÓÃPHPÖеÄserialize() ºÍ unserialize()½¨À´ÊµÏÖÐoÁл¯ºÍ*´ÐoÁл¯£¬´uÂeÈçÏ£º


    // Ò»¸o¸´ÔÓµÄÊý×e 
    $myvar = array( 
    ¡®hello', 
    42, 
    array(1,'two'), 
    ¡®apple' 
    ); 
    // ÐoÁл¯ 
    $string = serialize($myvar); 
    echo $string; 
    /* Êa³o
    a:4:{i:0;s:5:¡±hello¡±;i:1;i:42;i:2;a:2:{i:0;i:1;i:1;s:3:¡±two¡±;}i:3;s:5:¡±apple¡±;}
    */ 
    // *´ÐoÀý»¯ 
    $newvar = unserialize($string); 
    print_r($newvar); 
    /* Êa³o
    Array
    (
    [0] => hello
    [1] => 42
    [2] => Array
    (
    [0] => 1
    [1] => two
    )
    [3] => apple
    )
    */ 

ÈçºÎÐoÁл¯³Éjson¸ñʽÄØ£¬*ÅÐÄ£¬phpÒ²ÒѾ­ÎªÄa×oºÃÁË£¬Ê¹ÓÃphp 5.2ÒÔÉÏ°ae±¾µÄÓû§¿ÉÒÔʹÓÃjson_encode() ºÍ json_decode() º¯ÊýÀ´ÊµÏÖjson¸ñʽµÄÐoÁл¯£¬´uÂeÈçÏ£º


    // a complex array
    $myvar = array( 
    ¡®hello', 
    42, 
    array(1,'two'), 
    ¡®apple' 

    ); 
    // convert to a string 
    $string = json_encode($myvar); 
    echo $string; 
    /* prints
    ["hello",42,[1,"two"],¡±apple¡±]
    */ 
    // you can reproduce the original variable 
    $newvar = json_decode($string); 
    print_r($newvar); 
    /* prints
    Array
    (
    [0] => hello
    [1] => 42
    [2] => Array
    (
    [0] => 1
    [1] => two
    )
    [3] => apple
    )
    */

*8¡¢×Öu´®Ñ¹Ëo
*µ±ÎÒÃÇ˵µ½Ñ¹Ëo£¬ÎÒÃÇ¿ÉÄÜ»aÏeµ½ÎļþѹËo£¬Æaʵ£¬×Öu´®Ò²ÊÇ¿ÉÒÔѹËoµÄ¡£PHPÌa¹(C)ÁË gzcompress() ºÍgzuncompress() º¯Êý£º


    $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); 
    echo ¡°Original size: ¡°. strlen($string).¡±\n¡±; 
    /* Êa³oԭʼ´oС
    Original size: 800
    */ 
    echo ¡°Compressed size: ¡°. strlen($compressed).¡±\n¡±; 
    /* Êa³oѹËoºoµÄ´oС
    Compressed size: 418
    */ 
    // ½aѹËo 
    $original = gzuncompress($compressed);

¼¸ºoÓÐ50% ѹËo±ÈÂÊ¡£Í¬Ê±£¬Äa»¹¿ÉÒÔʹÓà gzencode() ºÍ gzdecode() º¯ÊýÀ´Ñ¹Ëo£¬Ö»²»ÓÃÆaÓÃÁ˲»Í¬µÄѹËoËa¨¡£
ÒÔÉϾÍÊÇ8¸o¿ª
¢±Ø±¸µÄPHP¹¦ÄÜ£¬ÊDz»ÊǶ¼ºÜʵÓÃÄØ£¿

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例