PHP Common function Modules

Source: Internet
Author: User

Error Exception module error handling

1. The system defines a number of binary codes used to indicate the level of error Reporting:

? ? Modify the PHP configuration file in/etc/php5/apache2/php.ini, where display_errors default is off, set to on to open the PHP output error reporting function, it will be more convenient to develop, but it is best to switch off at the time of release, Because there will be too much disclosure of website information;

? ? By modifying the value of the error_reporting, use the bitwise operator:& | ~, combine multiple error levels and assign values to error_reporting to the default level;

? ? If you do not modify the default configuration file, you can also use the Ini_set (), error_reporting () These two functions to do the corresponding operation: Ini_set (' display_errors ', 1); Error_reporting (E_all & ~e_notice); such as

2. Custom error Handling:

? ? In addition to system error reporting, you can mask errors in the program through error_reporting (0), and you can specify your own defined error-handling functions through the Set_error_handler () function, and even give a beautifully designed error page.

3. Using the error log

? ? Enable the error logging feature by modifying the following configuration and restarting the Web server:

? ? error_reporting = E_all;? or other levels
? ? Display_errors = OFF; Turn off Error Reporting
? ? Log_errors = on; open error log
? ? Log_error_max_len = 1024; Set maximum length of log entries
? ? Error_log =/your_path_to_log/error.log; Specify the full path of the log

? ? You can also use the built-in Error_log () function, which allows you to customize the error message to the log, using the relevant parameters of the function:

? ? The first is a required parameter: message;
? ? The second one is Message_type, which is an integer value,
? ? ? ? 0 is sent to the operating system log, at this time do not need to pass another parameter;
? ? ? ? 1 means to use the mail () function of PHP, send information to a mailbox, at this time need to specify a parameter, specify the mailbox sent to;
? ? ? ? 2 indicates that the error message is sent to the TCP server and the address and port number of the server need to be specified;
? ? ? ? 3 means to save the error message to a file, you need to specify the file name to save to;

4. Other error log functions: Define_syslog_variables (), Openlog (), syslog (), Closelog (), the first function must be called before the next three functions are called;

Exception handling

1. PHP Exception handling Implementation function:

? ? try{
? ? ? ? ... do something arouse a exception
? ? ? ? Throw $exception _obj;
? ? ? ? ...
? ? ? ? ... would never do if excepted
? ? } catch (Ex_class $exception _obj) {
? ? ? ? Does something when excepted?
? ? } catch (Ex_class2 $exception _obj) {
? ? ? ? Do something when another exception aroused
? ? }

? ? As you can see, PHP can capture multiple exceptions at the same time and make different processing depending on the type of exception

2. PHP has built-in exception class: Exception, by inheriting the class can customize its own exception class, where the class can inherit the methods and properties are:

? date and Time

1. UNIX Timestamp: Unix system save January 1, 1970 0 O'Clock (UTC/GMT) (this date is often referred to as the Unix ERA) begins the number of seconds that are experienced by the current time, expressed as a 32-bit integer, So that before the 1902 and after 2038 time will encounter some problems, if under Windows, the timestamp can not be negative, if the time before the record 1970 must be handled accordingly;

2. Using the Mktime () function, the Unix timestamp can be obtained by passing in hour, minute, second, month, day and year in turn;

? ? Using the Strtotime () function, you can convert the time expressed in the English natural language to a timestamp:

? ? Strtotime (' Now ');
? ? Strtotime (' 8 May 2012 ');
? ? Strtotime (' +1 Day ');
? ? Strtotime (' last Monday ');

? ? And so on

3. Use the UNIX timestamp to calculate the time, only need to subtract two time stamps;

4. Through the GETDATE () function, pass in a timestamp and return an array with the time/date of the record, the key name of the array is: seconds, minutes, hours, Mday, Wday, Mon, year, Yday, weekday, month, 0; where Mday represents the day of the month, Wday represents the day of the week, Yday represents the day of the year, weekday that the in days of the full English text, 0 for the timestamp;

5. Formatted output: the date () function, which passes two parameters, the first format to be formatted, the second is a timestamp, such as date (' Y year m D Day H:i:s ', Strtotime (' Now ')); The verbose format code is:

If you want to format dates for other languages, you should use setlocal () localization, and strftime () instead of date ().

6. Modify the default time zone: In the configuration file to modify the Date.timezone, Beijing time can be set to any of the following: Asia/shanghai, asia/chongqing, etc/gmt-8, PRC;

? ? If you do not modify the configuration file, you can use the Date_default_timezone_set () function in your program to set the time zone;

7. In the timing application, the seconds is the smallest unit is not accurate enough, you can use the Microtime () function, the function passed a Boolean value, the default is False, by default, the output "msec sec", where msec is the number of microseconds, the SEC is the timestamp, if set to true, The output is a floating-point type, accurate to microseconds;

PHP Common function Modules

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.