Home > Backend Development > PHP Tutorial > PHP custom function + system function library (code example)

PHP custom function + system function library (code example)

藏色散人
Release: 2023-04-08 10:40:02
forward
2860 people have browsed it

PHP custom function + system function library (code example)

全局变量

  $n = 5; //全局变量
    function fun1(){
        global $n;
        echo '我在函数体内也可以调用全局变量n,它的值是:' , $n;//5
        $n++;
    }
    fun1();
    echo &#39;<hr>&#39;;
    echo $n;//6
Copy after login
  $n = 6;
    function fun1(){
        echo &#39;变量的值是:&#39; , $GLOBALS[&#39;n&#39;];
        $GLOBALS[&#39;n&#39;]++;
    }
    fun1();
    echo $GLOBALS[&#39;n&#39;];
Copy after login

不使用循环语句,来计算1~100的和

    function recursive($n){
        if($n>=1){
            return $n + recursive($n-1);
        }
    }
    echo recursive(100);
Copy after login

引用

    $foo = &#39;Bob&#39;;
    $bar = &$foo; //看待成变量的别名
    $bar = &#39;Rose&#39;;
    echo $foo;//Rose
    $foo = &#39;Mooc&#39;;
    $bar = &$foo; //看待成变量的别名
    unset($foo); //变量销毁
    echo $bar;//Mooc
Copy after login

自定义函数

    function fun1(&$n){
        $n++;
        echo &#39;我是函数体内的局部变量&#39; ,  $n ;//4
    }
    $n = 3;
    fun1($n);
    echo $n , &#39;<hr>&#39;;//4
Copy after login

获得扩展名

    function getExtension($filename)
    {
        $pos = strrpos($filename, &#39;.&#39;);
        $extension = strtolower(substr($filename, $pos + 1));
        return $extension;
    }
    $path = &#39;mooc.func.pHP&#39;;
    var_dump(getExtension($path));
Copy after login

求平均数

  function avg(...$args)
    {
        return $args;
    }
    var_dump(avg(1, 2, 3));
Copy after login

系统函数库

字符串转数组

    $str = &#39;A|B|C|D&#39;;
    $arr = explode(&#39;|&#39;, $str);
    print_r($arr);//[A,B,C,D]
Copy after login

数组转字符串

    $arr2 = array(&#39;Tom&#39;,&#39;John&#39;,&#39;Rose&#39;);
    $str2 = implode(&#39;,&#39;,$arr2);
    echo $str2;//Tom,John,Rose
Copy after login

获取扩展名:

方法一

    $filename = &#39;ab.cd.gif.JpEg&#39;; //gepj.fig.dc.ba
    $num = strrpos($filename, &#39;.&#39;);
    echo strtolower(substr($filename, $num+1)) , &#39;<br/><br/>&#39;;//jpeg
Copy after login

方法二

    $filename = &#39;ab.cd.gif.JpEg&#39;; //gepj.fig.dc.ba
    $str2 = strrev($filename);//strrev反转字符串
    $num = strpos($str2, &#39;.&#39;);
    echo strtolower(strrev(substr($str2, 0,$num)));//jpeg
Copy after login

trim移除字符串两侧的字符

  $str = "\n\n\t\tABC\t\t";
    echo trim($str);//ABC
Copy after login

md5()加密

    $str = &#39;abc&#39;;
    echo md5($str);//900150983cd24fb0d6963f7d28e17f72
Copy after login

格式化字符串

    $number = 5;
    $str = &#39;shanghai&#39;;
    $txt = sprintf(&#39;there are %d million cars in %s&#39;,$number,$str);
    echo $txt;//there are 5 million cars in shanghai
    $number = 123;
    $txt = sprintf("带有两位小数的结果是:%1\$.2f,\n不带小数的是:%1\$d",$number);
    echo $txt;//带有两位小数的结果是:123.00,不带小数的是:123
Copy after login

htmlspecialchars特殊字符转为HTML实体

    $str = "A>B,B<C,Tom&John,He said:\"I&#39;m OK\"";
    echo htmlspecialchars($str,ENT_QUOTES);//A&gt;B,B&lt;C,Tom&amp;John,He said:&quot;I&#039;m OK&quot;
Copy after login

通过str_replace进行转换

    $str1 = str_replace(&#39;&&#39;, &#39;&amp;&#39;, $str); //必须是第一阶梯
    $str2 = str_replace(&#39;>&#39;, &#39;&gt;&#39;, $str1);
    $str2 = str_replace(&#39;<&#39;, &#39;&lt;&#39;, $str2);
    $str2 = str_replace(&#39;"&#39;, &#39;&quot;&#39;, $str2);
    $str2 = str_replace(&#39;\&#39;&#39;, &#39;&#39;&#39;, $str2);
    echo $str2;//A&gt;B,B&lt;C,Tom&amp;John,He said:&quot;I&#039;m OK&quot;
Copy after login

str_ireplace不区分大小写

  $str = &#39;javascript&#39;;
    echo str_ireplace(&#39;A&#39;, &#39;b&#39;, $str);//jbvbscript
Copy after login

随机地打乱字符串中的所有字符

    $str = &#39;abcdefghijklmnopqrstuvwxyz&#39;;
    $str = str_shuffle($str);
    echo substr($str,0,4);//drif
Copy after login

strlen获得字符长度

  $str1 = NULL;//0
    $str2 = &#39;AB&#39;;//2
    $str3 = &#39;中国&#39;;//6 一个中文3个字符
    echo strlen($str1) , strlen($str2) , strlen($str3);
Copy after login

stripos不区分大小写,字符串从0开始编号,如果没有出现,则返回FALSE

    $str1 = &#39;javascript&#39;;
    $str2 = &#39;A&#39;;
    var_dump(stripos($str1, $str2)); //int(1)
Copy after login

搜索$str2在字符串中的位置,并返回从该位置到字符串结尾的所有字符

  $str1 = &#39;abcdcef&#39;;
    $str2 = &#39;c&#39;;
    echo strrchr($str1, $str2);//cef
Copy after login

获取扩展名

    $filename = &#39;a.bc.cd.png&#39;;
    echo substr(strrchr($filename, &#39;.&#39;),1);//png
Copy after login

strtoupper转大写

strtolower转小写

    $str1 = &#39;html&#39;;
    $str2 = &#39;PHP&#39;;
    echo strtoupper($str1) , strtolower($str2);//HTMLphp
Copy after login

ucfirst句子首字母大写

ucwords单词首字母大写

    $str3 = &#39;this is a test&#39;;
    echo ucfirst($str3) , ucwords($str3);
Copy after login

substr截取字符串

负数=字符串长度+该负数

    $str = &#39;javascript&#39;;
    echo strlen($str);//10
    echo substr($str, 0,4) ;//java
    echo substr($str, 4);//script
    echo substr($str, -2);//pt   -2=10-2=8
    echo substr($str, -5,-2) , "\n";//cri   -5,-2=5,8
Copy after login

将字符串转为Zend_Controller_Front

   $str = &#39;ZenD_CONTRollER_FronT&#39;;
    //1.转换小写
    $str1 = strtolower($str);
    //2.将下划线替换成空格
    $str2 = str_replace(&#39;_&#39;, &#39; &#39;, $str1);
    //3.通过ucwords进行首字母大写操作
    $str3 = ucwords($str2);
    //4.将空格替换成下划线
    $str4 = str_replace(&#39; &#39;, &#39;_&#39;, $str3);
    echo $str4;//Zend_Controller_Front
Copy after login
floor() ceil()
    $x = 2.7;
    $y = 3.01;
    echo floor($x) , &#39;<br/><br/>&#39;;//2 向下取整
    echo ceil($y) , &#39;<br/><br/>&#39;;//4 向上取整
Copy after login

假设记录数为X,每页显示Y条记录,求总页数z

  z = ceil(X/Y);
Copy after login

fmod()对浮点数取模

    echo fmod(7.8,3) , &#39;<br/>&#39;;//1.8
Copy after login

对整数取模

    echo 7.8 % 3 ; //整数余数的操作//1
Copy after login

格式化数字

    $x = 7896.827;
    echo number_format($x) , &#39;<br/><br/>&#39;;//7,897
    echo number_format($x,2) , &#39;<br/><br/>&#39;;//7,896.83
Copy after login

pow()幂操作 sqrt()平方根操作

    echo pow(2,3);//8
    echo sqrt(4) ;//2
Copy after login

mt_rand()是更好的随机数生成器,因为它跟rand()相比播下了一个更好地随机数种子;而且性能上比rand()快4倍

    echo rand(50,80);
    echo mt_rand(10,99);
Copy after login

生成四位数随机验证码

  $chars = &#39;abcdefghijlmnopqrstuvwxyz789654321&#39;;
    $len = strlen($chars);
    for($i=0;$i<4;$i++){
        $char .= substr($chars,mt_rand(0,$len-1),1);
    }
    echo $char;
Copy after login

round()四舍五入

    $x = 7.238;
    echo round($x);//7
    echo round($x,2);//7.24
Copy after login

strtotime字符串转时间

   echo &#39;当前日期:&#39;  , date(&#39;Y-m-d&#39;) , "\n";//2020-01-10
    echo &#39;下个月的日期:&#39; , date(&#39;Y-m-d&#39;, strtotime(&#39;1 month&#39;)) , "\n";//2020-02-10
    echo &#39;上个月最后一天:&#39; , date(&#39;Y-m-d H:i:s&#39;,strtotime(&#39;last day of -1 month&#39;)) , "\n";//2019-12-31 10:39:12
    echo &#39;上个月最后一天零点:&#39; , date(&#39;Y-m-d H:i:s&#39;, strtotime("midnight last day of -1 month")) , "\n"; //2019-12-31 00:00:00
    echo &#39;昨天零点:&#39; ,  date(&#39;Y-m-d H:i:s&#39;,strtotime(&#39;yesterday&#39;)) , "\n";//2020-01-09 00:00:00
    echo &#39;现在:&#39; ,  date(&#39;Y-m-d H:i:s&#39;,strtotime(&#39;now&#39;)) , "\n";//2020-01-10 10:39:12
    echo &#39;三个星期之间的时间戳是:&#39; ,  strtotime(&#39;-3 weeks&#39;);//三个星期之间的时间戳是:1576810790
    echo (time() -  strtotime(&#39;-3 weeks&#39;))/86400 ;//21  间隔时间
    echo &#39;上个月:&#39;.date(&#39;Y-m-d H:i:s&#39;,strtotime(&#39;-1 month&#39;)) ; //上个月:2019-12-10 10:59:50
    echo &#39;上个月的第一天:&#39;.date(&#39;Y-m-d H:i:s&#39;,strtotime(&#39;first day of -1 month&#39;));//上个月的第一天:2019-12-01 10:59:50
Copy after login

返回当前本地的日期/时间的日期/时间信息

    print_r(getdate());
    //Array
    //(
    //    [seconds] => 3
    //    [minutes] => 42
    //    [hours] => 10
    //    [mday] => 10
    //    [wday] => 5
    //    [mon] => 1
    //    [year] => 2020
    //    [yday] => 9
    //    [weekday] => Friday
    //[month] => January
    //[0] => 1578624123
    //)
Copy after login

microtime()返回当前 Unix 时间戳的微秒数

    echo microtime();//0.41369400 1578624195
Copy after login

当设置为 TRUE 时,规定函数应该返回一个浮点数,否则返回一个字符串;默认为 FALSE

  echo microtime(true);//1578624195.4137
Copy after login

计算程序运行时间

    $start = microtime(true);
    $sum = 0;
    for ($i=0; $i <1000000 ; $i++) { 
        $sum += $i;
    }
    $end = microtime(true);
    echo  &#39;共花费&#39; , round($end - $start,3) , &#39;秒&#39;;//共花费0.016秒
Copy after login
time()
    echo time() ;//1578625294
    echo &#39;当前的日期时间是:&#39; , date(&#39;Y-m-d H:i:s&#39;) ;//当前的日期时间是:2020-01-10 11:01:34
    echo &#39;昨天的日期时间是:&#39; , date(&#39;Y-m-d H:i:s&#39;,time()-86400) ; //24*60*60 //昨天的日期时间是:2020-01-09 11:01:34
Copy after login

uniqid() 函数基于以微秒计的当前时间,生成一个唯一的 ID

    echo uniqid();//5e17e94f8a19b
    echo uniqid(&#39;abc&#39;);//abc5e17e96c1771e
    echo uniqid(microtime());//0.09603300 15786253885e17e96c17727
    echo uniqid(microtime() . mt_rand()); //mt_rand(100,999);//0.09604200 15786253884744704985e17e96c1772f
    //uuid 8-4-4-4-12 = 32
    echo md5(uniqid(microtime() . mt_rand()));//cf6333288fcb04f60fbbedafd127201e
Copy after login
session
    session_start();
    echo session_id();//bp99jhu204h6vi214ttgcjce80
Copy after login

更多相关php知识,请访问php教程

The above is the detailed content of PHP custom function + system function library (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template