PHP基础 20问
isset
判定为false
的情况(5):- 未定义的变量
- 变量的值为
null
- 变量的值为空字符串
''
- 变量的值为整数
0
- 变量的值为空数组
[]
empty
判定为true
的情况(5):- 变量未定义
- 变量的值为
false
- 变量的值为空字符串
''
- 变量的值为整数
0
- 变量的值为空数组
[]
数组
[1,2,3,4,5]
变成字符串"1-2-3-4-5"
的方法(5):$arr = [1,2,3,4,5]; $str = implode('-', $arr);
获取时间日期(5):
- 当前时间日期:
date('Y-m-d H: i:s')
- 上周的今天:
date('Y-m-d H: i:s', strtotime('last week'))
- 下周的今天:
date('Y-m-d H: i:s', strtotime('next week'))
- 当前时间日期:
计算天数(5):
$start_date = new DateTime('2023-12-01'); $end_date = new DateTime(); // 当前日期 $interval = $start_date->diff($end_date); $days = $interval->days;
字符串长度(5):
$str = "Hello World"; $length = strlen($str);
字符串操作(5):
$str = "Hello World"; $first_o_position = strpos($str, 'o'); $l_count = substr_count($str, 'l');
字符串截取(5):
$str = "Hello World"; $new_str = substr($str, 6);
字符串替换(5):
$str = "abcd+efg-higk*lmn/opq"; $str = str_replace(['+', '-', '*', '/'], '|', $str);
数组操作(5):
$arr = [1, 2, 3, 4, 5]; array_unshift($arr, 0);
数组操作(5):
$arr = [1, 2, 3, 4, 5]; array_pop($arr);
数组合并(5):
$arr1 = [1, 2, 3, 4, 5]; $arr2 = [6, 7, 8, 9, 10]; $merged_array = array_merge($arr1, $arr2);
数组截取(5):
$arr = [1, 2, 3, 4, 5]; $sliced_array = array_slice($arr, 0, 3);
数组过滤(5):
$arr = ['id' => 1, 'name' => '张三', 'age' => null]; $arr = array_filter($arr, function ($value) { return $value !== null; });
数组统计次数(5):
$arr = [0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3]; $count_values = array_count_values($arr);
数组去重(5):
$arr = [0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3]; $unique_array = array_unique($arr);
数组反转(5):
$arr = [1, 2, 3, 4, 5, 6, 7, 8]; $reversed_array = array_reverse($arr);
数组删除值为5的元素(5):
$arr = [1, 3, 4, 2, 5, 6]; $arr = array_diff($arr, [5]);
二维数组取出id字段(5):
$arr = [['id'=>1, 'name'=>'张三'],['id'=>2, 'name'=>'李四'],['id'=>3, 'name'=>'王五']]; $ids = array_column($arr, 'id');
判断两个数组是否相等并获取差异(5):
$arr1 = [1, 2, 3, 4, 5]; $arr2 = [1, 2, 3, 4, 5, 6]; $diff = array_diff($arr1, $arr2);
本作品采用《CC 协议》,转载必须注明作者和本文链接