对数字取整
四舍五入:round()根据参数2指定精度将参数1进行四舍五入。参数2可以是负数或零(默认值)
round(3.14159); // 3
round(3.64159); // 4
round(3.64159, 2); // 3.64
直接取整:intval—对变数转成整数型态
intval(3.14159); // 3
intval(3.64159); // 3
explode()
explode() 函数把字符串分割为数组,多用于字符切割为数组,进而进行后续操作
$str = 'one,two,three,four';
dump(explode(',',$str));die;
打印结果:
array (size=4)
0 => string 'one' (length=3)
1 => string 'two' (length=3)
2 => string 'three' (length=5)
3 => string 'four' (length=4)
implode()
把数组元素组合为一个字符串:
$arr = array('Hello','World!','Beautiful','Day!');
dump(implode(",",$arr));die
打印结果:
string 'Hello World! Beautiful Day!' (length=27)
strtotime
将英文文本日期时间解析为 Unix 时间戳:
dump(strtotime("now"));die;
打印结果:
int 1576047793
date(“Y-m-d H:i:s”)
将 Unix 时间戳解析为英文文本日期时间:
dump(date("Y-m-d H:i:s"));die;
打印结果:
string '2019-12-11 15:10:50' (length=19)
trim()
移除字符串左侧的字符
$str = "Hello World!";
echo $str . "<br>";
echo trim($str,"!");die;
打印结果:
Hello World!
Hello World
但是 有一次我使用trim()方法翻车了
$str = "zz_zz";
echo $str . "<br>";
echo trim($str,"zz_");die;
打印结果:
zz_zz
<--这里竟然为空 ps:不知道为什么,现在也不知道原因
str_replace()
该函数返回替换后的数组或者字符串。
$str = "zz_zz";
echo $str . "<br>";
echo str_replace('zz_','',$str);die;
打印结果:
zz_zz
zz
:这个方法是我用trim()翻车之后用的一个方法
empty()
该函数用于检查一个变量是否为空,当一个变量并不存在,或者它的值等同于 FALSE,那么它会被认为不存在。
当变量为
- “” (空字符串)
- 0 (作为整数的0)
- 0.0 (作为浮点数的0)
- “0” (作为字符串的0)
- NULL
- FALSE
- array() (一个空数组)
- $var; (一个声明了,但是没有值的变量)
时,会被认为是空的
$a=0;
$b='root';
if (empty($a))
{
echo '$a' . " 为空或为 0。";
}
else
{
echo '$a' . " 不为空或不为 0。";
}
if (empty($b))
{
echo '$b' . " 为空或为 0。";
}
else
{
echo '$b' . " 字符串不为空或不为0。";
}
die;
打印结果:
$a 为空或为 0。
$b 字符串不为空或不为0。
is_numeric()
用于检测变量是否为数字或数字字符串。
$a=678.5;
$b="a678.5";
if (is_numeric($a))
{
echo "$a 是数字";
}
else
{
echo "$a 不是数字";
}
if (is_numeric($b))
{
echo "$b 是数字";
}
else
{
echo "$b 不是数字";
}
die;
打印结果:
678.5 是数字
a678.5 不是数字
strtoupper()
把所有字符转换为大写:
echo strtoupper("Hello WORLD.");die;
打印结果:
HELLO WORLD.
strtolower
把所有字符转换为小写
echo strtolower("Hello WORLD.");die;
打印结果:
hello world.
array_column
返回数组中指定的一列
$records = array(
array(
'id' => 2135,
'first_name' => 'John',
'last_name' => 'Doe',
),
array(
'id' => 3245,
'first_name' => 'Sally',
'last_name' => 'Smith',
),
array(
'id' => 5342,
'first_name' => 'Jane',
'last_name' => 'Jones',
),
array(
'id' => 5623,
'first_name' => 'Peter',
'last_name' => 'Doe',
)
);
$first_names = array_column($records, 'first_name');
print_r($first_names);
打印结果:
Array
(
[0] => John
[1] => Sally
[2] => Jane
[3] => Peter
)
array_count_values
统计数组中所有的值
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
打印结果:
Array
(
[1] => 2
[hello] => 2
[world] => 1
)
array_search
在数组中搜索给定的值,如果成功则返回首个相应的键名
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array); // $key = 1;
array_merge
将一个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面。返回作为结果的数组。
通俗一点说就是后面的覆盖前面的
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
打印结果:
Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)
array_keys
返回数组中部分的或所有的键名
个人理解:获取数组键名
<?php
$array = array(0 => 100, "color" => "red");
print_r(array_keys($array));
$array = array("blue", "red", "green", "blue", "blue");
print_r(array_keys($array, "blue"));
$array = array("color" => array("blue", "red", "green"),
"size" => array("small", "medium", "large"));
print_r(array_keys($array));
?>
打印结果:
Array
(
[0] => 0
[1] => color
)
Array
(
[0] => 0
[1] => 3
[2] => 4
)
Array
(
[0] => color
[1] => size
)
array_values
返回数组中所有的值
个人理解:获取数组键值
<?php
$array = array("size" => "XL", "color" => "gold");
print_r(array_values($array));
?>
打印结果:
Array
(
[0] => XL
[1] => gold
)
substr
返回字符串的子串
<?php
$rest = substr("abcdef", -1); // 返回 "f"
$rest = substr("abcdef", -2); // 返回 "ef"
$rest = substr("abcdef", -3, 1); // 返回 "d"
$rest = substr("abcdef", 0, -1); // 返回 "abcde"
$rest = substr("abcdef", 2, -1); // 返回 "cde"
$rest = substr("abcdef", 4, -4); // 返回 ""
$rest = substr("abcdef", -3, -1); // 返回 "de"
?>
array_chunk
将一个数组分割成多个
<?php
$input_array = array('a', 'b', 'c', 'd', 'e');
print_r(array_chunk($input_array, 2));
print_r(array_chunk($input_array, 2, true));
?>
打印结果:
Array
(
[0] => Array
(
[0] => a
[1] => b
)
[1] => Array
(
[0] => c
[1] => d
)
[2] => Array
(
[0] => e
)
)
Array
(
[0] => Array
(
[0] => a
[1] => b
)
[1] => Array
(
[2] => c
[3] => d
)
[2] => Array
(
[4] => e
)
)
array_flip
交换数组中的键和值
<?php
$input = array("oranges", "apples", "pears");
$flipped = array_flip($input);
print_r($flipped);
?>
打印结果:
Array
(
[oranges] => 0
[apples] => 1
[pears] => 2
)
array_diff_key
使用键名比较计算数组的差集
<?php
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
//白话:$array2里面的的key如果和$array1的key相同 只获取$array1不同的内容
var_dump(array_diff_key($array1, $array2));
?>
打印结果:
array(2) {
["red"]=>
int(2)
["purple"]=>
int(4)
}
file_put_contents
将一个字符串写入文件
<?php
$file = 'people.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "John Smith\n";
// Write the contents back to the file
file_put_contents($file, $current);
?>
file_exists
检查文件或目录是否存在
<?php
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>
unlink
删除文件
<?php
//例1:
$fh = fopen('test.html', 'a');
fwrite($fh, '<h1>Hello world!</h1>');
fclose($fh);
unlink('test.html');
//例2:
//定时删除31天前的的log文件 前提是文件名为时间名 比如20200110.log
$path = __DIR__.'\\log\\'; // 文件路径
$delfile = $path.date('Ymd',strtotime('-31 day')).'.log';
if (file_exists($delfile)) {
unlink($delfile);
}
?>
max()
找出最大值
<?php
echo max(1, 3, 5, 6, 7); // 7
echo max(array(2, 4, 5)); // 5
// When 'hello' is cast as integer it will be 0. Both the parameters are equally
// long, so the order they are given in determines the result
echo max(0, 'hello'); // 0
echo max('hello', 0); // hello
echo max('42', 3); // '42'
// Here 0 > -1, so 'hello' is the return value.
echo max(-1, 'hello'); // hello
// With multiple arrays of different lengths, max returns the longest
$val = max(array(2, 2, 2), array(1, 1, 1, 1)); // array(1, 1, 1, 1)
// 对多个数组,max 从左向右比较。
// 因此在本例中:2 == 2,但 4 < 5
$val = max(array(2, 4, 8), array(2, 5, 7)); // array(2, 5, 7)
// 如果同时给出数组和非数组作为参数,则总是将数组视为
// 最大值返回
$val = max('string', array(2, 5, 7), 42); // array(2, 5, 7)
?>
min()
找出最小值
<?php
echo min(2, 3, 1, 6, 7); // 1
echo min(array(2, 4, 5)); // 2
echo min(0, 'hello'); // 0
echo min('hello', 0); // hello
echo min('hello', -1); // -1
// 对多个数组,min 从左向右比较。
// 因此在本例中:2 == 2,但 4 < 5
$val = min(array(2, 4, 8), array(2, 5, 1)); // array(2, 4, 8)
// 如果同时给出数组和非数组作为参数,则不可能返回数组,因为
// 数组被视为最大的
$val = min('string', array(2, 5, 7), 42); // string
?>
isset
检测变量是否已设置并且非 NULL
<?php
$var = '';
// 结果为 TRUE,所以后边的文本将被打印出来。
if (isset($var)) {
echo "This var is set so I will print.";
}
// 在后边的例子中,我们将使用 var_dump 输出 isset() 的返回值。
// the return value of isset().
$a = "test";
$b = "anothertest";
var_dump(isset($a)); // TRUE
var_dump(isset($a, $b)); // TRUE
unset ($a);
var_dump(isset($a)); // FALSE
var_dump(isset($a, $b)); // FALSE
$foo = NULL;
var_dump(isset($foo)); // FALSE
?>
array_shift
将数组开头的单元移出数组
<?php
$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_shift($stack);
print_r($stack);
?>
以上例程会输出:
Array
(
[0] => banana
[1] => apple
[2] => raspberry
)
array_pop
弹出数组最后一个单元(出栈)
<?php
$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_pop($stack);
print_r($stack);
?>
以上例程会输出:
Array
(
[0] => orange
[1] => banana
[2] => apple
)
array_slice
从数组中取出一段
<?php
$input = array("a", "b", "c", "d", "e");
$output = array_slice($input, 2); // returns "c", "d", and "e"
$output = array_slice($input, -2, 1); // returns "d"
$output = array_slice($input, 0, 3); // returns "a", "b", and "c"
// note the differences in the array keys
print_r(array_slice($input, 2, -1));
print_r(array_slice($input, 2, -1, true));
?>
以上例程会输出:
Array
(
[0] => c
[1] => d
)
Array
(
[2] => c
[3] => d
)
arrary_merge
合并一个或多个数组
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
以上例程会输出:
Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)
项目中实实在在遇到的,不断记录,更新。