thinkphp函数学习(1)——header, get_magic_quotes_gpc, array_map, stripslashes, stripslashes_deep

1. header

相关语句

header('Content-type: text/html; charset=utf-8'); // 因为这是在TP的入口文件中,所以每个页面返回的http header都会多一行Content-type: text/html; charset=utf-8

header函数原型

header(string,replace,http_response_code) // 作用是在页面的返回header中插入信息(一个键值对,第一个参数定义),第二个是一个布尔值,默认true,表示如果插入信息的键跟前面的有重复,那么就覆盖前面的键和值。第三个参数是定义返回码(200,301,404 。。。),如果第二个参数时true的话,返回码也是会覆盖前面已经定义的返回码的 

2. get_magic_quotes_gpc

相关语句

if (get_magic_quotes_gpc()) {...}

php的配置文件php.ini中有一项配置为magic_quotes_gpc = On/Off,如果是On,那么get_magic_quotes_gpc()返回1,否则,返回0  

3. array_map

相关语句

 $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);

函数原型

array_map(function,array1,array2,array3...)

array_map有点像是一个过滤器,把value中的每一个元素丢进第一个参数指定的函数中处理一下,再放回数组中原来的位置。

4. stripslashes

函数原型

stripslashes(string)

作用是把addslashes函数添加的反斜杠去掉,其实只要是反斜杠都会去掉(除了\两个斜杠一起的时候)

5. stripslashes_deep(tp自定义函数)

function stripslashes_deep($value)
{
        $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
        return $value;
}

如果是数组,那么去掉里面每一项的反斜杠,如果是字符串直接去掉反斜杠。从这个函数中得到的启示是:这个函数也可以遍历数组!不需要for, foreach!!缺点就是可读性不强

原文地址:https://www.cnblogs.com/bushe/p/4226985.html