初识php的重点二

2020-09-02  本文已影响0人  mutang
  1. 循环

    switch和goto不能从外部跳入循环内部

  2. 全局变量

    1. 在外部定义的变量,在函数内部使用,需要global关键字或者$GLOBALS预定义变量,否则,报变量未定义,这点与python不同
  3. 函数

    1. callback回调函数
    function cude($n){
        echo 'n的三次幂:',($n**3),"</hr>";
    }
    
    call_user_func('cude',3);//n的三次幂:8
    call_user_func(function ($m){
        echo 'm的三次幂:',($m**3), "</br>";
    },5);//m的三次幂:125
    
    1. 内部函数:只有在外部函数调用后才可以被调用

      <?php
      
      function outer(){
          echo "我是外部". "</br>";
          function inner(){
              echo "我是内部", "</br>";
      
              //outer();//引起致命错误,内部不能调用外部函数
          }
      }
      outer();
      inner();
      
    2. 有条件函数:好鸡肋啊

一、字符串函数

1. strlen函数:获取字符串长度
    int strlen(string $str)
    注意:不建议获取中文字符串的长度
    2. strtolower函数:将字符串转换为小写
    string strtolower(string $str)
    3. strtoupper函数:将字符串大写
    string strtoupper(string $str)
    4. str_replace函数:实现字符替换,区分大小写
    mixed str_replace(mixid $search,mixed replace,mixed $object,[int $count])
    该函数必须遵循下列规则:
        如果搜索的字符串是一个数组,那么它将返回一个数组。
        如果搜索的字符串是一个数组,那么它将对数组中的每个元素进行查找和替换。
        如果同时需要对某个数组进行查找和替换,并且需要执行替换的元素少于查找到的元素的数量,那么多余的元素将用空字符串进行替换。
        如果是对一个数组进行查找,但只对一个字符串进行替换,那么替代字符串将对所有查找到的值起作用。
        注释:该函数是区分大小写的。请使用 str_ireplace() 函数执行不区分大小写的搜索。

        注释:该函数是二进制安全的。
    5. str_ireplace函数:用法同上,除了不区分大小写
    6. ucfirst函数:将句子首字母转换为大写
    string ucfirst(string $str)
    7.ucwords函数:将每个单词的首字母转换为大写
    string ucwords(string $str)   
    8.sprintf函数:详细看下方链接
    https://account.cnblogs.com/signin?ReturnUrl=https:%2F%2Fwz.cnblogs.com%2Fcreate%3Ft%3Dc3ByaW50ZuWHveaVsHBocOeahOivpue7huS9v%2BeUqOaWueazlSAtIOS4jeivtO%2B8jCAtIOWNmuWuouWbrQ%3D%3D%26u%3Dhttps%253A%252F%252Fwww.cnblogs.com%252Fbushuo%252Farticles%252F5657730.html%26c%3D%26bid%3D5657730%26i%3D0%26base64%3D1

二、数字函数

1. rand函数:产生随机数
    int rand(int $min,int $max)
    2. mt_rand :将产生一个更好的随机数
    int mt_rand(int $min,int $max)
    3. round函数:四舍五入
    float round(float $val[,int $precision=0])
    参数:int $precision  :保留小数点后几位,默认为0
    4. number_format:将以千分位分隔符格式化数字,参数同上
        说明:最后一位会四舍五入
    5.ceil(int $n):向上取整
        floor(int $n):向下取整
    6.pow(number $base,number $exp):幂运算
        sqrt(float $arg):平方根
    7. mixed max(mixed $value,...)  最大值
        mixed min(mixed $value,...) 最小值
    8.float fmod(float $x,float $y):除法的浮点数余数

三、时间函数

1. date :格式化一个本地时间/日期
string date(string format[,int timestamp])
2. time : 当前Unix时间戳
    int time(void)
3.strtotime:将字符串转为Unix时间戳
    int strtotime(string $time[,int $now=time()])
4.bool date_default_timezone_set(string timezone_identifier):设置默认时区
  string date_default_timezone_get(void):获取默认时区
5.array getdate([int timestamp]):可以获取日期/时间信息
6.string uniqid([string $prefix=""[,bool $more_entropy=false]]):生成唯一ID
    
    uuid 格式:8-4-4-4-12 = 32
    写法:md5(uniqid(microtime().mt_rand()))
上一篇 下一篇

猜你喜欢

热点阅读