常用实用的php函数小结

来源:互联网 发布:安卓条形码扫描源码 编辑:程序博客网 时间:2024/05/21 18:36

常用实用的php函数小结

  1. array_filter():去掉数组中的空值

    举个例子,代码如下:

    public function test()
    {
    $arr = array(1=>'a','e'=>'b',2=>'3',3=>'',4=>'v',5=>0,6=>'p');
    // dd($arr);
    $res = array_filter($arr);
    dd($res);
    }

    输出结果为:

    array:5 [▼
    1 => "a"
    "e" => "b"
    2 => "3"
    4 => "v"
    6 => "p"
    ]

  2. array_merge():把一个或多个数组合并为一个数组

    举个例子,代码如下:

    public function test()
    {
    $arr1 = array('a'=>'a','b'=>'b');
    $arr2 = array('c','d');
    $res = array_merge($arr1,$arr2);
    dd($res);
    }

    输出结果为:

    array:4 [▼
    "a" => "a"
    "b" => "b"
    0 => "c"
    1 => "d"
    ]

  3. strtoupper():把字符串转换为大写

    举个例子,代码如下:

    public function test()
    {
    $a = 'fdsfdsMN';
    $res = strtoupper($a);
    dd($res);
    }

    输出结果为:

    FDSFDSMN

  4. strtolower():把字符串转换为小写

    举个例子,代码如下:

    public function test()
    {
    $a = 'fdsfdsMN';
    $res = strtolower($a);
    dd($res);
    }

    输出结果为:

    fdsfdsmn

  5. ucfirst():字符串首字母大写

    举个例子,代码如下:

    public function test()
    {
    $a = 'fdsfdsMN';
    $res = ucfirst($a);
    dd($res);
    }

    输出结果为:

    FdsfdsMN

  6. ucwords():字符串每个单词首字符转为大写

    举个例子,代码如下:

    public function test()
    {
    $a = 'hello my world';
    $res = ucwords($a);
    dd($res);
    }

    输出结果为:

    Hello My World

  7. unset():销毁指定的变量
  8. trim():移除字符串两侧的空白字符或者其他

    举个例子,代码如下:

    public function test()
    {
    $a = 'hellomyworld';
    $res = trim($a,'helld');
    dd($res);
    }

    输出结果为:

    omywor

  9. in_array():数组中是否存在指定的值

    举个例子,代码如下:

    public function test()
    {
    $a = array('a','b','c','d');
    if(in_array('c',$a)){
    echo '在其中';
    }else{
    echo '不在其中';
    }
    }

    输出结果为:

    在其中

  10. nl2br():在字符串中的每个新行(\n)之前插入HTML换行符
  11. json_encode():将数组转换为json数据存储格式

    举个例子,代码如下:

    public function test()
    {
    $arr = array(1=>'a',2=>'b',3=>'c',4=>'d');
    $res = json_encode($arr);
    dd($res);
    }

    输出结果为:

    {"1":"a","2":"b","3":"c","4":"d"}

  12. json_decode():将json数据存储格式转换为数组

    举个例子,代码如下:

    public function test()
    {
    $json = '{"1":"a","2":"b","3":"c","4":"d"}';
    $res = json_decode($json,'true');
    dd($res);
    }

    输出结果为:

    array:4 [▼
    1 => "a"
    2 => "b"
    3 => "c"
    4 => "d"
    ]

  13. explode():把字符串切割为数组

    举个例子,代码如下:

    public function test()
    {
    $str = 'a,b,c,d,e,f';
    $res = explode(',',$str);
    dd($res);
    }

    输出结果为:

    array:6 [▼
    0 => "a"
    1 => "b"
    2 => "c"
    3 => "d"
    4 => "e"
    5 => "f"
    ]

  14. implode():把数组组合为字符串

    举个例子,代码如下:

    public function test()
    {
    $arr = array('a','b','c','d');
    $res = implode(',',$arr);
    dd($res);
    }

    输出结果为:

    "a,b,c,d"

  15. substr():截取字符串

    举个例子,代码如下:

    public function test()
    {
    $str = '2017-09-21 12:24:12';
    $res = substr($str,0,10);
    dd($res);
    }

    输出结果为:

    "2017-09-21"

  16. str_replace():字符串替换操作,区分大小写

    举个例子,代码如下:

    public function test()
    {
    $str = '你好吗?';
    $res = str_replace('好','不好',$str);
    dd($res);
    }

    输出结果为:

    "你不好吗?"

  17. strlen():统计字符串长度

    举个例子,代码如下:

    public function test()
    {
    $a = strlen('rewrwr');
    dd($a);
    }

    输出结果为:

    6

    注意:一个中文字等于三个字符串

  18. md5():字符串md5编码

    举个例子,代码如下:

    public function test()
    {
    $str = '你好吗?';
    $res = md5($str);
    dd($res);
    }

    输出结果为:

    "bb0b6bc45375143826f72439e050743e"

  19. time()返回当前的Unix时间戳time();返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的秒数
  20. strtotime():将任何英文文本的日期时间描述解析为时间戳

    举个例子,代码如下:

    public function test()
    {
    $time = strtotime('2017/6/15');
    dd($time);
    }

    输出结果为:

    "1497456000"

  21. intval():获取变量的整数值

    举个例子,代码如下:

    public function test()
    {
    $data = intval('2.3');
    dd($data);
    }

    输出结果为:

    "2"

  22. abs():求绝对值

    举个例子,代码如下:

    public function test()
    {
    $data = abs('-2.22');
    dd($data);
    }

    输出结果为:

    "2.22"

  23. ceil():进一法取整

    举个例子,代码如下:

    public function test()
    {
    $data = ceil('2.2');
    dd($data);
    }

    输出结果为:

    "3.0"

  24. floor():舍去法取整

    举个例子,代码如下:

    public function test()
    {
    $data = floor('2.22');
    dd($data);
    }

    输出结果为:

    "2.0"

  25. fmod():浮点数取余

    举个例子,代码如下:

    public function test()
    {
    $data = fmod(5,3);
    dd($data);
    }

    输出结果为:

    "2.0"

  26. round():浮点数四舍五入

    举个例子,代码如下:

    public function test()
    {
    $data = round(5.3);
    dd($data);
    }

    输出结果为:

    "5.0"

  27. sqrt():求平方根

    举个例子,代码如下:

    public function test()
    {
    $data = sqrt(6);
    dd($data);
    }

    输出结果为:

    2.4494897427832

  28. max():求最大值

    举个例子,代码如下:

    public function test()
    {
    $data = max(6,7,8,2,3);
    dd($data);
    }

    输出结果为:

    8

  29. min():求最小值

  30. mt_rand():更好的随机数

    举个例子,代码如下:

    public function test()
    {
    $data = mt_rand(1000,9999);
    dd($data);
    }

    输出结果为:

    8601

  31. rand():随机数随机返回范围内的值

    举个例子,代码如下:

    public function test()
    {
    $data = rand(1000,9999);
    dd($data);
    }

    输出结果为:

    6798

  32. pi():获取圆周率值

    举个例子,代码如下:

    public function test()
    {
    $data = pi();
    dd($data);
    }

    输出结果为:

    3.1415926535898

原创粉丝点击