zoukankan      html  css  js  c++  java
  • PHP常用功能和函数

    1、传递任意数量的函数参数

      我们在.NET或者JAVA编程中,一般函数参数个数都是固定的,但是PHP允许你使用任意个数的参数。下面这个示例向你展示了PHP函数的默认参数:

    // 两个默认参数的函数
    function foo($arg1 = ”, $arg2 = ”) {
     
    echo “arg1: $arg1
    ”;
    echo “arg2: $arg2
    ”;
     
    }
     
    foo(‘hello’,'world’);
     /* 输出:
    arg1: hello
     arg2: world
     */
     
    foo();
     /* 输出:
    arg1:
     arg2:
     */

    下面这个示例是PHP的不定参数用法,其使用到了?func_get_args()方法:

    // 是的,形参列表为空
    function foo() {
     
    // 取得所有的传入参数的数组
    $args = func_get_args();
     
    foreach ($args as $k => $v) {
     echo “arg”.($k+1).”: $v
    ”;
     }
     
    }
     
    foo();
     /* 什么也不会输出 */
     
    foo(‘hello’);
     /* 输出
    arg1: hello
     */
     
    foo(‘hello’, ‘world’, ‘again’);
     /* 输出
    arg1: hello
     arg2: world
     arg3: again
     */

    2、使用glob()查找文件

    大部分PHP函数的函数名从字面上都可以理解其用途,但是当你看到?glob() 的时候,你也许并不知道这是用来做什么的,其实glob()和scandir() 一样,可以用来查找文件,请看下面的用法:

    // 取得所有的后缀为PHP的文件
    $files = glob(‘*.php’);
     
    print_r($files);
     /* 输出:
    Array
     (
     [0] => phptest.php
     [1] => pi.php
     [2] => post_output.php
     [3] => test.php
     )
     */

    3、获取内存使用情况信息

    PHP的内存回收机制已经非常强大,你也可以使用PHP脚本获取当前内存的使用情况,调用memory_get_usage() 函数获取当期内存使用情况,调用memory_get_peak_usage() 函数获取内存使用的峰值。参考代码如下:

    echo “Initial: “.memory_get_usage().” bytes 
    ”;
     /* 输出
    Initial: 361400 bytes
     */
     
    // 使用内存
    for ($i = 0; $i < 100000; $i++) {
     $array []= md5($i);
     }
     
    // 删除一半的内存
    for ($i = 0; $i < 100000; $i++) {
     unset($array[$i]);
     }
     
    echoFinal: “.memory_get_usage().” bytes 
    ”;
     /* prints
     Final: 885912 bytes
     */
     
    echo “Peak: “.memory_get_peak_usage().” bytes 
    ”;
     /* 输出峰值
    Peak: 13687072 bytes
     */

    4、获取系统常量

    PHP 提供非常有用的系统常量 可以让你得到当前的行号 (__LINE__),文件 (__FILE__),目录 (__DIR__),函数名 (__FUNCTION__),类名(__CLASS__),方法名(__METHOD__) 和名字空间 (__NAMESPACE__),很像C语言。

      我们可以以为这些东西主要是用于调试,当也不一定,比如我们可以在include其它文件的时候使用?__FILE__ (当然,你也可以在 PHP 5.3以后使用 __DIR__ ),下面是一个例子。

    // 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’);
    
    // some code
     // …
    my_debug(“some debug message”, __LINE__);
     /* 输出
    Line 4: some debug message
     */
     
    // some more code
     // …
    my_debug(“another debug message”, __LINE__);
     /* 输出
    Line 11: another debug message
     */
     
    function my_debug($msg, $line) {
     echo “Line $line: $msg
    ”;
     }

    5、生成唯一的id

    很多朋友都利用md5()来生成唯一的编号,但是md5()有几个缺点:1、无序,导致数据库中排序性能下降。2、太长,需要更多的存储空间。其实PHP中自带一个函数来生成唯一的id,这个函数就是uniqid()。下面是用法:

    // generate unique string
     echo uniqid();
     /* 输出
    4bd67c947233e
     */
     
    // generate another unique string
     echo uniqid();
     /* 输出
    4bd67c9472340
     */

    6、序列化

    如何序列化成json格式呢,放心,php也已经为你做好了,使用php 5.2以上版本的用户可以使用json_encode() 和 json_decode() 函数来实现json格式的序列化,代码如下:

    // 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
     )
     */

    转载自:柒捌玖零博客

  • 相关阅读:
    语言只是个工具
    最近学到的一点东西
    iBeacon开发
    马上着手开发Mac应用程序
    Text Kit入门
    Text Kit进阶
    Web Notification
    Objective-C异步编程
    Clang Language Extensions
    黑客与画家
  • 原文地址:https://www.cnblogs.com/feimengv/p/4151010.html
Copyright © 2011-2022 走看看