phpマニュアルまとめ(一)

10215 ワード

一:自動ロード
__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規格およびアプリケーション/x−www−form−urlencodedメディアタイプで符号化され、スペースはプラス記号(+)に符号化される.enc_typePHP_QUERY_RFC3986である場合、» RFC 3986に従って符号化され、スペースはパーセンテージ符号化される(%20).
 
類似:urlencodingはRFC 1738で符号化されます.
rawurlencodingは» RFC 3986で符号化されます
        $name = "wang le le";        echo urlencode($name) . '
';        echo rawurlencode($name);
出力:
wang+le+lewang%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では、そうではありません.