php手册总结(一)

一:自动加载

__autoload():

注意:

1:spl_autoload_register() 提供了一种更加灵活的方式来实现类的自动加载。因此,不再建议使用 __autoload() 函数,在以后的版本中它可能被弃用。

2:在 5.3.0 版之前,__autoload 函数抛出的异常不能被 catch语句块捕获并会导致一个致命错误。从 5.3.0+ 之后,__autoload 函数抛出的异常可以被 catch语句块捕获,但需要遵循一个条件。如果抛出的是一个自定义异常,那么必须存在相应的自定义异常类。__autoload 函数可以递归的自动加载自定义异常类。

3:自动加载不可用于 PHP 的 CLI 交互模式

 

二:文件系统函数

1:string basename ( string $path [, string $suffix ] )  记住$suffix参数

basename() operates naively on the input string, and is not aware of the actual filesystem, or path components such as "..".

Note:

basename() is locale aware, so for it to see the correct basename with multibyte character paths, the matching locale must be set using the setlocale() function.

<?php
echo "1) ".basename("/etc/sudoers.d"".d").PHP_EOL;
echo "2) ".basename("/etc/passwd").PHP_EOL;
echo "3) ".basename("/etc/").PHP_EOL;   //basename也可以作用于没有文件名的路径,这样的话则最后的路径段被返回
echo "4) ".basename(".").PHP_EOL;
echo "5) ".basename("/");
?>
The above example will output:
1) sudoers
2) passwd
3) etc
4) .
5) 

三:变量名中的点

通常,PHP 不会改变传递给脚本中的变量名。然而应该注意到点(句号)不是 PHP 变量名中的合法字符。至于原因,看看:

<?php
$varname.ext;  /* 非法变量名 */
?>
这时,解析器看到是一个名为 $varname 的变量,后面跟着一个字符串连接运算符,后面跟着一个裸字符串(即没有加引号的字符串,且不匹配任何已知的健名或保留字)'ext'。很明显这不是想要的结果。

出于此原因,要注意 PHP 将会自动将变量名中的点替换成下划线。即获取的时候用: $_POST['varname_ext'];

 

四:string http_build_query ( mixed $query_data [, string $numeric_prefix [, string $arg_separator [, int $enc_type = PHP_QUERY_RFC1738 ]]] )

生成 URL-encode 之后的请求字符串

注意:

1:结果是生按url-encode编码后的字符

2: php版本5.4后加入了$enc_type,

enc_type

默认使用 PHP_QUERY_RFC1738

如果 enc_typePHP_QUERY_RFC1738,则编码将会以 » RFC 1738 标准和 application/x-www-form-urlencoded 媒体类型进行编码,空格会被编码成加号(+)。

如果 enc_typePHP_QUERY_RFC3986,将根据 » RFC 3986 编码,空格会被百分号编码(%20)。

 

类似:urlencoding按RFC 1738来编码

而rawurlencoding按» RFC 3986来编码

        $name = "wang le le";
        echo urlencode($name) . '<br>';
        echo rawurlencode($name);

输出:

wang+le+le
wang%20le%20le

3:Params with null value do not present in result string.

<?
$arr = array('test' => null, 'test2' => 1);
echo http_build_query($arr);
?>
will produce:
test2=1

 

4:关于对空格的编码问题

例1:

When using the http_build_query function to create a URL query from an array for use in something like curl_setopt($ch, CURLOPT_POSTFIELDS, $post_url), be careful about the url encoding.

In my case, I simply wanted to pass on the received $_POST data to a CURL's POST data, which requires it to be in the URL format.  If something like a space [ ] goes into the http_build_query, it comes out as a +. If you're then sending this off for POST again, you won't get the expected result.  This is good for GET but not POST.

Instead you can make your own simple function if you simply want to pass along the data:

<?php
$post_url = '';
foreach ($_POST AS $key=>$value)
    $post_url .= $key.'='.$value.'&';
$post_url = rtrim($post_url, '&');
?>

You can then use this to pass along POST data in CURL.

<?php
    $ch = curl_init($some_url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_url);
    curl_exec($ch);
?>

Note that at the final page that processes the POST data, you should be properly filtering/escaping it

 

例2:

instead of some other suggestions that did not work for me, I found that the best way to build POST content (e.g. for stream_context_create) is urldecode(http_build_query($query))

 5:void set_time_limit ( int $seconds )

注意:

1》:当php运行于安全模式时,此功能不能生效。除了关闭安全模式或改变php.ini中的时间限制,没有别的办法。

2》set_time_limit()函数和配置指令max_execution_time只影响脚本本身执行的时间。任何发生在诸如使用system()的系统调用,流操作,数据库操作等的脚本执行的最大时间不包括其中,当该脚本已运行。在测量时间是实值的Windows中,情况就不是如此了。

posted @ 2014-07-03 15:28  一束光  阅读(389)  评论(0编辑  收藏  举报

友情链接

CFC4N