PHP常用函数&...

liuliwei · · 23 次点击 · · 开始浏览    

1 连接数据库

$localhost='127.0.0.1';
$username = 'username';
$password = 'password';
$database = 'db';
$port = '3306';
$sqlConnect = new mysqli($localhost,$username,$password,$database,$port);

2 执行sql

$query = $connect->query('select * from table');

3 常用取数据库数据

$result = array();
while ($Data = mysqli_fetch_array($query,MYSQLI_ASSOC))
{
    $result[] = $Data;
}

 4 连接Redis(需要添加PHP扩展Redis:http://www.cnblogs.com/liuliwei/p/8243143.html)

/**
 * mysqli_fetch_array($query,MYSQLI_ASSOC))
 * 参数2表示索引方式
 * MYSQL_ASSOC - 关联数组
 * MYSQL_NUM - 数字数组
 * MYSQL_BOTH - 默认。同时产生关联和数字数组
 */
$result = array();
while ($Data = mysqli_fetch_array($query,MYSQLI_ASSOC))
{
$result[] = $Data;
}

 5 字符串拆分数组  explode(separator,string,limit)  limit可选(正数,负数,0)

$str = '1,2,3,4,5';
var_dump(explode(',',$str));
//Output:array(5) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1) "4" [4]=> string(1) "5" }
print_r(explode(',',$str,0));
//Output: Array ( [0] => 1,2,3,4,5 )
print_r(explode(',',$str,3));
// Output:Array ( [0] => 1 [1] => 2 [2] => 3,4,5 )
print_r(explode(',',$str,-2));
//Output:Array ( [0] => 1 [1] => 2 [2] => 3 )

6 合并数组  array_merge(array,array,array...)

7 返回数组单列值  array_column(array,columnKey,indexKey)  index_key可选

$array = array(
    array('id'=>'1111','name'=>'first','nick'=>'one'),
    array('id'=>'2222','name'=>'second','nick'=>'two'),
    array('id'=>'3333','name'=>'third','nick'=>'three'),
    array('id'=>'4444','name'=>'fourth','nick'=>'four')
);
var_dump(array_column($array,'name'));
//Output:array(4) { [0]=> string(5) "first" [1]=> string(6) "second" [2]=> string(5) "third" [3]=> string(6) "fourth" }
var_dump(array_column($array,'name','id'));
//Output:array(4) { [1111]=> string(5) "first" [2222]=> string(6) "second" [3333]=> string(5) "third" [4444]=> string(6) "fourth" }

 8 返回相反顺序的数组元素  array_reverse(array,bool)  bool可选,true反转保留原Key

$array = array(
    array('id'=>'1111','name'=>'first'),
    '1'=>'2222','2'=>'second','3'=>'two'
);
var_dump($array);
//Output:array(4) { [0]=> array(2) { ["id"]=> string(4) "1111" ["name"]=> string(5) "first" } [1]=> string(4) "2222" [2]=> string(6) "second" [3]=> string(3) "two" }
var_dump(array_reverse($array));
//Output:array(4) { [0]=> string(3) "two" [1]=> string(6) "second" [2]=> string(4) "2222" [3]=> array(2) { ["id"]=> string(4) "1111" ["name"]=> string(5) "first" } }
var_dump(array_reverse($array,true));
//Output:array(4) { [3]=> string(3) "two" [2]=> string(6) "second" [1]=> string(4) "2222" [0]=> array(2) { ["id"]=> string(4) "1111" ["name"]=> string(5) "first" } }

9 查找字符串在另一个字符串中的位置  strpos(),stripos(),strripos(),strrpos()

var_dump(strpos("I need to find the character A from 6","a",22));//int(23)
//查找字符串在另一字符串中第一次出现的位置,区分大小写
var_dump(stripos("I need to find the character A from 6","a",22));//int(23)
//查找字符串在另一字符串中第一次出现的位置,不区分大小写
var_dump(strripos("I need to find the character A from 6","a",22));//int(29)
//查找字符串在另一字符串中最后一次出现的位置,不区分大小写
var_dump(strrpos("I need to find the character A from 6","a",22));//int(23)
//查找字符串在另一字符串中最后一次出现的位置,区分大小写

 10 字符串大小写

$str = "just hEllo WorlD!";
var_dump(strtoupper($str));//string(17) "JUST HELLO WORLD!"
//字符串字符转大写
var_dump(lcfirst($str));//string(17) "just hEllo WorlD!"
//字符串首字符转小写
var_dump(strtolower($str));//string(17) "just hello world!"
//字符串转小写
var_dump(ucfirst($str));//string(17) "Just hEllo WorlD!"
//字符串首字符转大写
var_dump(ucwords($str));//string(17) "Just HEllo WorlD!"
//字符串每个单词首字符转大写

11 字符串长度  strlen(string)

12 查看变量是否存在  isset()

//若变量存在且其值为NULL,也返回FALSE
//若变量存在且其值不为NULL,则返回TRUE
//同时检查多个变量时,每个单项都符合上一条要求时才返回TRUE,否则返回FALSE

13 获取变量的类型  gettype(var)  返回较慢

14 返回数组非键值得所有值  array_values(array)

$array = array(
    array('id'=>'1111','name'=>'first'),
    '1'=>'2222','2'=>'second','3'=>'two'
);
var_dump(array_values($array));
//Output:array(4) { [0]=> array(2) { ["id"]=> string(4) "1111" ["name"]=> string(5) "first" } [1]=> string(4) "2222" [2]=> string(6) "second" [3]=> string(3) "two" }

15 操作本地文件  opendir(filePath),readdir(opendir(filePath)),closedir(opendir(filePath))

$openPath = opendir('D:\Program Files (x86)\Redis');
while (false!==($file = readdir($openPath))) {
    $files[] = $file;
}
sort($files);//按元素字母排序
var_dump($files);
//array(19) { [0]=> string(1) "." [1]=> string(2) ".." [2]=> string(8) "dump.rdb"}
closedir($openPath);

16 打乱字符串  str_shuffle(string)

$str = "qwertyuiopasdfghjkl";
var_dump(str_shuffle($str));//string(19) "agieksuplytwfrjqdho"(非定值)

17 截取字符串  substr(string,start,length)

$str = "qwertyuiopasdfghjkl";
var_dump(substr($str,0,6));//string(6) "qwerty"
var_dump(substr($str,6,6));//string(6) "uiopas"
var_dump(substr($str,-6,6));// string(6) "fghjkl"

 

本文来自:博客园

感谢作者:liuliwei

查看原文:PHP常用函数&...

23 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet