PHP Intermediate

Author: Joyce_BY, all rights reserved.
Contact by email: [email protected]


多维数组

一个数组中的值可以是另一个数组,另一个数组的值也可以是一个数组。

<?php
// 二维数组:
$cars = array
(
    array("Volvo",100,96),
    array("BMW",60,59),
    array("Toyota",110,100)
);
echo $cars[0][0] // Volvo
?>
// 3-dimension
<?php 
$sites = array 
( 
    "baidu"=>array 
    ( 
        "baidu search", 
        "http://www.baidu.com" 
    ), 
    "google"=>array 
    ( 
        "Google search", 
        "http://www.google.com" 
    )
); 
echo $sites['google'][0] . '地址为:' . $sites['google'][1];
?>

date() 函数

把时间戳格式化为可读性更好的日期和时间

string date ( string $format [, int $timestamp ] )
// format [必需]: 规定时间戳的格式。
// timestamp[可选]: 规定时间戳, 默认是当前的日期和时间。
  • d - dd
  • m - mm
  • Y - yyyy
    please google for more formats
    一些格式例子如下:
<?php
echo date("Y/m/d") . "<br>";
echo date("Y.m.d") . "<br>";
echo date("Y-m-d");
?>

include and require

include 和 require 除了处理错误的方式不同之外,在其他方面都是相同的:

  • require 生成一个致命错误(E_COMPILE_ERROR),在错误发生后脚本会停止执行;
  • include 生成一个警告(E_WARNING),在错误发生后脚本会继续执行。
<?php
include 'file';
require 'file';
?>

File

functions
fopen(string $filename, string $mode) - open a file
fclose(string $filename) - close a file
feof(string $filename) - test whether we reach the end of file

ps: In w, a and x mode, we cannot read a open file.

fgets(string $filename) - read file by line
fgetc(string $filename) - read file by character
fgetcsv(string $filename) - read CSV file, transform text to array value and store in $arr

CSV file is a kind a text file, in which data is separated by commas.

upload
通过使用 PHP 的全局数组 $_FILES,你可以从客户计算机向远程服务器上传文件;
第一个参数是表单的 input name,第二个下标可以是 “name”、“type”、“size”、“tmp_name” 或 “error”.

上传限制
end(string $filename) - 获取文件后缀名
move_uploaded_file (string $_FILES[‘file’][‘tmp_name’], string $category. $_FILES[“file”][“name”]);

cookie
setcookie(name, value, expire, path, domain);

setcookie() 函数必须位于 标签之前
在发送 cookie 时,cookie 的值会自动进行 URL 编码;;在取回时进行自动解码。(为防止 URL 编码,请使用 setrawcookie() 取而代之。

$_COOKIE - 取回 cookie 的值
isset($COOKIE[string $name]) - 判断是否已设置了 cookie
删除 cookie - use setcookie()设置过期日期为过去的时间点

Session

会话信息是临时的,当用户访问一个网站时会话变量在服务器上存储用户信息;
Session 的工作机制:为每个访客创建一个唯一的 id (UID),并基于这个 UID 来存储变量。UID 存储在 cookie 中,或者通过 URL 进行传导。

session_start() - start a session, put it before <html>
_SESSION - 存储,取回会话变量
unset($_SESSION[string $var]) OR session_destroy() - 删除session数据

E-mail

mail(to,subject,message,headers,parameters) - send emails through php scripts

to[必需]: email 接收者。
subject[必需]: email 的主题。不能包含任何新行字符。
message[必需]: 要发送的消息。使用 LF (\n) 来分隔各行,每行限制在 70 个字符内。
headers[可选]: 附加的标题,比如 From、Cc 和 Bcc。使用 CRLF (\r\n) 分隔附加的标题。
parameters[可选]: 对邮件发送程序规定额外的参数。

E-mail注入
未经授权的用户可通过输入表单在邮件头部插入数据。
假如用户在表单中的输入框内输入以下文本到电子邮件中

[email protected]%0ACc:[email protected]
%0ABcc:[email protected],[email protected],
[email protected],[email protected]
%0ABTo:[email protected]

mail() 函数会把上面的文本放入邮件头部,那么现在头部有了额外的 Cc:、Bcc: 和 To: 字段,当用户点击提交按钮时,这封 e-mail 会被发送到上面所有的地址。

为了防止email注入,我们对输入进行验证。
filter_var() - 过滤 e-mail

filter_var($field, FILTER_SANITIZE_EMAIL); // 过滤器从字符串中删除电子邮件的非法字符
filter_var($field, FILTER_VALIDATE_EMAIL); // 过滤器验证电子邮件地址的值

Error Handling

  1. 简单地终止脚本
    die(string $error_message)
  2. 用户自定义
    自定义报错函数等
    error_function(error_level,error_message,error_file,error_line,error_context)

error_level[必需]: 错误报告级别,必须是一个数字。
error_message[必需]: 错误消息。
error_file[可选]: 错误发生的文件名。
error_line[可选]: 错误发生的行号。
error_context [可选]: 规定一个数组,包含了当错误发生时在用的每个变量以及它们的值。

错误报告级别
错误报告级别 (user defined)

value constant describe
2 E_WARNING 非致命的 run-time 错误。不暂停脚本执行。
8 E_NOTICE run-time 通知。在脚本发现可能有错误时发生,但也可能在脚本正常运行时发生。
256 E_USER_ERROR 致命的用户生成的错误。这类似于程序员使用 PHP 函数 trigger_error() 设置的 E_ERROR。
512 E_USER_WARNING 非致命的用户生成的警告。这类似于程序员使用 PHP 函数 trigger_error() 设置的 E_WARNING。
1024 E_USER_NOTICE 用户生成的通知。这类似于程序员使用 PHP 函数 trigger_error() 设置的 E_NOTICE。
4096 E_RECOVERABLE_ERROR 可捕获的致命错误。类似 E_ERROR,但可被用户定义的处理程序捕获 (reference set_error_handler())
8191 E_ALL 所有错误和警告 (in PHP 5.4, E_STRICT 成为 E_ALL 的一部分)

Error record

在默认的情况下,根据在 php.ini 中的 error_log 配置,PHP 向服务器的记录系统或文件发送错误记录。通过使用 error_log() 函数,您可以向指定的文件或远程目的地发送错误记录。
bool error_log ( string message [, int message_type [, string destination [, string extra_headers]]] )

Exception Handling

异常处理: 在指定的异常情况发生时改变脚本的正常流程。
When exception happens:

  1. save current state
  2. jump to exception handler defined previously or by yourself
  3. the processor may restart where exception happens, or terminate, or start at somewhere else of the process

When throw an exception, the following code will not be executed, and PHP will search for the catch block, if the exception is not catched and there is no set_exception_handler(), it will rise an “Uncaught Exception” vital Error.

Try、throw and catch
Try - put code that may throw exceptions into try block.
Throw - define how to trigger the exception, every throw must at least have a catch block.
Catch - catch the exception and create an object containing exception information.
Class Exception - built-in exception class, contain getMessage() function.
Self-defined exception class - must extends Exception.

顶层异常处理器
set_exception_handler() - 设置处理所有未捕获异常的用户定义函数

Filter

PHP 过滤器用于验证和过滤来自非安全来源的数据.
如需过滤变量,请使用下面的过滤器函数之一:

  • filter_var() - 通过一个指定的过滤器来过滤单一的变量
  • filter_var_array() - 通过相同的或不同的过滤器来过滤多个变量
  • filter_input - 获取一个输入变量,并对它进行过滤
  • filter_input_array - 获取多个输入变量,并通过相同的或不同的过滤器对它们进行过滤

2 filters

validating sanitizing
用于验证用户输入 用于允许或禁止字符串中指定的字符
严格的格式规则(比如 URL 或 E-Mail 验证) 无数据格式规则
如果成功则返回预期的类型,如果失败则返回 FALSE 始终返回字符串

选项和标志
选项和标志用于向指定的过滤器添加额外的过滤选项。
选项必须放入一个名为 “options” 的相关数组中;如果使用标志,则不需在数组内。

Filter Callback
通过使用 FILTER_CALLBACK 过滤器,可以调用自定义的函数,把它作为一个过滤器来使用,它可以用自定义函数,也可以用built-in数。

Please check the filter handbook for more information.

PHP and JSON

json_encode()

string json_encode ( $value [, $options = 0 ] )

json_decode()

mixed json_decode ( j s o n s t r i n g [ , json_string [, assoc = false [, $depth = 512 [, $options = 0 ]]])

猜你喜欢

转载自blog.csdn.net/qq_35649764/article/details/82962029