/xml/language/variables.xml
XML | 948 lines | 801 code | 123 blank | 24 comment | 0 complexity | 6823aed953c11d52c8d6eb856d71dff8 MD5 | raw file
- <?xml version="1.0" encoding="utf-8"?>
- <!-- $Revision: 324974 $ -->
- <!-- EN-Revision: 324522 Maintainer: HonestQiao Status: ready -->
- <!-- Reviewed: no Maintainer: HonestQiao -->
- <!-- CREDITS: dallas, Gregory, verdana -->
- <chapter xml:id="language.variables" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
- <title>??</title>
-
- <sect1 xml:id="language.variables.basics">
- <title>??</title>
-
- <simpara>
- PHP ????????????????????????????????
- </simpara>
-
- <para>
- ???? PHP
- ??????????????????????????????????????????????????????????????????????????'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'?
- </para>
-
- <note>
- <simpara>
- ???????? a-z?A-Z??? ASCII ??? 127 ? 255?0x7f-0xff??
- </simpara>
- </note>
- <note>
- <simpara>
- <literal>$this</literal> ????????????????
- </simpara>
- </note>
-
- &tip.userlandnaming;
-
- <para>
- ??????????<link linkend="ref.var">????</link>?
- </para>
- <para>
- <informalexample>
- <programlisting role="php">
- <![CDATA[
- <?php
- $var = 'Bob';
- $Var = 'Joe';
- echo "$var, $Var"; // ?? "Bob, Joe"
-
- $4site = 'not yet'; // ???????????
- $_4site = 'not yet'; // ????????????
- $i??is = 'mansikka'; // ???????????
- ?>
- ]]>
- </programlisting>
- </informalexample>
- </para>
-
- <para>
- ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????<link linkend="language.expressions">???</link>???
- </para>
- <para>
- PHP ????????????????<link linkend="language.references">????</link>???????????????????“?????” ?? “??”???????????????????????????
- </para>
- <para>
- ????????????? &
- ??????????????????????????????“My name is Bob”???
- <informalexample>
- <programlisting role="php">
- <![CDATA[
- <?php
- $foo = 'Bob'; // ? 'Bob' ?? $foo
- $bar = &$foo; // ?? $bar ?? $foo
- $bar = "My name is $bar"; // ?? $bar ??
- echo $bar;
- echo $foo; // $foo ??????
- ?>
- ]]>
- </programlisting>
- </informalexample>
- </para>
-
- <para>
- ???????????????????????????????
- <informalexample>
- <programlisting role="php">
- <![CDATA[
- <?php
- $foo = 25;
- $bar = &$foo; // ?????
- $bar = &(24 * 7); // ??; ??????????
-
- function test()
- {
- return 25;
- }
-
- $bar = &test(); // ??
- ?>
- ]]>
- </programlisting>
- </informalexample>
- </para>
- <para>
- ??? PHP ?????????????????????????????????????????? - ???????????
- &false;????????????????????????????????????????????
- </para>
- <para>
- <example>
- <title>??????????</title>
- <programlisting role="php">
- <![CDATA[
- <?php
- // Unset AND unreferenced (no use context) variable; outputs NULL
- var_dump($unset_var);
-
- // Boolean usage; outputs 'false' (See ternary operators for more on this syntax)
- echo($unset_bool ? "true\n" : "false\n");
-
- // String usage; outputs 'string(3) "abc"'
- $unset_str .= 'abc';
- var_dump($unset_str);
-
- // Integer usage; outputs 'int(25)'
- $unset_int += 25; // 0 + 25 => 25
- var_dump($unset_int);
-
- // Float/double usage; outputs 'float(1.25)'
- $unset_float += 1.25;
- var_dump($unset_float);
-
- // Array usage; outputs array(1) { [3]=> string(3) "def" }
- $unset_arr[3] = "def"; // array() + array(3 => "def") => array(3 => "def")
- var_dump($unset_arr);
-
- // Object usage; creates new stdClass object (see http://www.php.net/manual/en/reserved.classes.php)
- // Outputs: object(stdClass)#1 (1) { ["foo"]=> string(3) "bar" }
- $unset_obj->foo = 'bar';
- var_dump($unset_obj);
- ?>
- ]]>
- </programlisting>
- </example>
- </para>
- <para>
- ???????????????????????????????????????????????????
- <link linkend="ini.register-globals">register_globals</link> ????????<link
- linkend="security.globals">????</link>?????????????<link
- linkend="errorfunc.constants.errorlevels.e-notice">E_NOTICE</link>????????????????????????<function>isset</function>
- ??????????????????????
- </para>
- </sect1>
-
- <sect1 xml:id="language.variables.predefined">
- <title>?????</title>
-
- <simpara>
- PHP ?????????????????????????????????????????????????????????????
- PHP ?<link linkend="features.commandline">???</link>?????????????????????????<link
- linkend="reserved.variables">?????</link>???
- </simpara>
-
- <warning>
- <simpara>
- PHP 4.2.0 ????????PHP ?? <link
- linkend="ini.register-globals">register_globals</link> ?????
- <emphasis>off</emphasis>??? PHP ????????? register_globals ???
- <emphasis>off</emphasis> ????????????????????????????
- <varname>DOCUMENT_ROOT</varname> ????????
- <varname>$_SERVER['DOCUMENT_ROOT']</varname> ??
- <varname>$DOCUMENT_ROOT</varname>??????
- <varname>$_GET['id']</varname> ??? <varname>$id</varname> ? URL
- <literal>http://www.example.com/test.php?id=3</literal> ???
- id ?????? <varname>$_ENV['HOME']</varname> ???
- <varname>$HOME</varname> ?????? HOME ???
- </simpara>
- <simpara>
- ?????????? <link
- linkend="ini.register-globals">register_globals</link>
- ?????????????<link
- linkend="security.globals">?? Register Globals</link>??? PHP
- <link xlink:href="&url.php.release4.1.0;">4.1.0</link> ?
- <link xlink:href="&url.php.release4.2.0;">4.2.0</link> ??????
- </simpara>
- <simpara>
- ?????? PHP ???????????<link
- linkend="language.variables.superglobals">?????</link>?
- </simpara>
- </warning>
-
- <simpara>
- ? PHP 4.1.0 ???PHP ???????????????????????? web
- ??????????????????????????????????????????????????????????????????????????autoglobals?????????superglobals???PHP
- ????????????????????????????????????????????
- PHP ???????????????????????<link
- linkend="reserved.variables">?????</link>??????????????????<varname>$HTTP_*_VARS</varname>??????&avail.register-long-arrays;
- </simpara>
-
- <note>
- <title>????</title>
- <para>
- ???????????<link linkend="language.variables.variable">????</link>?
- </para>
- </note>
- <note>
- <para>
- ???????? HTTP_*_VARS
- ??????????????????????????????????????
- </para>
- </note>
-
- <para>
- ???? <link linkend="ini.variables-order">variables_order</link>
- ??????????????? PHP ??????????
- </para>
-
- </sect1>
-
-
- <sect1 xml:id="language.variables.scope">
- <title>????</title>
-
- <simpara>
- ???????????????????????????????
- PHP ??????????????????????????
- include ? require ?????????
- </simpara>
- <informalexample>
- <programlisting role="php">
- <![CDATA[
- <?php
- $a = 1;
- include 'b.inc';
- ?>
- ]]>
- </programlisting>
- </informalexample>
- <simpara>
- ???? <varname>$a</varname> ???????
- <filename>b.inc</filename> ??????????????????????????????????????????????????????????????
- </simpara>
-
- <informalexample>
- <programlisting role="php">
- <![CDATA[
- <?php
- $a = 1; /* global scope */
-
- function Test()
- {
- echo $a; /* reference to local scope variable */
- }
-
- Test();
- ?>
- ]]>
- </programlisting>
- </informalexample>
-
- <simpara>
- ?????????????? echo ??????????????
- <varname>$a</varname>????????????????????????
- PHP ?????? C ?????????? C
- ????????????????????????????????????????????????????????PHP
- ?????????????????global?
- </simpara>
-
- <sect2 xml:id="language.variables.scope.global">
- <title>global ???</title>
- <simpara>
- ??????? <literal>global</literal> ????
- </simpara>
- <para>
- <example>
- <title>?? global</title>
- <programlisting role="php">
- <![CDATA[
- <?php
- $a = 1;
- $b = 2;
-
- function Sum()
- {
- global $a, $b;
-
- $b = $a + $b;
- }
-
- Sum();
- echo $b;
- ?>
- ]]>
- </programlisting>
- </example>
- </para>
-
- <simpara>
- ?????????“3”????????????
- <varname>$a</varname> ? <varname>$b</varname>???????????????????????????????????????????PHP ?????
- </simpara>
-
- <simpara>
- ?????????????????????? PHP ???
- <varname>$GLOBALS</varname> ?????????????
- </simpara>
-
- <para>
- <example>
- <title>?? <varname>$GLOBALS</varname> ?? global</title>
- <programlisting role="php">
- <![CDATA[
- <?php
- $a = 1;
- $b = 2;
-
- function Sum()
- {
- $GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
- }
-
- Sum();
- echo $b;
- ?>
- ]]>
- </programlisting>
- </example>
- </para>
-
- <simpara>
- <varname>$GLOBALS</varname>
- ????????????????????????????????????<varname>$GLOBALS</varname>
- ??????????????? $GLOBALS ???<link
- linkend="language.variables.superglobals">?????</link>?????????????????
- </simpara>
-
- <para>
- <example>
- <title>??????????????</title>
- <programlisting role="php">
- <![CDATA[
- <?php
- function test_global()
- {
- // ??????????? "super"?????? 'global' ???????????????????
- global $HTTP_POST_VARS;
-
- echo $HTTP_POST_VARS['name'];
-
- // Superglobals ???????????????? 'global' ???Superglobals ?? PHP 4.1.0 ????
- echo $_POST['name'];
- }
- ?>
- ]]>
- </programlisting>
- </example>
- </para>
- </sect2>
-
- <sect2 xml:id="language.variables.scope.static">
- <title>??????</title>
-
- <simpara>
- ?????????????<emphasis>????</emphasis>?static
- variable??????????????????????????????????????????????
- </simpara>
-
- <para>
- <example>
- <title>???????????</title>
- <programlisting role="php">
- <![CDATA[
- <?php
- function Test()
- {
- $a = 0;
- echo $a;
- $a++;
- }
- ?>
- ]]>
- </programlisting>
- </example>
- </para>
-
- <simpara>
- ???????????????????
- <varname>$a</varname> ???? <literal>0</literal> ???
- "0"??????? <varname>$a</varname>++
- ?????????????????
- <varname>$a</varname> ?????????????????????????????
- <varname>$a</varname> ???????
- </simpara>
-
- <para>
- <example>
- <title>?????????</title>
- <programlisting role="php">
- <![CDATA[
- <?php
- function test()
- {
- static $a = 0;
- echo $a;
- $a++;
- }
- ?>
- ]]>
- </programlisting>
- </example>
- </para>
-
- <simpara>
- ?????<varname>$a</varname>?????test()?????????? test() ??????
- <varname>$a</varname> ??????
- </simpara>
-
- <simpara>
- ???????????????????????????????????????????????????????????????????????????????????????
- 10???????
- <varname>$count</varname> ????????
- </simpara>
-
- <para>
- <example>
- <title>?????????</title>
- <programlisting role="php">
- <![CDATA[
- <?php
- function test()
- {
- static $count = 0;
-
- $count++;
- echo $count;
- if ($count < 10) {
- test();
- }
- $count--;
- }
- ?>
- ]]>
- </programlisting>
- </example>
- </para>
-
- <note>
- <para>
- ?????????????????????????????????????????
- </para>
- <para>
- <example>
- <title>??????</title>
- <programlisting role="php">
- <![CDATA[
- <?php
- function foo(){
- static $int = 0; // correct
- static $int = 1+2; // wrong (as it is an expression)
- static $int = sqrt(121); // wrong (as it is an expression too)
-
- $int++;
- echo $int;
- }
- ?>
- ]]>
- </programlisting>
- </example>
- </para>
- </note>
- </sect2>
-
- <sect2 xml:id="language.variables.scope.references">
- <title>??????????</title>
- <simpara>
- ? Zend ?? 1 ?????? PHP4??????
- <link linkend="language.variables.scope.static">static</link> ?
- <link linkend="language.variables.scope.global">global</link>
- ???? <link linkend="language.references">references</link>
- ???????????????????
- <literal>global</literal>
- ????????????????????????????????????????????????????????
- </simpara>
-
- <informalexample>
- <programlisting role="php">
- <![CDATA[
- <?php
- function test_global_ref() {
- global $obj;
- $obj = &new stdclass;
- }
-
- function test_global_noref() {
- global $obj;
- $obj = new stdclass;
- }
-
- test_global_ref();
- var_dump($obj);
- test_global_noref();
- var_dump($obj);
- ?>
- ]]>
- </programlisting>
- </informalexample>
-
- &example.outputs;
-
- <screen>
- NULL
- object(stdClass)(0) {
- }
- </screen>
-
- <simpara>
- ?????????
- <literal>static</literal> ???????????????
- </simpara>
-
- <informalexample>
- <programlisting role="php">
- <![CDATA[
- <?php
- function &get_instance_ref() {
- static $obj;
-
- echo 'Static object: ';
- var_dump($obj);
- if (!isset($obj)) {
- // ????????????
- $obj = &new stdclass;
- }
- $obj->property++;
- return $obj;
- }
-
- function &get_instance_noref() {
- static $obj;
-
- echo 'Static object: ';
- var_dump($obj);
- if (!isset($obj)) {
- // ????????????
- $obj = new stdclass;
- }
- $obj->property++;
- return $obj;
- }
-
- $obj1 = get_instance_ref();
- $still_obj1 = get_instance_ref();
- echo "\n";
- $obj2 = get_instance_noref();
- $still_obj2 = get_instance_noref();
- ?>
- ]]>
- </programlisting>
- </informalexample>
-
- &example.outputs;
-
- <screen>
- Static object: NULL
- Static object: NULL
-
- Static object: NULL
- Static object: object(stdClass)(1) {
- ["property"]=>
- int(1)
- }
- </screen>
-
- <simpara>
- ???????????????????????????
- <literal>&get_instance_ref()</literal> ?????????<emphasis>??</emphasis>?
- </simpara>
- </sect2>
- </sect1>
-
- <sect1 xml:id="language.variables.variable">
- <title>????</title>
-
- <simpara>
- ?????????????????????????????????????????????????????????
- </simpara>
-
- <informalexample>
- <programlisting role="php">
- <![CDATA[
- <?php
- $a = 'hello';
- ?>
- ]]>
- </programlisting>
- </informalexample>
-
- <simpara>
- ?????????????????????????????????????
- <emphasis>hello</emphasis> ??????????$???????????????????????
- </simpara>
-
- <informalexample>
- <programlisting role="php">
- <![CDATA[
- <?php
- $$a = 'world';
- ?>
- ]]>
- </programlisting>
- </informalexample>
-
- <simpara>
- ?????????????<varname>$a</varname> ????“hello”??
- <varname>$hello</varname> ????“world”??????????
- </simpara>
-
- <informalexample>
- <programlisting role="php">
- <![CDATA[
- <?php
- echo "$a ${$a}";
- ?>
- ]]>
- </programlisting>
- </informalexample>
-
- <simpara>
- ??????????????????
- </simpara>
-
- <informalexample>
- <programlisting role="php">
- <![CDATA[
- <?php
- echo "$a $hello";
- ?>
- ]]>
- </programlisting>
- </informalexample>
-
- <simpara>
- ???????<computeroutput>hello world</computeroutput>?
- </simpara>
-
- <simpara>
- ???????????????????????????????
- <varname>$$a[1]</varname>
- ????????????
- <varname>$a[1]</varname>
- ????????????
- <varname>$$a</varname>
- ???????????????? [1]
- ????????????????????
- <varname>${$a[1]}</varname>????????
- <varname>${$a}[1]</varname>?
- </simpara>
-
- <warning>
- <simpara>
- ???? PHP ??????????<link
- linkend="language.variables.superglobals">?????</link>?????????
- </simpara>
- </warning>
-
- </sect1>
-
- <sect1 xml:id="language.variables.external">
- <title>?? PHP ?????</title>
-
- <sect2 xml:id="language.variables.external.form">
- <title>HTML ???GET ? POST?</title>
-
- <simpara>
- ???????? PHP
- ??????????????????????????????????
- </simpara>
-
- <para>
- <example>
- <title>????? HTML ??</title>
- <programlisting role="html">
- <![CDATA[
- <form action="foo.php" method="POST">
- Name: <input type="text" name="username"><br />
- Email: <input type="text" name="email"><br />
- <input type="submit" name="submit" value="Submit me!" />
- </form>
- ]]>
- </programlisting>
- </example>
- </para>
-
- <para>
- ?????????????????????? HTML
- ??????????
- </para>
-
- <para>
- <example>
- <title>?????? POST HTML ??????</title>
- <programlisting role="html">
- <![CDATA[
- <?php
- // ? PHP 4.1.0 ???
- echo $_POST['username'];
- echo $_REQUEST['username'];
-
- import_request_variables('p', 'p_');
- echo $p_username;
- // PHP 6??????? PHP 5.0.0 ????????????
- // ?? register_long_arrays ?????
-
- echo $HTTP_POST_VARS['username'];
-
- // ?? PHP ?? register_globals = on ???????
- // PHP 4.2.0 ????? register_globals = off?
- // ?????/???????
-
- echo $username;
- ?>
- ]]>
- </programlisting>
- </example>
- </para>
- <para>
- ?? GET ?????????????? GET ??????GET
- ????
- QUERY_STRING?URL ??“?”??????????????<literal>http://www.example.com/test.php?id=3</literal>
- ????? <varname>$_GET['id']</varname>
- ??? GET ?????
- <link linkend="reserved.variables.request">$_REQUEST</link> ?
- <function>import_request_variables</function>?
- </para>
-
- <note>
- <para>
- <link linkend="language.variables.superglobals">?????</link>?
- <varname>$_POST</varname> ?? <varname>$_GET</varname> ????
- PHP 4.1.0 ????
- </para>
- </note>
-
- <para>
- ?????? PHP 4.2.0 ?? <link
- linkend="ini.register-globals">register_globals</link>
- ?????
- <emphasis>on</emphasis>?PHP
- ????????????????????????
- <emphasis>off</emphasis>?
- </para>
-
- <note>
- <para>
- <link linkend="ini.magic-quotes-gpc">magic_quotes_gpc</link>
- ???????
- Get?Post ? Cookie ????????? (It's "PHP!") ??????
- (It\'s \"PHP!\")???????????????
- <function>addslashes</function>?<function>stripslashes</function> ?
- <link linkend="ini.magic-quotes-sybase">magic_quotes_sybase</link>?
- </para>
- </note>
-
- <simpara>
- PHP ?????????????????<link
- linkend="faq.html">??????</link>????????????????????????????????????????? POST ?????????????
- </simpara>
-
- <para>
- <example>
- <title>????????</title>
- <programlisting role="php">
- <![CDATA[
- <?php
- if (isset($_POST['action']) && $_POST['action'] == 'submitted') {
- echo '<pre>';
-
- print_r($_POST);
- echo '<a href="'. $_SERVER['PHP_SELF'] .'">Please try again</a>';
-
- echo '</pre>';
- } else {
- ?>
- <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
- Name: <input type="text" name="personal[name]"><br />
- Email: <input type="text" name="personal[email]"><br />
- Beer: <br>
- <select multiple name="beer[]">
- <option value="warthog">Warthog</option>
- <option value="guinness">Guinness</option>
- <option value="stuttgarter">Stuttgarter Schwabenbr</option>
- </select><br />
- <input type="hidden" name="action" value="submitted" />
- <input type="submit" name="submit" value="submit me!" />
- </form>
- <?php
- }
- ?>
- ]]>
- </programlisting>
- </example>
- </para>
-
- <sect3 xml:id="language.variables.external.form.submit">
- <title>IMAGE SUBMIT ???</title>
-
- <simpara>
- ?????????????????????????????????
- </simpara>
-
- <informalexample>
- <programlisting role="html">
- <![CDATA[
- <input type="image" src="image.gif" name="sub" />
- ]]>
- </programlisting>
- </informalexample>
-
- <simpara>
- ???????????????????????????????????
- sub_x ? sub_y???????????????????????????????????????????????????????
- sub.x ? sub.y??? PHP ????????????
- </simpara>
- </sect3>
-
- </sect2>
-
- <sect2 xml:id="language.variables.external.cookies">
- <title>HTTP Cookies</title>
-
- <simpara>
- PHP ????? <link
- xlink:href="&url.rfc;6265">RFC 6265</link>????
- HTTP cookies?Cookies
- ???????????????????????????????????
- <function>setcookie</function>
- ???? cookies?Cookies ?
- HTTP ???????????
- SetCookie ??????????????????????
- <function>header</function> ??????????Cookie
- ??????? cookie ??????????
- <varname>$_COOKIE</varname>?<varname>$HTTP_COOKIE_VARS</varname> ?
- <varname>$_REQUEST</varname>?????????
- <function>setcookie</function> ?????
- </simpara>
-
- <simpara>
- ??????????? cookie ???????????????
- </simpara>
-
- <informalexample>
- <programlisting role="php">
- <![CDATA[
- <?php
- setcookie("MyCookie[foo]", 'Testing 1', time()+3600);
- setcookie("MyCookie[bar]", 'Testing 2', time()+3600);
- ?>
- ]]>
- </programlisting>
- </informalexample>
-
- <simpara>
- ?????????? cookie??? MyCookie
- ????????????????????? cookie
- ???????????????
- <function>serialize</function> ?
- <function>explode</function>?
- </simpara>
-
- <simpara>
- ????????? cookie ??????????
- cookie??????????????????????????????????????
- </simpara>
-
- <example>
- <title>?? <function>setcookie</function> ???</title>
- <programlisting role="php">
- <![CDATA[
- <?php
- if (isset($_COOKIE['count'])) {
- $count = $_COOKIE['count'] + 1;
- } else {
- $count = 1;
- }
- setcookie('count', $count, time()+3600);
- setcookie("Cart[$count]", $item, time()+3600);
- ?>
- ]]>
- </programlisting>
- </example>
-
- </sect2>
-
- <sect2 xml:id="language.variables.external.dot-in-names">
- <title>??????</title>
-
- <para>
- ???PHP ?????????????????????????????
- PHP ??????????????????
- <programlisting role="php">
- <![CDATA[
- <?php
- $varname.ext; /* ????? */
- ?>
- ]]>
- </programlisting>
- ?????????????
- <varname>$varname</varname>
- ?????????????????????????????????????????????????????????'ext'?????????????
- </para>
-
- <para>
- ????????? PHP
- ??????????????????
- </para>
-
- </sect2>
-
- <sect2 xml:id="language.variables.determining-type-of">
- <title>??????</title>
-
- <para>
- ?? PHP ??????????????????????????????????????????????PHP
- ???????????????????<function>gettype</function>?<function>is_array</function>?<function>is_float</function>?<function>is_int</function>?<function>is_object</function> ?
- <function>is_string</function>???<link
- linkend="language.types">??</link>???
- </para>
- </sect2>
-
- </sect1>
-
- </chapter>
-
- <!-- Keep this comment at the end of the file
- Local variables:
- mode: sgml
- sgml-omittag:t
- sgml-shorttag:t
- sgml-minimize-attributes:nil
- sgml-always-quote-attributes:t
- sgml-indent-step:1
- sgml-indent-data:t
- indent-tabs-mode:nil
- sgml-parent-document:nil
- sgml-default-dtd-file:"~/.phpdoc/manual.ced"
- sgml-exposed-tags:nil
- sgml-local-catalogs:nil
- sgml-local-ecat-files:nil
- End:
- vim600: syn=xml fen fdm=syntax fdl=2 si
- vim: et tw=78 syn=sgml
- vi: ts=1 sw=1
- -->