如需过滤变量,请使用下面的过滤器函数之一:
- filter_var() - 通过一个指定的过滤器来过滤单一的变量
- filter_var_array() - 通过相同的或不同的过滤器来过滤多个变量
- filter_input - 获取一个输入变量,并对它进行过滤
- filter_input_array - 获取多个输入变量,并通过相同的或不同的过滤器对它们进行过滤
Validating 和Sanitizing
有两种过滤器:
Validating 过滤器:
- 用于验证用户输入
- 严格的格式规则(比如 URL 或 E-Mail 验证)
- 如果成功则返回预期的类型,如果失败则返回 FALSE
Sanitizing 过滤器:
- 用于允许或禁止字符串中指定的字符Are used to allow or disallow specified characters in a string
- 无数据格式规则
- 始终返回字符串
错误处理
- 简单的 "die()" 语句
- 自定义错误和错误触发器
- 错误报告
为了避免用户得到类似上面的错误消息,我们在访问文件之前检测该文件是否存在:
<?php
if(!file_exists("welcome.txt"))
{
die("File not found");
}
else
{
$file=fopen("welcome.txt","r");
}
?>
if(!file_exists("welcome.txt"))
{
die("File not found");
}
else
{
$file=fopen("welcome.txt","r");
}
?>