PHP基础知识总结

来源:互联网 发布:淘宝网消保规则 编辑:程序博客网 时间:2024/05/16 08:19

1.bool empty(mixed var): 检查一个变量是否为空

如果 var 为空(""0"0"NULLFALSEarray()var $var; 以及没有任何属性的对象都将被认为是空的),则返回 TRUE;如果 var 是非空或非零的值,返回 FALSE.

 

2.bool isset( mixed var [, mixed var [, ...]] ):检测变量是否设置

如果 var 存在则返回 TRUE,否则返回 FALSE。若使用 isset() 测试一个被设置成 NULL 的变量,将返回 FALSE。同时要注意的是一个 NULL 字节("/0")并不等同于 PHP 的 NULL 常数。

 

3.bool defined ( string name ):检测常量是否已设置

 

4.流程控制 

 

(1)if: 只在条件表达式值为 TRUE 时执行语句

 

(2)elseif: 仅在之前的 ifelseif 的表达式值为 FALSE,而当前的 elseif 表达式值为 TRUE 时执行。

 

(3)else: 仅在 if 以及 elseif(如果有的话)语句中的表达式的值为 FALSE 时执行

 

(4)while: 只要 while 表达式的值为 TRUE 就重复执行嵌套中的循环语句.如果 while 表达式的值一开始就是 FALSE,则循环语句一次都不会执行。

 

(5)do-while:和正规的 while 循环主要的区别是: do-while 的循环语句保证会执行一次(表达式的真值在每次循环结束后检查)

 

5.时间日期函数库

 

(1)bool checkdate ( int month, int day, int year)

 

如果给出的日期有效则返回 TRUE,否则返回 FALSE

  • year 的值是从 1 到 32767

  • month 的值是从 1 到 12

  • Day 的值在给定的 month 所应该具有的天数范围之内,闰年已经考虑进去了。

  •  

     eg:if( checkdate(2,22,2009) ){...} 

     

    (2)string date ( string format [, int timestamp])

     

    返回将整数 timestamp 按照给定的格式字串而产生的字符串

    timestamp 是可选的,默认值为 time()

     

    (3)int strtotime ( string time [, int now])

    本函数预期接受一个包含英文日期格式的字符串并尝试将其解析为 UNIX 时间戳。如果 time 的格式是绝对时间则 now 参数不起作用。如果 time 的格式是相对时间则其所相对的时间由 now 提供,或者如果未提供 now 参数时用当前时间。失败时返回 -1

    eg:echo strtotime ("now"), "/n";//<=>time()
         echo strtotime ("10 September 2009"), "/n";//1252540800
         echo strtotime ("+1 day"), "/n";
         echo strtotime ("+1 week"), "/n";
         echo strtotime ("+1 week 2 days 4 hours 2 seconds"), "/n";
         echo strtotime ("next Thursday"), "/n";
         echo strtotime ("last Monday"), "/n";

     

    (4)array getdate ( [int timestamp])

    返回一个根据 timestamp 得出的包含有日期信息的结合数组。

    返回的关联数组中的键名单元 键名说明返回值例子"seconds"秒的数字表示059"minutes"分钟的数字表示059"hours"小时的数字表示023"mday"月份中第几天的数字表示131"wday"星期中第几天的数字表示0(表示星期天)到 6(表示星期六)"mon"月份的数字表示112"year"4 位数字表示的完整年份例如:19992003"yday"一年中第几天的数字表示0366"weekday"星期几的完整文本表示SundaySaturday"month"月份的完整文本表示January>December0自从 Unix 纪元开始至今的秒数,和 time() 的返回值以及用于 date() 的值类似。 系统相关,典型值为从 -21474836482147483647

     

    (5)string gmdate ( string format [, int timestamp])

    date() 函数完全一样,只除了返回的时间是格林威治标准时(GMT)。例如当在中国(GMT +0800)运行以下程序时,第一行显示“Jan 01 2000 00:00:00”而第二行显示“Dec 31 1999 16:00:00”。

     

    注: 在 Microsoft Windows 系列的操作系统中实现本函数的系统库是坏的,因此 gmdate() 不支持负的 timestamp 参数值。细节见错误报告:#22620,#22457 和 #14391。

    此问题不会出现于 Unix/Linux 操作系统,该系统下的系统库会按照预期结果运作。

     

    (6)int mktime ( [int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]])

     

    根据给出的参数返回 Unix 时间戳。时间戳是一个长整数,包含了从 Unix 新纪元(1970 年 1 月 1 日)到给定时间的秒数。

     

    (7)string strftime ( string format [, int timestamp])

     

    返回用给定的格式字串对给出的 timestamp 进行格式输出后的字符串。

     

    eg:strftime ("%Y-%m-%d",time());

     

     

    (8)int time ( void )

    返回当前的 UNIX 时间戳

     

    (9)string gmstrftime ( string format [, int timestamp])

     

    strftime() 的行为相同,只除了返回时间是格林威治标准时(GMT)。

     

    (10)int gmmktime ( [int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]])

    mktime() 完全一样,只除了返回值是格林威治标准时的时间戳。

     

    6.int error_reporting(int [level]);

     

    确定PHP错误报告敏感度的级别,一共有十三个预定的错误级别,每一个都唯一对应于应用程序或服务器功能。

     

    如果参数 level 未指定,当前报错级别将被返回。下面几项是 level 可能的值:

    值常量描述1E_ERRORFatal run-time errors. Errors that can not be recovered from. Execution of the script is halted2E_WARNINGNon-fatal run-time errors. Execution of the script is not halted4E_PARSECompile-time parse errors. Parse errors should only be generated by the parser8E_NOTICERun-time notices. The script found something that might be an error, but could also happen when running a script normally16E_CORE_ERRORFatal errors at PHP startup. This is like an E_ERROR in the PHP core32E_CORE_WARNINGNon-fatal errors at PHP startup. This is like an E_WARNING in the PHP core64E_COMPILE_ERRORFatal compile-time errors. This is like an E_ERROR generated by the Zend Scripting Engine128E_COMPILE_WARNINGNon-fatal compile-time errors. This is like an E_WARNING generated by the Zend Scripting Engine256E_USER_ERRORFatal user-generated error. This is like an E_ERROR set by the programmer using the PHP function trigger_error()512E_USER_WARNINGNon-fatal user-generated warning. This is like an E_WARNING set by the programmer using the PHP function trigger_error()1024E_USER_NOTICEUser-generated notice. This is like an E_NOTICE set by the programmer using the PHP function trigger_error()2048E_STRICTRun-time notices. PHP suggest changes to your code to help interoperability and compatibility of the code4096E_RECOVERABLE_ERRORCatchable fatal error. This is like an E_ERROR but can be caught by a user defined handle (see also set_error_handler())8191E_ALLAll errors and warnings, except level E_STRICT (E_STRICT will be part of E_ALL as of PHP 6.0)

    • E_NOTICE 表示一般情形不记录,只有程序有错误情形时才用到,例如企图存取一个不存在的变量,或是呼叫 stat() 函数检视不存在的文件。
    • E_WARNING 通常都会显示出来,但不会中断程序的执行。这对除错很有效。例如:用有问题的正则表达式呼叫 ereg()。
    • E_ERROR 通常会显示出来,亦会中断程序执行。意即用这个遮罩无法追查到内存配置或其它的错误。
    • E_PARSE 从语法中解析错误。
    • E_CORE_ERROR 类似 E_ERROR,但不包括 PHP 核心造成的错误。
    • E_CORE_WARNING 类似 E_WARNING,但不包括 PHP 核心错误警告。

    eg:<?php
    //禁用错误报告
    error_reporting(0);
     
    //报告运行时错误
    error_reporting(E_ERROR | E_WARNING | E_PARSE);
     
    //报告所有错误
    error_reporting(E_ALL);
    ?>

    原创粉丝点击