Home  >  Article  >  Backend Development  >  PHP common function collection

PHP common function collection

不言
不言Original
2018-04-28 12:20:593548browse

This article mainly introduces the collection of commonly used functions in PHP, which has certain reference value. Now I share it with everyone. Friends in need can refer to it



PHP is_numeric() function


Created by Chen, last modified 2016-12-02

Definition and usage

is_numeric() — Check whether the variable is a number or a numeric string

Syntax

bool is_numeric ( mixed $var )

Returns if var is a number or a numeric string TRUE, otherwise FALSE is returned.

Allow passing Enter any parameters.
Parameter Description
##var
Technical details

Return value:


实例 

<?php
$Temperature = array(666,&#39;w3cschool&#39;,&#39;666&#39;,null);
foreach ($Temperature as $key => $value) {
    
    if(is_numeric($value)){
    print("参数是数字或数字字符串<br/>");
    }else{
    print("参数不是数字或数字字符串<br/>");
    }
}
?>

--------------------------------------------------------

PHP array_column() 函数

PHP Array 函数

实例

从记录集中取出 last_name 列:

7aa58229097e66bda13dedb9481f7e1e 5698,
    'first_name' => 'Bill',
    'last_name' => 'Gates',
  ),
  array(
    'id' => 4767,
    'first_name' => 'Steve',
    'last_name' => 'Jobs',
  ),
  array(
    'id' => 3809,
    'first_name' => 'Mark',
    'last_name' => 'Zuckerberg',
  )
);

$last_names = array_column($a, 'last_name');
print_r($last_names);
?>

输出:

Array
(
  [0] => Gates
  [1] => Jobs
  [2] => Zuckerberg
)


定义和用法

array_column() 返回输入数组中某个单一列的值。

语法

array_column(array,column_key,index_key);
If var is Returns TRUE for numbers and numeric strings, otherwise returns FALSE.
参数 描述
array 必需。规定要使用的多维数组(记录集)。
column_key

必需。需要返回值的列。

可以是索引数组的列的整数索引,或者是关联数组的列的字符串键值。

该参数也可以是 NULL,此时将返回整个数组(配合 index_key 参数来重置数组键的时候,非常有用)。

index_key 可选。用作返回数组的索引/键的列。


技术细节

返回值: 返回数组,此数组的值为输入数组中某个单一列的值。
 


--------------------------------------------------------
  

PHP array_search() 函数

PHP Array 函数

实例

在数组中搜索键值 "red",并返回它的键名:

a75c26f29b812b987d8e93cb78df764e"red","b"=>"green","c"=>"blue");
echo array_search("red",$a);
?>

运行实例

定义和用法

array_search() 函数在数组中搜索某个键值,并返回对应的键名。

详细说明

array_search() 函数与 in_array() 一样,在数组中查找一个键值。如果找到了该值,匹配元素的键名会被返回。如果没找到,则返回 false。

在 PHP 4.2.0 之前,函数在失败时返回 null 而不是 false。

如果第三个参数 strict 被指定为 true,则只有在数据类型和值都一致时才返回相应元素的键名。

语法

array_search(value,array,strict)
参数 描述
value 必需。规定需要搜素的键值。
array 必需。规定被搜索的数组。
strict

可选。如果该参数被设置为 TRUE,则函数在数组中搜索数据类型和值都一致的元素。可能的值:

  • true

  • false - 默认

如果设置为 true,则在数组中检查给定值的类型,数字 5 和字符串 5 是不同的(参见实例 2)。


技术细节

返回值:

如果在数组中找到指定的键值,则返回对应的键名,否则返回 FALSE。

如果在数组中找到键值超过一次,则返回第一次找到的键值所匹配的键名。

------------------------------------------------------------

PHP in_array() 函数

PHP Array 函数

实例

在数组中搜索值 "Glenn" ,并输出一些文本:

2f3c5e570bbea39caaa2164190f641d4

运行实例

定义和用法

in_array() 函数搜索数组中是否存在指定的值。

注释:如果 search 参数是字符串且 type 参数被设置为 TRUE,则搜索区分大小写。

语法

in_array(search,array,type)
参数 描述
search 必需。规定要在数组搜索的值。
array 必需。规定要搜索的数组。
type 可选。如果设置该参数为 true,则检查搜索的数据与数组的值的类型是否相同。

说明

如果给定的值 search 存在于数组 array 中则返回 true。如果第三个参数设置为 true,函数只有在元素存在于数组中且数据类型与给定值相同时才返回 true。

如果没有在数组中找到参数,函数返回 false。

注释:如果 search 参数是字符串,且 type 参数设置为 true,则搜索区分大小写。

技术细节

返回值: 如果在数组中找到值则返回 TRUE,否则返回 FALSE。


-----------------------------------------------------------------

PHP array_unique() 函数

PHP Array 函数

实例

移除数组中重复的值:

a75c26f29b812b987d8e93cb78df764e"red","b"=>"green","c"=>"red");
print_r(array_unique($a));
?>

运行实例

定义和用法

array_unique() 函数移除数组中的重复的值,并返回结果数组。

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

返回的数组中键名不变。

注释:被保留的数组将保持第一个数组项的键名类型。

语法

array_unique(array)
参数 描述
array 必需。规定数组。
sortingtype

可选。规定如何比较数组元素/项目。可能的值:

  • SORT_STRING - 默认。把项目作为字符串来比较。

  • SORT_REGULAR - 把每一项按常规顺序排列(Standard ASCII,不改变类型)。

  • SORT_NUMERIC - 把每一项作为数字来处理。

  • SORT_LOCALE_STRING - 把每一项作为字符串来处理,基于当前区域设置(可通过 setlocale() 进行更改)。

说明


array_unique() 先将值作为字符串排序,然后对每个值只保留第一个遇到的键名,接着忽略所有后面的键名。这并不意味着在未排序的 array 中同一个值的第一个出现的键名会被保留。

技术细节

返回值: 返回被过滤的数组。


-------------------------------------------------------------------

PHP array_merge() 函数

PHP Array 函数

实例

把两个数组合并为一个数组:

0dee88675f38520f42494b9983f23902

运行实例

定义和用法

array_merge() 函数把一个或多个数组合并为一个数组。

提示:您可以向函数输入一个或者多个数组。

注释:如果两个或更多个数组元素有相同的键名,则最后的元素会覆盖其他元素。

注释:如果您仅向 array_merge() 函数输入一个数组,且键名是整数,则该函数将返回带有整数键名的新数组,其键名以 0 开始进行重新索引(参见下面的实例 1)。

提示:该函数与 array_merge_recursive() 函数之间的不同是在处理两个或更多个数组元素有相同的键名的情况。array_merge_recursive() 不会进行键名覆盖,而是将多个相同键名的值递归组成一个数组。

语法

array_merge(array1,array2,array3...)
参数 描述
array1 必需。规定数组。
array2 可选。规定数组。
array3 可选。规定数组。


技术细节

返回值: 返回合并的数组。


---------------------------------------------------------------------------------

PHP implode() 函数

PHP String 函数

实例

把数组元素组合为字符串:

54b3667f5a66e4903417a93fbf414364

运行实例

定义和用法

implode() 函数返回由数组元素组合成的字符串。

注释:implode() 函数接受两种参数顺序。但是由于历史原因,explode() 是不行的,您必须保证 separator 参数在 string 参数之前才行。

注释:implode() 函数的 separator 参数是可选的。但是为了向后兼容,推荐您使用使用两个参数。

注释:该函数是二进制安全的。

语法

implode(separator,array)
参数 描述
separator 可选。规定数组元素之间放置的内容。默认是 ""(空字符串)。
array 必需。要组合为字符串的数组。


技术细节

返回值: 返回由数组元素组合成的字符串。


---------------------------------------------------------------------------

PHP count() 函数

PHP Array 函数

实例

返回数组中元素的数目:

d90fada51e98a8b7b9b05d403d38f7f0

运行实例

定义和用法

count() 函数返回数组中元素的数目。

语法

count(array,mode);
参数 描述
array 必需。规定数组。
mode

可选。规定模式。可能的值:

  • 0 - 默认。不对多维数组中的所有元素进行计数

  • 1 - 递归地计数数组中元素的数目(计算多维数组中的所有元素)

说明

count() 函数计算数组中的单元数目或对象中的属性个数。

对于数组,返回其元素的个数,对于其他值,返回 1。如果参数是变量而变量没有定义,则返回 0。

如果 mode 被设置为 COUNT_RECURSIVE(或 1),则会递归底计算多维数组中的数组的元素个数。

技术细节

返回值: 返回数组中元素的数目。


-------------------------------------------------------------------------

PHP strtotime() 函数

PHP Date / Time 函数

实例

将英文文本日期时间解析为 Unix 时间戳:

97fe28c84384612b0fbc12423f1f4b8f");
echo(strtotime("15 October 1980") . "0c6dc11e160d3b678d68754cc175188a");
echo(strtotime("+5 hours") . "0c6dc11e160d3b678d68754cc175188a");
echo(strtotime("+1 week") . "0c6dc11e160d3b678d68754cc175188a");
echo(strtotime("+1 week 3 days 7 hours 5 seconds") . "0c6dc11e160d3b678d68754cc175188a");
echo(strtotime("next Monday") . "0c6dc11e160d3b678d68754cc175188a");
echo(strtotime("last Sunday"));
?>

运行实例

定义和用法

strtotime() 函数将任何英文文本的日期或时间描述解析为 Unix 时间戳(自 January 1 1970 00:00:00 GMT 起的秒数)。

注意:如果年份表示使用两位数格式,则值 0-69 会映射为 2000-2069,值 70-100 会映射为 1970-2000。

注意:请注意 m/d/y 或 d-m-y 格式的日期,如果分隔符是斜线(/),则使用美洲的 m/d/y 格式。如果分隔符是横杠(-)或者点(.),则使用欧洲的 d-m-y 格式。为了避免潜在的错误,您应该尽可能使用 YYYY-MM-DD 格式或者使用 date_create_from_format() 函数。

语法

strtotime(time,now);
参数 描述
time 必需。规定日期/时间字符串。
now 可选。规定用来计算返回值的时间戳。如果省略该参数,则使用当前时间。


技术细节

返回值: 若成功则返回时间戳,失败则返回 FALSE。


----------------------------------------------------------------

PHP array_values() 函数

PHP Array 函数

实例

返回数组的所有值(非键名):

9379f121e5117f2531c2325d016c1f70"Bill","Age"=>"60","Country"=>"USA");
print_r(array_values($a));
?>

运行实例

定义和用法

array_values() 函数返回一个包含给定数组中所有键值的数组,但不保留键名。

提示:被返回的数组将使用数值键,从 0 开始并以 1 递增。

语法

array_values(array)
参数 描述
array 必需。规定数组。


技术细节

返回值: 返回包含数组中所有的值的数组。


-------------------------------------------------------
import方法是ThinkPHP框架用于类库导入的封装实现,尤其对于项目类库、扩展类库和第三方类库的导入支持,import方法早期的版本可以和java的import方法一样导入目录和通配符导入,后来考虑到性能问题,在后续的版本更新中不断改进和简化了,所以现在的用法比较简单明了。调用格式:
import('类库名', '起始路径', '类库后缀')
imprt方法有一个别名vendor方法,专门用于导入第三方类库,区别在于起始路径和类库后缀默认值不同。
我们来分析下具体的用法:
导入系统基类库

系统基类库其实就是指的Think类库包,所在目录就是指框架的核心Lib目录,import方法可以用于导入系统基类库,例如:

    import('Think.Util.Array');

复制代码
表示导入系统目录下面的Lib/Util/Array.class.php 类库文件,相当于我们这样使用

 require THINK_PATH.&#39;Lib/Util/Array.class.php&#39;;

复制代码
可以支持多级目录,例如:

    import(&#39;Think.Util.U1.ClassA&#39;);

复制代码

import(&#39;Think.Util.U1.A2.ClassB&#39;);

复制代码
通过import方法导入类库后,就可以进行类库的实例化操作了。
导入扩展类库
扩展类库位于Extend/Library目录下面,这是系统的公共扩展类库目录,目前支持的扩展类库包只有ORG和Com包。

  import(&#39;ORG.Util.Image&#39;);
    import(&#39;Com.Sina.OAuth&#39;);

复制代码

会导入扩展目录下面的第三方类库(分别是Extend/Library/ORG/Util/Image.class.php和Extend/Library/Com/Sina/OAuth.class.php 类库文件),第三方类库包只能支持ORG和Com两种,下面的子目录可以随意添加。
导入项目应用类库
如果没有指定起始导入路径的话,类库包Think、ORG、Com之外的都会被认为是导入项目应用类库,例如:

import("MyApp.Action.UserAction");
    import("MyApp.Model.InfoModel");

复制代码
表示导入MyApp项目的UserAction和InfoModel类库文件,由于通常,我们都是导入当前项目下面的类库,所以可以简写成:

import("@.Action.UserAction");
    import("@.Model.InfoModel");

复制代码
@符号表示导入当前项目下面的类库,这种方式也一定程度上方便了项目类库的代码移植,如果项目名称改变或者移动到其它项目下面的时候,写法不需要改变。
导入非标准类库文件

这里所说的非标准类库文件,主要是指位于特殊位置或者非.class.php后缀的类库文件。像导入基类库、扩展类库和项目类库都是基于框架规范的目录下面,如果我们需要导入项目的Common目录下面的MyClass.php文件,则可以采用:

  import(&#39;Common.MyClass&#39;,APP_PATH,&#39;.php&#39;);

复制代码
或者

  import(&#39;MyClass&#39;,APP_PATH.&#39;Common&#39;,&#39;.php&#39;);

复制代码
或者要导入当前目录下面的RBAC类库

 import("RBAC.AccessDecisionManager",dirname(__FILE__),".php");

复制代码
还有一种特殊情况,是类库命名的特殊性。按照系统的规则,import方法是无法导入具有点号的类库文件的,因为点号会直接转化成斜线,例如我们定义了一个名称为User.Info.class.php 的文件的话,采用:

 import("ORG.User.Info");

复制代码
方式加载的话就会出现错误,导致加载的文件不是ORG/User.Info.class.php 文件,而是ORG/User/Info.class.php 文件,这种情况下,我们可以使用:

import("ORG.User#Info");

复制代码
来导入。
第三方类库导入
ThinkPHP 的基类库都是以.class.php 为后缀的,这是系统内置的一个约定,当然也可以通过 import 的参数来控制, 为了更加方便引入其他框架和系统的类库, 系统还提供了一个import方法的别名vendor,专门用于导入第三方类库,并且默认的起始目录和类文件后缀有区别。第三方类库位于系统扩展目录下的Vendor 目录, 例如,我们把 Zend 的 Filter\Dir.php 放到 Vendor 目录下面,这个时候 Dir 文件的路径就是 Vendor\Zend\Filter\Dir.php,我们使用vendor 方法导入只需要使用:

 Vendor(&#39;Zend.Filter.Dir&#39;);

复制代码

就可以导入Dir类库了。
Vendor方法也可以支持和import方法一样的基础路径和文件名后缀参数,例如:

Vendor(&#39;Zend.Filter.Dir&#39;,dirname(__FILE__),&#39;.class.php&#39;);

复制代码
别名导入
除了命名空间的导入方式外,import方法还可以支持别名导入,要使用别名导入,首先要定义别名,我们可以在项目配置目录下面增加alias.php 用以定义项目中需要用到的类库别名,例如:

   return array(
    &#39;rbac&#39; =>LIB_PATH.&#39;Common/Rbac.class.php&#39;,
    &#39;page&#39; =>LIB_PATH.&#39;Common/Page.class.php&#39;,
    );

复制代码
那么,现在就可以直接使用:

    import("rbac");
    import("page");

复制代码
导入Rbac和Page类,别名导入方式禁止使用import方法的第二和第三个参数,别名导入方式的效率比命名空间导入方式要高效,缺点是需要预先定义相关别名。
可以为某些需要的类库定义别名,那么无需定义自动加载路径也可以快速的自动加载。
一般情况下,由于框架内部采用了自动加载方式,所以大多数情况下面不需要用户手动导入类库文件,通常用于导入扩展类库和第三方类库的情况居多。而且配合别名定义和自动加载路径的定义,也能减少用户手动导入类库的情况。
------------------------------------------------------------------
is_numcomma()检测变量是否是数字或逗号。
------------------------------------------------------------------

PHP join() 函数

PHP String 函数

实例

把数组元素组合为一个字符串:

63221d4383c13cc0e471d4c0629cfe4a

运行实例

定义和用法

join() 函数返回由数组元素组合成的字符串。

join() 函数是 implode() 函数的别名。

注释:join() 函数接受两种参数顺序。但是由于历史原因,explode() 是不行的,您必须保证 separator 参数在 string 参数之前才行。

注释:join() 函数的 separator 参数是可选的。但是为了向后兼容,推荐您使用使用两个参数。

语法

join(separator,array)
参数 描述
separator 可选。规定数组元素之间放置的内容。默认是 ""(空字符串)。
array 必需。要组合为字符串的数组。


技术细节

返回值: 返回由数组元素组合成的字符串。


--------------------------------------------------------------------

PHP explode() 函数

PHP String 函数

实例

把字符串打散为数组:

317e97e6c3e994a4ad906485c1622bfb

运行实例

定义和用法

explode() 函数把字符串打散为数组。

注释:"separator" 参数不能是空字符串。

注释:该函数是二进制安全的。

语法

explode(separator,string,limit)
参数 描述
separator 必需。规定在哪里分割字符串。
string 必需。要分割的字符串。
limit

可选。规定所返回的数组元素的数目。

可能的值:

  • 大于 0 - 返回包含最多 limit 个元素的数组

  • 小于 0 - 返回包含除了最后的 -limit 个元素以外的所有元素的数组

  • 0 - 返回包含一个元素的数组


技术细节

返回值: 返回字符串的数组
PHP 版本: 4+
更新日志: 在 PHP 4.0.1 中,新增了 limit 参数。在 PHP 5.1.0 中,新增了对负数 limit 的支持。


实例

使用 limit 参数来返回一些数组元素:

c50ec8cdfe1adc1cbdc5183941e5876c


------------------------------------------------------------------


ini_set

(PHP 4,PHP 5,PHP 7)

ini_set—设置一个配置选项的值

描述

字符串 ini_set字符串 varname美元字符串 美元的价值

设置一个配置选项的值。配置选项 将脚本的执行过程中保持这种新的价值,并将恢复 在剧本的结尾。

参数

  • 变量名

  • 不是所有可用的选项可以使用ini_set()。有一个列表中的所有可用的选项 在附录。

  • 新的价值

  • 对于期权的新价值。

返回值

如果成功返回旧值,错误的对失效。

实例

 


1例# INI选项设置

<?PHP
回声ini_get(“display_errors”);

如果(!ini_get(“display_errors”)){
    ini_set(“display_errors”,“1”);
}

回声ini_get(“display_errors”);
?>

参见

  • get_cfg_var()-获取一个PHP配置选项的值

  • ini_get()-获取配置选项的值

  • ini_get_all()-获取所有配置选项

  • ini_restore()恢复一个配置选项的值

  • How to change configuration settings



------------------- ------------------------------------------------

PHP Date / Time Function

  • PHP Calendar

  • ##PHP Directory

PHP Introduction to Date/Time


The Date/Time function allows you to get the date and time from the server where the PHP script is running. You can use the Date/Time functions to format dates and times in different ways.

Note: These functions rely on the local settings of the server. Remember to take daylight saving time and leap years into account when using these functions.

Installation

PHP Date/Time functions are an integral part of the PHP core. No installation is required to use these functions.

Runtime Configuration

The behavior of the Date/Time function is affected by the settings in php.ini:

##Namedate.timezonedate.default_latitudedate.default_longitudedate.sunrise_zenithdate.sunset_zenith


PHP 5 Date/Time function

Description Default PHP version
Default time zone (all Date/Time functions use this option ) "" PHP 5.1
Default latitude (date_sunrise() and date_sunset() use this Options) "31.7667" PHP 5.0
Default longitude (date_sunrise() and date_sunset() Use this option) "35.2333" PHP 5.0
Default sunrise zenith (date_sunrise( ) and date_sunset() use this option) "90.83" PHP 5.0
Default sunset day Top (date_sunrise() and date_sunset() use this option) "90.83" PHP 5.0
Function Description
checkdate() Verify Gregorian date.
date_add() Add day, month, year, hour, minute and second to date.
date_create_from_format() Returns a new DateTime object formatted according to the specified format.
date_create() Returns a new DateTime object.
date_date_set() Set the new date.
date_default_timezone_get() Returns the default time zone used by all Date/Time functions.
date_default_timezone_set() Sets the default time zone used by all Date/Time functions.
date_diff() Returns the difference between two dates.
date_format() Returns the date formatted according to the specified format.
date_get_last_errors() Returns warnings/errors in the date string.
date_interval_create_from_date_string() Creates a DateInterval from the relevant part of the string.
date_interval_format() Format time interval.
date_isodate_set() Set ISO date.
date_modify() Modify the timestamp.
date_offset_get() Returns the time zone offset.
date_parse_from_format() Returns an associative array with detailed information about the specified date according to the specified format.
date_parse() Returns an associative array with detailed information about the specified date.
date_sub() Subtract the day, month, year, hours, minutes, and seconds from the specified date.
date_sun_info() Returns an array containing information about sunrise/sunset and dusk start/dusk end for the specified date and location.
date_sunrise() Returns the sunrise time of the specified date and location.
date_sunset() Returns the sunset time of the specified date and location.
date_time_set() Set the time.
date_timestamp_get() Returns the Unix timestamp.
date_timestamp_set() Sets the date and time based on a Unix timestamp.
date_timezone_get() Returns the time zone of the given DateTime object.
date_timezone_set() Set the time zone of the DateTime object.
date() Format local date and time.
getdate() Returns the date/time information of a timestamp or the current local date/time.
gettimeofday() Returns the current time.
gmdate() Format GMT/UTC date and time.
gmmktime() Returns the UNIX timestamp of the GMT date.
gmstrftime() Format GMT/UTC date and time according to locale settings.
idate() Format local time/date into an integer.
localtime() Returns the local time.
microtime() Returns the number of microseconds in the current time.
mktime() Returns the Unix timestamp of a date.
strftime() Format local time/date according to locale settings.
strptime() Parse the time/date generated by strftime().
strtotime() Parses any English text date or time description into a Unix timestamp.
time() Returns the Unix timestamp of the current time.
timezone_abbreviations_list() Returns an associative array containing daylight saving time, offset, and time zone name.
timezone_identifiers_list() Returns an indexed array with all time zone identifiers.
timezone_location_get() Returns the location information of the specified time zone.
timezone_name_from_abbr() Returns the time zone name based on the time zone abbreviation.
timezone_name_get() Returns the name of the time zone.
timezone_offset_get() Returns the time zone offset relative to GMT.
timezone_open() Create a new DateTimeZone object.
timezone_transitions_get() Returns all transitions for time zones.
timezone_version_get() Returns the version of the time zone database.


PHP 5 predefined Date/Time constants

##DATE_COOKIEHTTP Cookies ( For example: Sun, 14 Aug 2005 16:13:03 UTC)DATE_ISO8601ISO-8601 (For example: 2005-08-14T16:13:03 0000) DATE_RFC822RFC 822 (Example: Sun, 14 Aug 2005 16:13:03 UTC) DATE_RFC850RFC 850 (Example: Sunday, 14-Aug-05 16:13:03 UTC) DATE_RFC1036RFC 1036 (Example: Sunday, 14-Aug-05 16:13:03 UTC)DATE_RFC1123RFC 1123 (Example: Sun, 14 Aug 2005 16:13:03 UTC)DATE_RFC2822RFC 2822 (Sun, 14 Aug 2005 16:13:03 0000)DATE_RSSRSS (Sun, 14 Aug 2005 16:13:03 UTC) DATE_W3CWorld Wide Web Consortium (Example: 2005-08-14T16:13:03 0000)



The format string can recognize the string format characters of the following format parameters Description Return value example
Day --- ---
d The day of the month, with leading zeros 2 digits 01 to 31
D Day of the week, text representation, 3 letters Mon to Sun
j Day of the month, no leading zero 1 to 31
l (" L" lowercase letter) Day of the week, complete text format Sunday to Saturday
N Day of the week represented by numbers in ISO-8601 format (newly added in PHP 5.1.0) 1 (meaning Monday) to 7 ( Indicates Sunday)
S The English suffix after the day of the month, 2 characters st, nd, rd or th. Can be used with j
w The day of the week, the number indicates 0 (indicating Sunday) to 6 (indicating Saturday)
z The day of the year 0 to 365
week --- - --
W The week number in the year in ISO-8601 format, each week starts on Monday (newly added in PHP 4.1.0) For example: 42 (the 42nd week of the year)
month --- - --
F Month, complete text format, such as January or March January to December
m Month represented by numbers, with leading zeros 01 to 12
M Month represented by three-letter abbreviation Jan to Dec
n Number of months, no leading zero 1 to 12
t Number of days in a given month 28 to 31
Year --- ---
L Whether it is a leap year If it is a leap year, 1, otherwise 0
o Year number in ISO-8601 format. This is the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used. (Newly added in PHP 5.1.0) Examples: 1999 or 2003
Y 4-digit full year representation For example: 1999 or 2003
y 2-digit year representation For example: 99 or 03
time -- - ---
a Lowercase AM and PM values ​​am or pm
A Uppercase AM and PM values ​​AM or PM
B Swatch Internet Standard Time 000 to 999
g Hour, 12-hour format, No leading zeros 1 to 12
G Hours, 24-hour format, without leading zeros 0 to 23
h Hours, 12-hour format, with leading zeros 01 to 12
H Hours, 24-hour format, with leading zeros Zero 00 to 23
i Minutes with leading zeros 00 to 59>
s Seconds with leading zeros 00 to 59>
u Milliseconds (new in PHP 5.2.2). It should be noted that the date() function always returns 000000 because it only accepts integer parameters, and DateTime::format() only supports milliseconds. Example: 654321
Time zone --- ---
e Time zone identifier (newly added in PHP 5.1.0) For example: UTC, GMT, Atlantic/Azores
I Whether it is daylight saving time If it is daylight saving time, it is 1 , otherwise 0
O The number of hours difference from Greenwich Mean Time For example: 0200
P The difference from Greenwich Mean Time (GMT), with a colon separating hours and minutes (newly added in PHP 5.1.3 ) For example: 02:00
T The time zone where this machine is located For example: EST, MDT ([Translator's Note] It is a complete text format under Windows, such as "Eastern Standard Time", the Chinese version will display "China Standard Time" ).
Z The number of seconds of the time difference offset. Time zone offsets west of UTC are always negative, and time zone offsets east of UTC are always positive. -43200 to 43200
Complete date/time --- ---
c Date in ISO 8601 format (new in PHP 5) 2004-02-12T15:19:21 00:00
r     RFC 822 格式的日期     例如:Thu, 21 Dec 2000 16:01:07 +0200
U     从 Unix 纪元(January 1 1970 00:00:00 GMT)开始至今的秒数     参见 time()
-------------------------------------------------------------------------

preg_last_error

(PHP 5 >= 5.2.0, PHP 7)

preg_last_error — 返回最后一个PCRE正则执行产生的错误代码

说明

int preg_last_error ( void )

返回最后一次PCRE正则执行的错误代码。

Example #1 preg_last_error() 示例

<?php

preg_match(&#39;/(?:\D+|<\d+>)*[!?]/&#39;, &#39;foobar foobar foobar&#39;);

if (preg_last_error() == PREG_BACKTRACK_LIMIT_ERROR) {
    print &#39;Backtrack limit was exhausted!&#39;;
}

?>

以上例程会输出:

Backtrack limit was exhausted!

返回值

返回下面常量中的一个(查看它们自身的解释):

  • PREG_NO_ERROR

  • PREG_INTERNAL_ERROR

  • PREG_BACKTRACK_LIMIT_ERROR (参见 pcre.backtrack_limit)

  • PREG_RECURSION_LIMIT_ERROR (参见 pcre.recursion_limit)

  • PREG_BAD_UTF8_ERROR

  • PREG_BAD_UTF8_OFFSET_ERROR (自 PHP 5.3.0 起)

  • PREG_JIT_STACKLIMIT_ERROR (自 PHP 7.0.0 起)

----------------------------------------------------------------------------------------------------

array_diff() 函数返回两个数组的差集数组。该数组包括了所有在被比较的数组中,但是不在任何其他参数数组中的键值。

在返回的数组中,键名保持不变。

语法

array_diff(array1,array2,array3...);
Constant Description
DATE_ATOM Atom (Example: 2005-08-15T16:13:03 0000)
参数 描述
array1 必需。与其他数组进行比较的第一个数组。
array2 必需。与第一个数组进行比较的数组。
array3,... 可选。与第一个数组进行比较的其他数组。


提示和注释

提示:可用一个或任意多个数组与第一个数组进行比较。

注释:仅有值用于比较。

技术细节

返回值:

返回差集数组,该数组包括了所有在被比较的数组(array1)中,但是不在任何其他参数数组(array2 或 array3 等等)中的键值。

-------------------------------------------------- -----------------------------------------------

json_decode

(PHP 5 > = 5.2.0, PHP 7, PECL JSON > = 1.2.0)

json_decode — Decode a JSON string

Description

Mixed json_decode(String JSON $[, Boolean Related $=Error [, int is the depth = 512 [, int $option = 0] ])

Converts a JSON-encoded string to a PHP variable.

Parameters

  • JSON

  • ##This

    JSON string decoding.

    This feature only works with UTF-8 encoded strings.

    82e1f27b15ccd58568f7e2808f5fbb4cNote128dba7a3a77be0113eb0bea6ea0a5d0:

  • # Association

  • When
  • is true, the return target will be converted into an associative array S.

  • Depth

  • # User-specified recursion depth.
  • Options

  • JSON decoding options for Bitmask. There are currently two options. The first is
  • json_bigint_as_string This allows casting a string of big integers instead of floating, which is the default. The second option json_object_as_array has the same effect as the backgroundassociation to really.

  • Return Value

The returned value is encoded in

JSON

in the appropriate PHP type. Values ​​ true , false and invalid returns true , false and are invalid respectively. Invalid Returned if JSON cannot be decoded or the encoded data is deeper than the recursion limit.

 

---------------------------------------------------------


addslashes() 函数返回在预定义字符之前添加反斜杠的字符串。

预定义字符是:

  • 单引号(')

  • 双引号(")

  • 反斜杠(\)

  • NULL

提示:该函数可用于为存储在数据库中的字符串以及数据库查询语句准备字符串。

注释:默认地,PHP 对所有的 GET、POST 和 COOKIE 数据自动运行 addslashes()。所以您不应对已转义过的字符串使用 addslashes(),因为这样会导致双层转义。遇到这种情况时可以使用函数 get_magic_quotes_gpc() 进行检测。


语法

addslashes(string)
参数描述string必需。规定要转义的字符串。


技术细节

返回值:返回已转义的字符串。  ------------------------------------------------------------------------------------stripslashes() 函数删除由 addslashes() 函数添加的反斜杠。提示:该函数可用于清理从数据库中或者从 HTML 表单中取回的数据。语法stripslashes(string)参数描述string必需。规定要检查的字符串。技术细节返回值:返回已剥离反斜杠的字符串。 

Related recommendations:

PHP common function encapsulation

The above is the detailed content of PHP common function collection. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP standard libraryNext article:PHP standard library