• php基础-预定义函数


    操作变量常用函数

    gettype:获取变量数据类型

            $a=11;
            $b="11";
            $c=true;
            $d=11.11;
    
            echo gettype($a)."<br>";  //integer
            echo gettype($b)."<br>";  //string
            echo gettype($c)."<br>";  //boolean
            echo gettype($d)."<br>";  //double
    

    settype:永久更改变量数据类型
    is_array():检查是否是数据,返回true或者false
    is_数据类型关键字():检查变量是否是对应的数据类型
    is_null(): 是否是null
    is_callable(): 检查变量是否是有效的 函数名称
    isset():变量是否存在 即;是否被初始化
    unset():销毁变量
    empty() 检查变量是否存在;并且变量的值是否为空 null 或者是0

    字符串函数

    strpos() 查找字符位置:
    strstr ( ) 输出指定字符串
    str_replace() 替换字符串
    strlen() 获取字符串长度
    strcmp () 比较字符函数
    trim() 去除空格:
    implode() 连接字符串
    strtoupper ( ) , strtolower() 字符大小写转换函数
    strval( ) 获取变量的字符串
    

    strpos:注意事项判断时需要使用 ===

    <?php
    $mystring = 'abc';
    $findme   = 'a';
    $pos = strpos($mystring, $findme);
    
    // 注意这里使用的是 ===。简单的 == 不能像我们期待的那样工作,
    // 因为 'a' 是第 0 位置上的(第一个)字符。
    if ($pos === false) {
        echo "The string '$findme' was not found in the string '$mystring'";
    } else {
        echo "The string '$findme' was found in the string '$mystring'";
        echo " and exists at position $pos";
    }
    ?>
    

    strstr 参数说明strstr ( string $haystack , mixed $needle [, bool $before_needle = FALSE ] ) : string

    haystack
    输入字符串。

    needle
    如果 needle 不是一个字符串,那么它将被转化为整型并且作为字符的序号来使用。

    before_needle
    若为 TRUE,strstr() 将返回 needle 在 haystack 中的位置之前的部分。

    返回 haystack 字符串从 needle 第一次出现的位置开始到 haystack 结尾的字符串。(不管后面有没有再出现target)

    Note:

    该函数区分大小写。如果想要不区分大小写,请使用 stristr()。

    Note:

    如果你仅仅想确定 needle 是否存在于 haystack 中,请使用速度更快、耗费内存更少的 strpos() 函数。

    <?php
    $email  = 'name@example.com';
    $domain = strstr($email, '@');
    echo $domain; // 打印 @example.com
    
    $user = strstr($email, '@', true); // 从 PHP 5.3.0 起
    echo $user; // 打印 name
    ?>
    

    数学函数:

    1. abs() 绝对值:
    2. ceil() 进一法取整:
    3. floor() 舍去法取整:
    4. round() 对浮点数进行四舍五入:
    5. rand() 产生一个随机整数
    6. max 找出最大值:
    7. min 找出最小值:
    8. number_format () 格式化数字:
    9. intval() 获取变量的整数值
    

    数组函数:

    1.array_values 返回数组中所有的值
    2.array_keys 返回数组中所有的键名
    3.in_array 检查数组中是否存在某个值
    4.array_flip 交换数组中的键和值
    5.array_reverse 返回一个单元顺序相反的数组
    

    时间函数

    1.time() 时间戳
    2.date() 格式化时间
    3.date_default_timezone_set('PRC') 设置时区 PRC:中国
    4.strtotime()
    转换为Unix时间戳修改时间单词: year(年) month(月)
    day(日) hours(时) minute(分) second(秒)
    5.mktime() 日期unix时间戳
    

    输出函数

    echo语句 值(字符串形式) 数组:Array 对象:无法查看(toString
    除外)
    print_r()函数 值(字符串形式) 格式化输出,加参数true,输出到字
    符串
    var_dump()函 数
    类型(字符串形式)、值(字符串
    形式) 类型、长度、值
    

    导入函数:

    1.include 语句包含并运行指定文件
    2.include_once 和 include 语句类似,唯一区别是如果该文件中已经被包含过,则不会再次包
    含。
    3.require require 在出错时将导致脚本中止而 include 只产生警告,脚本会继续运行。
    4.require_once
    和require类似,PHP 会检查该文件是否已经被包含过,如果是则不会再次包
    含
    

    函数使用案例:

    封装一个函数,返回上一周的当前时间,使用日期函数

    function return_time($a){
     	return date("Y-m-d H:i:s",strtotime("-$a day"));
     }
    

    封装一个函数,找出数组里面的最大值,实现max函数的效果,
    不使用系统函数

     $arr=[1,4,6,9,8,7,3];
     function getMAX($array){//$arr
     	$maxNum=0;//1 4 
     	foreach ($array as $k => $v) {//$arr
     		if ($maxNum<$v) {//4<6
     			$maxNum = $v;
     		}
     	}
     	return $maxNum;
     }
    

    封装一个函数,这个函数可以实现从0123456789a-zA-Z这62个字符中随机生成6位字符串或者8位字符串。
    思考题 如何去重,达到随机的字符里面没有重复的
    非去重:

    function get_rand()
    {
        $str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $rand_str = '';
        for ($i = 0; $i < 6; $i++) {
            $rand_str .= $str[rand(0, strlen($str) - 1)];
        }
    
    
        echo $rand_str;
    }
    

    去重:

     function get_rand($num){
     	$str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
     	$rand_str = "";
     	while (strlen($rand_str)< $num) {
     		$s=$str[rand(0,strlen($str)-1)];
     		if(!strpos($rand_str, $s)){
     			$rand_str.=$s;
     		}
    
     	}
     	return $rand_str;
     }
    
  • 相关阅读:
    【PAT甲级】1043 Is It a Binary Search Tree (25 分)(判断是否为BST的先序遍历并输出后序遍历)
    Educational Codeforces Round 73 (Rated for Div. 2)F(线段树,扫描线)
    【PAT甲级】1042 Shuffling Machine (20 分)
    【PAT甲级】1041 Be Unique (20 分)(多重集)
    【PAT甲级】1040 Longest Symmetric String (25 分)(cin.getline(s,1007))
    【PAT甲级】1039 Course List for Student (25 分)(vector嵌套于map,段错误原因未知)
    Codeforces Round #588 (Div. 2)E(DFS,思维,__gcd,树)
    2017-3-9 SQL server 数据库
    2017-3-8 学生信息展示习题
    2017-3-5 C#基础 函数--递归
  • 原文地址:https://www.cnblogs.com/justsus/p/13958986.html
Copyright © 2020-2023  润新知