项目开发经常使用PHP功能

时间:2023-03-09 16:09:14
项目开发经常使用PHP功能

日期操作

为了便于存储、比较和交付。我们通常使用strtotime()功能转换的日期UNIX时间戳。有仅用于在显示给用户时date()成经常使用的时间格式。

strtotime()
 函数将不论什么英文文本的日期时间描写叙述解析为 Unix 时间戳

eg:

<?php
echo(strtotime("now"));
echo(strtotime("3 October 2005"));
echo(strtotime("+5 hours"));
echo(strtotime("+1 week"));
echo(strtotime("+1 week 3 days 7 hours 5 seconds"));
echo(strtotime("next Monday"));
echo(strtotime("last Sunday"));
? >

输出:

1138614504

1128290400

1138632504

1139219304

1139503709

1139180400

1138489200

date()函数 将时间戳转换成经常使用的日期格式

eg:

echo date('Y-m-d H:i:s',"1138614504");

输出:

2006-01-30 17:48:24

字符串操作

有时候须要取得某个字符串的一部分,就须要用到字符串的截取substr()函数

substr()函数返回字符串的一部分

语法:

substr(string,start,length)

eg:

echo substr("Hello world!",6,5);

输出:

world

数组操作

这里介绍两个很有用的函数:

array_unique()移除数组中同样元素的个数

当几个数组元素的值相等时,仅仅保留第一个元素。其它的元素被删除。

返回的数组中键名不变。

array_filter()删除数组中为空的元素

语法:

array array_filter ( array $input [, callable $callback = "" ] )

依次将 input 数组中的每一个值传递到 callback 函数。假设 callback 函数返回 TRUE。则 input 数组的当前值会被包括在返回的结果数组中。数组的键名保留不变。

input为要循环的数组

callback为使用的回调函数,假设没有提供 callback 函数,将删除 input 中全部等值为 FALSE 的条目(能够利用这条删除数组中为空的元素)。

eg1:

<?php
function odd($var){
return($var & 1);
}
$array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
echo "Odd :\n";
print_r(array_filter($array1, "odd"));
?>

输出:

Odd :

Array

(

    [a] => 1

    [c] => 3

    [e] => 5

)

eg2:

<?php
$entry = array(
0 => 'foo',
1 => false,
2 => -1,
3 => null,
4 => ''
); print_r(array_filter($entry));
?>

输出:

Array

(

    [0] => foo

    [2] => -1

)

版权声明:本文博客原创文章,博客,未经同意,不得转载。