常用实用的php函数小结
来源:互联网 发布:安卓条形码扫描源码 编辑:程序博客网 时间:2024/05/21 18:36
常用实用的php函数小结
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"
]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"
]strtoupper():把字符串转换为大写
举个例子,代码如下:
public function test()
{
$a = 'fdsfdsMN';
$res = strtoupper($a);
dd($res);
}输出结果为:
FDSFDSMN
strtolower():把字符串转换为小写
举个例子,代码如下:
public function test()
{
$a = 'fdsfdsMN';
$res = strtolower($a);
dd($res);
}输出结果为:
fdsfdsmn
ucfirst():字符串首字母大写
举个例子,代码如下:
public function test()
{
$a = 'fdsfdsMN';
$res = ucfirst($a);
dd($res);
}输出结果为:
FdsfdsMN
ucwords():字符串每个单词首字符转为大写
举个例子,代码如下:
public function test()
{
$a = 'hello my world';
$res = ucwords($a);
dd($res);
}输出结果为:
Hello My World
- unset():销毁指定的变量
trim():移除字符串两侧的空白字符或者其他
举个例子,代码如下:
public function test()
{
$a = 'hellomyworld';
$res = trim($a,'helld');
dd($res);
}输出结果为:
omywor
in_array():数组中是否存在指定的值
举个例子,代码如下:
public function test()
{
$a = array('a','b','c','d');
if(in_array('c',$a)){
echo '在其中';
}else{
echo '不在其中';
}
}输出结果为:
在其中
- nl2br():在字符串中的每个新行(\n)之前插入HTML换行符
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"}
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"
]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"
]implode():把数组组合为字符串
举个例子,代码如下:
public function test()
{
$arr = array('a','b','c','d');
$res = implode(',',$arr);
dd($res);
}输出结果为:
"a,b,c,d"
substr():截取字符串
举个例子,代码如下:
public function test()
{
$str = '2017-09-21 12:24:12';
$res = substr($str,0,10);
dd($res);
}输出结果为:
"2017-09-21"
str_replace():字符串替换操作,区分大小写
举个例子,代码如下:
public function test()
{
$str = '你好吗?';
$res = str_replace('好','不好',$str);
dd($res);
}输出结果为:
"你不好吗?"
strlen():统计字符串长度
举个例子,代码如下:
public function test()
{
$a = strlen('rewrwr');
dd($a);
}输出结果为:
6
注意:一个中文字等于三个字符串
md5():字符串md5编码
举个例子,代码如下:
public function test()
{
$str = '你好吗?';
$res = md5($str);
dd($res);
}输出结果为:
"bb0b6bc45375143826f72439e050743e"
- time()返回当前的Unix时间戳time();返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的秒数
strtotime():将任何英文文本的日期时间描述解析为时间戳
举个例子,代码如下:
public function test()
{
$time = strtotime('2017/6/15');
dd($time);
}输出结果为:
"1497456000"
intval():获取变量的整数值
举个例子,代码如下:
public function test()
{
$data = intval('2.3');
dd($data);
}输出结果为:
"2"
abs():求绝对值
举个例子,代码如下:
public function test()
{
$data = abs('-2.22');
dd($data);
}输出结果为:
"2.22"
ceil():进一法取整
举个例子,代码如下:
public function test()
{
$data = ceil('2.2');
dd($data);
}输出结果为:
"3.0"
floor():舍去法取整
举个例子,代码如下:
public function test()
{
$data = floor('2.22');
dd($data);
}输出结果为:
"2.0"
fmod():浮点数取余
举个例子,代码如下:
public function test()
{
$data = fmod(5,3);
dd($data);
}输出结果为:
"2.0"
round():浮点数四舍五入
举个例子,代码如下:
public function test()
{
$data = round(5.3);
dd($data);
}输出结果为:
"5.0"
sqrt():求平方根
举个例子,代码如下:
public function test()
{
$data = sqrt(6);
dd($data);
}输出结果为:
2.4494897427832
max():求最大值
举个例子,代码如下:
public function test()
{
$data = max(6,7,8,2,3);
dd($data);
}输出结果为:
8
min():求最小值
mt_rand():更好的随机数
举个例子,代码如下:
public function test()
{
$data = mt_rand(1000,9999);
dd($data);
}输出结果为:
8601
rand():随机数随机返回范围内的值
举个例子,代码如下:
public function test()
{
$data = rand(1000,9999);
dd($data);
}输出结果为:
6798
pi():获取圆周率值
举个例子,代码如下:
public function test()
{
$data = pi();
dd($data);
}输出结果为:
3.1415926535898
- 常用实用的php函数小结
- 非常实用的PHP常用函数汇总
- 非常实用的PHP常用函数汇总
- 非常实用的PHP常用函数汇总
- php常用函数小结
- PHP常用函数小结
- php中7个实用及常用的函数
- 非常实用的PHP常用函数汇总(一)
- PHP常用字符串函数小结
- php常用字符串函数小结
- php常用字符串函数小结
- php常用字符串函数小结
- PHP取整数函数常用的四种方法小结
- 超实用的PHP函数
- 九个实用的PHP函数
- PHP 正则表达式常用函数使用小结
- PHP 正则表达式常用函数使用小结
- PHP 正则表达式常用函数使用小结
- web前端笔记:CSS top属性问题
- linux命令之管理输入输出
- 微软灯管问题
- Android优秀开源项目
- ford: ($27$67)Security Access
- 常用实用的php函数小结
- CentOS7下使用yum快速安装配置oracle数据库
- 6.RabbitMQ特性
- ViewGroup事件分发机制源码分析
- springmvc@RequestMapping-params参数规则
- opencv+mfc搭建框架
- QNX系统BSP开发研发与应用,QNX技术解答-项目开发-软件开发
- Maven本地仓库越用越大,删除无效版本文件
- linux下PHP项目写入日志失败,fopen(xxx.log): failed to open stream: Permission denied