cvs: phpdoc-ar /language variables.xml

From: Date: Sun, 12 Oct 2003 06:44:12 +0000
Subject: cvs: phpdoc-ar /language variables.xml
Groups: php.doc.ar 
Request: Send a blank email to doc-ar+get-578@lists.php.net to get a copy of this message
hafid Sun Oct 12 02:44:12 2003 EDT Added files: /phpdoc-ar/language variables.xml Log: Initial and Partial Translation. 1.70 Compliant.

Index: phpdoc-ar/language/variables.xml +++ phpdoc-ar/language/variables.xml <?xml version="1.0" encoding="WINDOWS-1256"?> <!-- $Revision: 1.1 $ --> <!-- EN-Revision: 1.70 Maintainer: hafid Status: working --> <chapter id="language.variables"> <title>ÇáãõÊÛíøöÑÇÊ</title> <sect1 id="language.variables.basics"> <title>ÃÓÇÓíÇÊ</title> <simpara> ÊõãóËøóáõ ÇáãÊÛíøöÑÇÊ Ýí PHP ãä ÎáÇá ÑãÒ ÇáÏæáÇÑ ãÊÈæÚÇ ÈÅÓã ÇáãÊÛíøöÑ. íÊÓã ÅÓã ÇáãÊÛíøöÑ Èßæäå ãÊÍÓøöÓÇ áÍÇáÉ ÇáÍÑæÝ (case-sensitive). </simpara> <para> ÊÊÈÚ ÃÓãÇÁ ÇáãÊÛíøöÑÇÊ äÝÓ ÞæÇÚÏ ÇáÊøóÓãíÇÊ (labels) ÇáÃÎÑì Ýí PHP. ÅÓã ãÊÛíøöÑ ÕÇáÍ íÈÏà ÈÍÑÝ Ãæ ÔÑØÉ ÓÝáíøÉ (underscore)¡ ãÊÈæÚÇ ÈÃí ÚÏÏ ãä ÇáÍÑæÝ¡ ÇáÃÚÏÇÏ Ãæ ÇáÔøóÑØÇÊ ÇáÓøõÝáíøÉ (underscores). ÈÇÚÊÈÇÑå ÚÈÇÑÉ ÇÚÊíÇÏíøÉ (regular expression)¡ ÝÅäøå íãßä ÇáÊøóÚÈíÑ ÚäåÇ åßÐÇ: '[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> <para> <informalexample> <programlisting role="php"> <![CDATA[ <?php $var = "ÍÝíÙ"; $Var = "ÕáÇÍ"; echo "$var¡ $Var"; // ÇáãõÎÑÌ åæ "ÍÝíÙ¡ ÕáÇÍ" $4site = 'áíÓ ÈÚÏ'; // ÛíÑ ÕÇáͺ íÈÏà ÈÚÏÏ $_4site = 'áíÓ ÈÚÏ'; // ÕÇáͺ íÈÏà ÈÔÑØÉ ÓÝáíøÉ (underscore) $täyte = 'mansikka'; // ÕÇáͺ 'ä' åæ ASCII 228 (ãõæóÓøóÚ). ?> ]]> </programlisting> </informalexample> </para> <para> Ýí PHP 3¡ ÏÇÆãÇ ãÇ íÊã ÊÚííä (assigned) ÇáãÊÛíøöÑÇÊ ãä ÎáÇá ÞíãÉ. íÚäí åÐÇ Ãäøå ÚäÏãÇ ÊÞæã ÈÊÚííä (assign) ÚÈÇÑÉ (expression) áãÊÛíøöÑ¡ ÝÅäøå íÊã äÓÎ ÇáÞíãÉ ÇáßáøöíøóÉ ááÚÈÇÑÉ ÇáÃÕáíøÉ Åáì ÇáãÊÛíøöÑ ÇáæõÌåÉ. íÚäí åÐÇ¡ æÚáì ÓÈíá ÇáãËÇá¡ Ãäøå ÈÚÏ ÊÚííä (assigning) ÞíãÉ ãÊÛíøöÑ ãÇ áÂÎÑ¡ ÝÅäø ÊÛííÑ ÃÍÏ åÐå ÇáãÊÛíøöÑÇÊ ÓæÝ áä íßæä áå ÊÃËíÑÇ Úáì ÇáÂÎÑ. áãÒíÏ ãä ÇáãÚáæãÇÊ ÈÎÕæÕ åÐÇ ÇáäøæÚ ãä ÇáÊøóÚííä (assignment)¡ ÇÓÊÚÑÖ ÇáÝÕá ÇáãÊÚáÞ ÈÜ <link linkend="language.expressions">ÇáÚÈÇÑÇÊ (Expressions)</link>. </para> <para> ÊõæóÝøöÑ PHP 4 ØÑíÞÉ ÃÎÑì áÊÚííä (assign) Þíã áãÊÛíøöÑÇÊ: <link linkend="language.references">ÇáÊøóÚííä ÈÇáÅÍÇáÉ (assign by reference)</link>. íÚäí åÐÇ Ãäø ÇáãÊÛíøöÑ ÇáÌÏíÏ¡ æÈÈÓÇØÉ¡ ÓíÍíá (ÈÚÈÇÑÇÊ ÃÎÑì¡ "íÕÈÍ ÅÓãÇ ãõäÊóÍóáÇ (alias) áÜ" Ãæ "íõÔíÑ Åáì") Åáì ÇáãÊÛíøöÑ ÇáÃÕáíø. ÊÄËÑ ÇáÊøóÛííÑÇÊ ÇáØøÇÑÆÉ Úáì ÇáãõÊóÛóíøöÑ ÇáÌÏíÏ Ýí ÇáãÊÛíøöÑ ÇáÃÕáíø¡ æÇáÚßÓ ÕÍíÍ. íÚäí åÐÇ ÃíÖÇ Ãäøå áä ÊÊã ÃíÉ ÚãáíÉ äÓκ æåßÐÇ¡ ÝÅäø ÇáÊøóÚííä (assignment) íÍÏË ÈÔßá ÃÓÑÚ. ãÚ Ðáß¡ ÝÅäø Ãí ÊÓÑíÚ¡ Úáì ãÇ íÙåÑ¡ ÓíÊã ÇáÊøóäÈíå Åáíå ÝÞØ Ýí ÇáÍáÞÇÊ ÇáãÔÏæÏÉ (tight loops) Ãæ ÚäÏ ÊÚííä (assigning) <link linkend="language.types.array">ãÕÝæÝÇÊ (arrays)</link> Ãæ <link linkend="language.types.object">ßÇÆäÇÊ (objects)</link> ßÈíÑÉ. </para> <para> ÈÛÑÖ ÇáÊøÚííä ÈÇáÅÍÇáÉ (assign by reference)¡ ÝÞØ ÇÓÊåá (prepend) ÇáãÊÛíøöÑ ÇáÌÇÑí ÊÚííäå (ÇáãÊÛíøöÑ ÇáãÕÏÑíø - source variable) ÈæÇæ ÊÌÇÑíøÉ (&amp;) Úáì ÓÈíá ÇáãËÇá: ÇáãÞÊØÝ ÇáÊøÇáíø ãä ÇáÔøöíÝÑÉ ÓíÓÊÕÏÑ ÇáãõÎúÑóÌ 'ÅÓãí åæ ãÞÏÓí' ãÑÊíä: <informalexample> <programlisting role="php"> <![CDATA[ <?php $foo = 'ãÞÏÓí'; // íõÚíøöä (assign) ÇáÞíãÉ 'ãÞÏÓí' Åáì $foo $bar = &$foo; // íÍíá Åáì (Reference) $foo ÚÈÑ $bar. $bar = "ÅÓãí åæ $bar"; // ÈÚÏ $bar... echo $bar; echo $foo; // $foo íÊã ÊÍæíÑåÇ (alter) ÃíÖÇ. ?> ]]> </programlisting> </informalexample> </para> <para> åäÇß ÔíÁ ãåã íÌÏÑ ÇáÊøóäÈíå Åáíå æåæ Ãäøå ÝÞØ ÇáãÊÛíøöÑÇÊ ÇáãõÓãÇÉ (named variables) åí ÇáÊøí íãßä ÊÚííäåÇ ÈÇáÅÍÇáÉ (assigned by reference). <informalexample> <programlisting role="php"> <![CDATA[ <?php $foo = 25; $bar = &$foo; // åÐÇ ÊÚííä ÕÇáÍ (valid assignment). $bar = &(24 * 7); // ÛíÑ ÕÇáÍ (Invalid)º íÍíá (references) Åáì ÚÈÇÑÉ ÛíÑ ãÓãÇÉ (unnamed expression). function test() { return 25; } $bar = &test(); // ÛíÑ ÕÇáÍ. ?> ]]> </programlisting> </informalexample> </para> </sect1> <sect1 id="language.variables.predefined"> <title>ÇáãÊÛíÑÇÊ ÇáãÚÑÝÉ ãÓÈÞÇ (Predefined variables)</title> <simpara> ÊõÞÏøöã PHP ÚÏÏ ßÈíÑÇ ãä ÇáãÊÛíÑÇÊ ÇáãÚÑÝÉ ãÓÈÞÇ áÃí ÎÇØæØ ÊõÔÛøöáõå. ãÚ Ðáß¡ ÝÅäø ÇáÚÏíÏ ãä åÐå ÇáãÊÛíøöÑÇÊ áÇ íãßä ÊæËíÞåÇ ÈÔßá ßÇãá ÈÇÚÊÈÇÑ ÃäåÇ ÊÇÈÚÉ (dependent) áäæÚ ÇáÎÇÏã ÇáÌÇÑí ÊÔÛíáå¡ ÇáÅÕÏÇÑÉ æÊäÕíÈ ÇáÎÇÏã æÚæÇãá ÃÎÑì. ÈÚÖ åÐå ÇáãÊÛíÑÇÊ áä Êßæä ãÊÇÍÉ ÚäÏãÇ íÊã ÊÔÛíá PHP ãä <link linkend="features.commandline">ÓØÑ ÇáÃæÇãÑ</link>. ááÍÕæá Úáì áÇÆÍÉ ÈåÐå ÇáãÊÛíÑÇÊ¡ ÑÌÇÁð¡ ÇÓÊÚÑÖ ÇáÝÕá ÇáãÊÚáÞ ÈÜ <link linkend="reserved.variables">ÇáãÊÛíÑÇÊ ÇáãÚÑÝÉ ãÓÈÞÇ æÇáãÍÌæÒÉ (Reserved Predefined 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> íÄËÑ Ýí ãÌãæÚÉ ÇáãÊÛíøöÑÇÊ ÇáãÚÑÝÉ ãÓÈÞÇ æÇáãÊÇÍÉ Ýí ÇáãÏì ÇáÔøóÇãá (global scope). Úáì ÓÈíá ÇáãËÇá¡ ááÍÕæá Úáì <varname>DOCUMENT_ROOT</varname>¡ ÓÊÓÊÎÏã <varname>$_SERVER['DOCUMENT_ROOT']</varname> ÈÏáÇ ãä <varname>$DOCUMENT_ROOT</varname> Ãæ <varname>$_GET['id']</varname> ÅäØáÇÞÇ ãä ãÍÏøöÏ ÇáãæÑÏ ÇáãÊÔÇßá (URL) <literal>http://www.example.com/test.php?id=3</literal> ÈÏáÇ ãä <varname>$id</varname> Ãæ <varname>$_ENV['HOME']</varname> ÈÏáÇ ãä <varname>$HOME</varname>. </simpara> <simpara> ááÍÕæá Úáì ãÚáæãÇÊ ÐÇÊ ÕáÉ ÈÎÕæÕ åÐÇ ÇáÊøóÛííÑ¡ ÇÞÑà ãõÏúÎóá ÇáÊøóåíÆÉ (configuration entry) áÜ <link linkend="ini.register-globals">register_globals</link>¡ ÝÕá ÇáÃãä ÇáãÊÚáÞ ÈÜ <link linkend="security.registerglobals">ÇÓÊÎÏÇã Register Globals </link>ÅÚáÇäÇÊ äóÔÑ PHP <ulink url="&url.php.release4.1.0;">4.1.0 </ulink> æ <ulink url="&url.php.release4.2.0;">4.2.0</ulink> . </simpara> <simpara> íõÝÖøóá ÇÓÊÎÏÇã ãÇ åæ ãÊÇÍ ãä ãÊÛíøöÑÇÊ ãõÚóÑøóÝóÉ ãÓÈÞÇ æãÍÌæÒÉ¡ ãËá <link linkend="language.variables.superglobals">ÇáãÕÝæÝÇÊ ÇáÝÇÆÞÉ ÇáÔøõãæá (superglobal arrays)</link> . </simpara> </warning> <simpara> ÅÈÊÏÇÁð ãä ÇáÅÕÏÇÑÉ 4.1.0 ÝÕÇÚÏÇð¡ ÃÕÈÍÊ PHP ÊõÞóÏøöã ãÌãæÚÉ ÅÖÇÝíøÉ ãä ÇáãÕÝæÝÇÊ ÇáãõÚóÑøóÝóÉ ãÓÈÞÇ (predefined arrays) ÊÍÊæí ãÊÛíøöÑÇÊ ÅäØáÇÞÇ ãä ÇáÎÇÏã ÇáÔøõÚíø (Åä ÞÈá ÇáÊøóØÈíÞ)¡ ÇáÈíÆÉ (environment) æãõÏúÎáÇÊ ÇáãÓÊÎÏã (user input). ÊÊÓã åÐå ÇáãÕÝæÝÇÊ ÇáÌÏíÏÉ ÈßæäåÇ ÎÇÕÉ¡ Åáì ÍÏø ãÇ¡ ãä ÍíË ÃäøåÇ Êßæä ÔÇãáÉ Úáì äÍæ Âáíø --Ãí ÃäøåÇ Êßæä ãÊæÝÑÉ Úáì äÍæ Âáíø Ýí ßáø ãÏì (scope). áåÐÇ ÇáÓøóÈÈ¡ ÝÅäøóåÇ ÛÇáÈÇ ãÊ ÊÚÑÝ ÈÜ 'ÇáÔøóÇãáÇÊ ÇáÂáíøÉ - autoglobals' Ãæ 'ÇáÔøóÇãáÇÊ ÇáÝÇÆÞÉ - superglobals'. (áÇ ÊæÌÏ ÂáíÉ Ýí PHP áÃÌá ÇáÔøóÇãáÇÊ ÇáÝÇÆÞÉ ÇáãõÚóÑøóÝÉ ãä ÞÈá ÇáãÓÊÎÏã - user-defined superglobals.) ÊæÌÏ ÇáÔøóÇãáÇÊ ÇáÝÇÆÞÉ Ýí ÇáÃÓÝá æåí ãõÏæøóäóÉ Ýí áÇÆÍɺ ãÚ Ðáß¡ æááÍÕæá Úáì áÇÆÍÉ ÈãÍÊæíÇÊåÇ æáäÞÇÔ ÃÚãÞ ÈÎÕæÕ ãÊÛíÑÇÊ PHP ÇáãÚÑÝÉ ãÓÈÞÇ æØÈíÚÊåÇ¡ ÑÌÇÁð ÇÓÊÚÑÖ ÝÕá <link linkend="reserved.variables">ÇáãÊÛíÑÇÊ ÇáãÚÑÝÉ ãÓÈÞÇ æÇáãÍÌæÒÉ (Reserved Predefined Variables)</link>. ßÐáß¡ ÝÅäøóß ÓÊáÇÍÙ ßíÝ Ãä ÇáãÊÛíøöÑÇÊ ÇáãÚÑÝÉ ãÓÈÞÇ ÇáÞÏíãÉ (<varname>$HTTP_*_VARS</varname>) áÇ ÒÇáÊ ãæÌæÏÉ. &avail.register-long-arrays; </simpara> <note> <title>ÇáãÊÛíøöÑÇÊ ÇáãÊÛíøöÑÉ (Variable variables)</title> <para> áÇ íãßä ÇÓÊÎÏÇã ÇáÔøóÇãáÇÊ ÇáÝÇÆÞÉ ßÜ <link linkend="language.variables.variable">ãÊÛíøöÑÇÊ ãÊÛíøöÑÉ (variable variables)</link>. </para> </note> <para> ÅÐÇ áã íÊã ÊÍÏíÏ ÈÚÖ ÇáãÊÛíøöÑÇÊ Ýí <link linkend="ini.variables-order">variables_order</link>¡ ÝÅäø ãÇ íÞÇÈáåÇ ãä ãÕÝæÝÇÊ PHP ÇáãÚÑÝÉ ãÓÈÞÇ ÇáãáÇÆãÉ íÊã ÊÑßåÇ ÝÇÑÛÉ ÃíÖÇ. </para> <variablelist id="language.variables.superglobals"> <title>ÔÇãáÇÊ PHP ÇáÝÇÆÞÉ (PHP Superglobals)</title> <varlistentry> <term><link linkend="reserved.variables.globals">$GLOBALS</link></term> <listitem> <simpara> ÊÍÊæí Úáì ÅÍÇáÉ (reference) Åáì ßá ãÊÛíøöÑ íÊæÝÑ ÍÇáíÇ Öãä ÇáãÏì ÇáÔøóÇãá (global scope) ÇáÎÇÕ ÈÇáÎÇØæØ (script). ãÝÇÊíÍ åÐå ÇáãÕÝæÝÉ (keys of array) åí ÃÓãÇÁ ÇáãÊÛíøöÑÇÊ ÇáÔøóÇãáÉ (global variables). æõÌöÏóÊ <varname>$GLOBALS</varname> ãäÐ PHP 3. </simpara> </listitem> </varlistentry> <varlistentry> <term><link linkend="reserved.variables.server">$_SERVER</link></term> <listitem> <simpara> ãÊÛíøöÑÇÊ ãõÍÏøóÏóÉ ãä ÞÈá ÇáÎÇÏã ÇáÔøõÚíø Ãæ ãÇ ÚÏÇ Ðáß ãÑÊÈØ ãÈÇÔÑÉ ÈÈíÆÉ ÊäÝíÐ ÇáÎÇØæØ ÇáÌÇÑíø. ãËáãÇ ÇáÍÇá ãÚ ãÕÝæÝÉ <varname>$HTTP_SERVER_VARS</varname> ÇáÞÏíãÉ (ÇáÊøí áÇ ÊÒÇá ãÊæÝÑÉ¡ áßä ãäÞæÕÉ ÇáÞíãÉ "deprecated"). </simpara> </listitem> </varlistentry> <varlistentry> <term><link linkend="reserved.variables.get">$_GET</link></term> <listitem> <simpara> ãÊÛíøöÑÇÊ íõÒóæøóÏ ÈåÇ ÇáÎÇØæØ ÚÈÑ HTTP GET. ãËáãÇ ÇáÍÇá ãÚ ãÕÝæÝÉ <varname>$HTTP_GET_VARS</varname> ÇáÞÏíãÉ (ÇáÊøí áÇ ÊÒÇá ãÊæÝÑÉ¡ áßä ãäÞæÕÉ ÇáÞíãÉ "deprecated"). </simpara> </listitem> </varlistentry> <varlistentry> <term><link linkend="reserved.variables.post">$_POST</link></term> <listitem> <simpara> ãÊÛíøöÑÇÊ íõÒóæøóÏ ÈåÇ ÇáÎÇØæØ ÚÈÑ HTTP POST. ãËáãÇ ÇáÍÇá ãÚ ãÕÝæÝÉ <varname>$HTTP_POST_VARS</varname> ÇáÞÏíãÉ (ÇáÊøí áÇ ÊÒÇá ãÊæÝÑÉ¡ áßä ãäÞæÕÉ ÇáÞíãÉ "deprecated"). </simpara> </listitem> </varlistentry> <varlistentry> <term><link linkend="reserved.variables.cookies">$_COOKIE</link></term> <listitem> <simpara> ãÊÛíøöÑÇÊ íõÒóæøóÏ ÈåÇ ÇáÎÇØæØ ÚÈÑ ãáÝÇÊ ÇÑÊÈÇØ HTTP (HTTP cookies). ãËáãÇ ÇáÍÇá ãÚ ãÕÝæÝÉ <varname>$HTTP_COOKIE_VARS</varname> ÇáÞÏíãÉ (ÇáÊøí áÇ ÊÒÇá ãÊæÝÑÉ¡ áßä ãäÞæÕÉ ÇáÞíãÉ "deprecated"). </simpara> </listitem> </varlistentry> <varlistentry> <term><link linkend="reserved.variables.files">$_FILES</link></term> <listitem> <simpara> ãÊÛíøöÑÇÊ íõÒóæøóÏ ÈåÇ ÇáÎÇØæØ ÚÈÑ ÊÕÚíÏÇÊ ÇáãáÝÇÊ (file uploads) ÈæÇÓØÉ HTTP post. ãËáãÇ ÇáÍÇá ãÚ ãÕÝæÝÉ <varname>$HTTP_POST_FILES</varname> ÇáÞÏíãÉ (ÇáÊøí áÇ ÊÒÇá ãÊæÝÑÉ¡ áßä ãäÞæÕÉ ÇáÞíãÉ "deprecated"). ÇÓÊÚÑÖ <link linkend="features.file-upload.post-method">ÇáÊøóÕÚíÏÇÊ ÈæÇÓØÉ ãäåÌ POST (POST method uploads)</link> æÐáß ááÍÕæá Úáì ãÒíÏ ãä ÇáãÚáæãÇÊ. </simpara> </listitem> </varlistentry> <varlistentry> <term><link linkend="reserved.variables.environment">$_ENV</link></term> <listitem> <simpara> ãÊÛíøöÑÇÊ íõÒóæøóÏ ÈåÇ ÇáÎÇØæØ ÚÈÑ ÇáÈíÆÉ. ãËáãÇ ÇáÍÇá ãÚ ãÕÝæÝÉ <varname>$HTTP_ENV_VARS</varname> ÇáÞÏíãÉ (ÇáÊøí áÇ ÊÒÇá ãÊæÝÑÉ¡ áßä ãäÞæÕÉ ÇáÞíãÉ "deprecated"). </simpara> </listitem> </varlistentry> <varlistentry> <term><link linkend="reserved.variables.request">$_REQUEST</link></term> <listitem> <simpara> ãÊÛíøöÑÇÊ íõÒóæøóÏ ÈåÇ ÇáÎÇØæØ ÚÈÑ ÂáíÇÊ ÅÏÎÇá (input mechanisms) GET¡ POST æ COOKIE æåí ÈÐáß áÇ íãßä ÇáæËæÞ ÈåÇ. Åäøó æÌæÏ æÊÑÊíÈ ÊÖãíä ÇáãÊÛíøöÑÇÊ (order of variable inclusion) Ýí åÐå ÇáãÕÝæÝÉ íÊã ÊÚÑíÝå ÈÍÓÈ ÊæÌíå ÊåíÆÉ (configuration directive) PHP <link linkend="ini.variables-order">variables_order</link>. áÇ ÊÊæÝÑ åÐå ÇáãÕÝæÝÉ Úáì äÙíÑ ãÈÇÔÑ Ýí ÅÕÏÇÑÇÊ PHP ÇáÓøóÇÈÞÉ áÜ 4.1.0. ÇÓÊÚÑÖ ÃíÖÇ <function>import_request_variables</function>. </simpara> <caution> <simpara> ÅÚÊÈÇÑÇð ãä PHP 4.3.0¡ áã ÊÚÏ ãÚáæãÇÊ FILE ÅäØáÇÞÇ ãä <varname>$_FILES</varname> ãæÌæÏÉ Ýí <varname>$_REQUEST</varname>. </simpara> </caution> <note> <simpara> ÚäÏ ÇáÊøóÔÛíá ãä <link linkend="features.commandline">ÓØÑ ÇáÃæÇãÑ (command line) </link>¡ ÝÅäø åÐÇ <emphasis>áä</emphasis> íÊÖãä ãõÏúÎóáÇÊ <varname>argv</varname> æ <varname>argc</varname>º ÊÊæÇÌÏ åÐå Ýí ãÕÝæÝÉ <varname>$_SERVER</varname>. </simpara> </note> </listitem> </varlistentry> <varlistentry> <term><link linkend="reserved.variables.session">$_SESSION</link></term> <listitem> <simpara> ãÊÛíøöÑÇÊ ãõÓÌáÉ ÍÇáíÇ Ýí ÌáÓÉ ÇáÎÇØæØ. ãËáãÇ ÇáÍÇá ãÚ ãÕÝæÝÉ <varname>$HTTP_SESSION_VARS</varname> ÇáÞÏíãÉ (ÇáÊøí áÇ ÊÒÇá ãÊæÝÑÉ¡ áßä ãäÞæÕÉ ÇáÞíãÉ "deprecated"). ÇÓÊÚÑÖ ÝÕá <link linkend="ref.session">ÏæÇá ÇáÊøóÚÇãá ãÚ ÇáÌáÓÇÊ (Session handling functions)</link> æÐáß ááÍÕæá Úáì ãÒíÏ ãä ÇáãÚáæãÇÊ. </simpara> </listitem> </varlistentry> </variablelist> </sect1> <sect1 id="language.variables.scope"> <title>ãÏì ÇáãÊÛíøöÑÇÊ (Variable scope)</title> <simpara> ãÏì ãÊÛíøöÑ åæ ÇáÓøöíÇÞ ÇáÐí ÊõÚÑøóÝ Öãäå. ÈÇáäøöÓÈÉ áãÚÙã ãÊÛíøöÑÇÊ PHP¡ íæÌÏ ãÏì ÃæÍÏ ÝÞØ. íãÊÏ åÐÇ ÇáãÏì ÇáÃæÍÏ (single scope) Åáì ÇáãáÝÇÊ ÇáãÊÖãäÉ æÇáãØáæÈÉ ßÐáß. Úáì ÓÈíá ÇáãËÇá: </simpara> <informalexample> <programlisting role="php"> <![CDATA[ <?php $a = 1; include "b.inc"; ?> ]]> </programlisting> </informalexample> <simpara> åäÇ¡ Óíßæä ÇáãÊÛíøöÑ <varname>$a</varname> ãÊæÝÑÇ Öãä ÎÇØæØ <filename>b.inc</filename> ÇáãõÊóÖøóãóä. ãÚ Ðáß¡ ÝÅäøóå Öãä ÏæÇá ãõÚóÑøóÝóÉ ãä ÞÈá ÇáãÓÊÎÏã (user-defined functions) íÊã ÅÏÑÇÌ ãÏì ÏÇáÉ ãÍáíø (local function scope). Ãí ãÊÛíøöÑ íÓÊÎÏã Öãä ÏÇáÉ åæ ãÍÏæÏ¡ æÈÕæÑÉ ÇÝÊÑÇÖíøÉ¡ ÈãÏì ÇáÏøóÇáÉ ÇáãÍáíø. Úáì ÓÈíá ÇáãËÇá: </simpara> <informalexample> <programlisting role="php"> <![CDATA[ <?php $a = 1; /* ãÏì ÔÇãá (global scope) */ function Test() { echo $a; /* ÅÍÇáÉ Åáì ãÊÛíøöÑ ãÏì ãÍáíø (reference to local scope variable) */ } Test(); ?> ]]> </programlisting> </informalexample> <simpara> åÐÇ ÇáÎÇØæØ áä íõäúÊöÌó Ãíøó ãõÎúÑóÌ áÃäøó ÈáÇÛ (statement) echo íÍíá (refer) Åáì ÅÕÏÇÑÉ ãÍáíøÉ ãä ãÊÛíøöÑ <varname>$a</varname> æáã ÊõÚíøóä áå ÞíãÉ Öãä åÐÇ ÇáãÏì. íãßä Ãä ÊáÇÍÙ Ãäøó åÐÇ ãÎÊáÝ ÔíÆÇ ãÇ Úä áÛÉ C ãä ÍíË Ãäøó ÇáãÊÛíøöÑÇÊ ÇáÔøóÇãáÉ (global variables) Ýí C ÊÊæÝÑ ÂáíÇ áÃÌá ÇáÏøóæÇá ÅáÇ ÅÐÇ ÓõÍöÞóÊ Úáì äÍæ ÎÇÕ ãä ÞÈá ÊÚÑíÝ ãÍáíø (local definition). íãßä áåÐÇ Ãä íÊÓÈÈ Ýí ÈÚÖ ÇáãÔÇßá ãä ÍíË Ãä íãßä ááÈÚÖ Ãä íÛíÑ¡ Úä ÛíÑ ÞÕÏ¡ ãÊÛíøöÑÇð ÔÇãáÇ (global variable). Ýí PHP¡ íÌÈ Ãä ÊõÕóÑøóÍ ÇáãÊÛíøöÑÇÊ ÇáÔøóÇãáÉ Úáì ÃäøóåÇ ÔÇãáÉ (global) ÏÇÎá ÏÇáÉ Åä ßÇä ÓíÊã ÇÓÊÎÏÇãåÇ Ýí Êáß ÇáÏøóÇáÉ. </simpara> <sect2 id="language.variables.scope.global"> <title>ÇáßáãÉ ÇáãÝÊÇÍíøÉ global</title> <simpara> First, an example use of <literal>global</literal>: </simpara> <para> <example> <title>Using 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> The above script will output &quot;3&quot;. By declaring <varname>$a</varname> and <varname>$b</varname> global within the function, all references to either variable will refer to the global version. There is no limit to the number of global variables that can be manipulated by a function. </simpara> <simpara> A second way to access variables from the global scope is to use the special PHP-defined <varname>$GLOBALS</varname> array. The previous example can be rewritten as: </simpara> <para> <example> <title>Using <varname>$GLOBALS</varname> instead of 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> The <varname>$GLOBALS</varname> array is an associative array with the name of the global variable being the key and the contents of that variable being the value of the array element. Notice how <varname>$GLOBALS</varname> exists in any scope, this is because $GLOBALS is a <link linkend="language.variables.superglobals">superglobal</link>. Here's an example demonstrating the power of superglobals: </simpara> <para> <example> <title>Example demonstrating superglobals and scope</title> <programlisting role="php"> <![CDATA[ <?php function test_global() { // Most predefined variables aren't "super" and require // 'global' to be available to the functions local scope. global $HTTP_POST_VARS; print $HTTP_POST_VARS['name']; // Superglobals are available in any scope and do // not require 'global'. Superglobals are available // as of PHP 4.1.0 print $_POST['name']; } ?> ]]> </programlisting> </example> </para> </sect2> <sect2 id="language.variables.scope.static"> <title>Using static variables</title> <simpara> Another important feature of variable scoping is the <emphasis>static</emphasis> variable. A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope. Consider the following example: </simpara> <para> <example> <title>Example demonstrating need for static variables</title> <programlisting role="php"> <![CDATA[ <?php function Test () { $a = 0; echo $a; $a++; } ?> ]]> </programlisting> </example> </para> <simpara> This function is quite useless since every time it is called it sets <varname>$a</varname> to <literal>0</literal> and prints &quot;0&quot;. The <varname>$a</varname>++ which increments the variable serves no purpose since as soon as the function exits the <varname>$a</varname> variable disappears. To make a useful counting function which will not lose track of the current count, the <varname>$a</varname> variable is declared static: </simpara> <para> <example> <title>Example use of static variables</title> <programlisting role="php"> <![CDATA[ <?php function Test() { static $a = 0; echo $a; $a++; } ?> ]]> </programlisting> </example> </para> <simpara> Now, every time the Test() function is called it will print the value of <varname>$a</varname> and increment it. </simpara> <simpara> Static variables also provide one way to deal with recursive functions. A recursive function is one which calls itself. Care must be taken when writing a recursive function because it is possible to make it recurse indefinitely. You must make sure you have an adequate way of terminating the recursion. The following simple function recursively counts to 10, using the static variable <varname>$count</varname> to know when to stop: </simpara> <para> <example> <title>Static variables with recursive functions</title> <programlisting role="php"> <![CDATA[ <?php function Test() { static $count = 0; $count++; echo $count; if ($count < 10) { Test (); } $count--; } ?> ]]> </programlisting> </example> </para> <note> <para> Static variables maybe declared as seen in the examples above. Trying to assign values to these variables which are the result of expressions will cause a parse error. </para> <para> <example> <title>Declaring static variables</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 id="language.variables.scope.references"> <title>References with global and static variables</title> <simpara> The Zend Engine 1, driving <literal>PHP4</literal>, implements the <link linkend="language.variables.scope.static">static</link> and <link linkend="language.variables.scope.global">global</link> modifier for variables in terms of <link linkend="language.references"> references</link>. For example, a true global variable imported inside a function scope with the <literal>global</literal> statement actually creates a reference to the global variable. This can lead to unexpected behaviour which the following example addresses: </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> <simpara> Executing this example will result in the following output: </simpara> <screen> NULL object(stdClass)(0) { } </screen> <simpara> A similar behaviour applies to the <literal>static</literal> statement. References are not stored statically: </simpara> <informalexample> <programlisting role="php"> <![CDATA[ <?php function &get_instance_ref() { static $obj; echo "Static object: "; var_dump($obj); if (!isset($obj)) { // Assign a reference to the static variable $obj = &new stdclass; } $obj->property++; return $obj; } function &get_instance_noref() { static $obj; echo "Static object: "; var_dump($obj); if (!isset($obj)) { // Assign the object to the static variable $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> <simpara> Executing this example will result in the following output: </simpara> <screen> Static object: NULL Static object: NULL Static object: NULL Static object: object(stdClass)(1) { ["property"]=> int(1) } </screen> <simpara> This example demonstrates that when assigning a reference to a static variable, it's not <emphasis>remembered</emphasis> when you call the <literal>&amp;get_instance_ref()</literal> function a second time. </simpara> </sect2> </sect1> <sect1 id="language.variables.variable"> <title>Variable variables</title> <simpara> Sometimes it is convenient to be able to have variable variable names. That is, a variable name which can be set and used dynamically. A normal variable is set with a statement such as: </simpara> <informalexample> <programlisting role="php"> <![CDATA[ <?php $a = "hello"; ?> ]]> </programlisting> </informalexample> <simpara> A variable variable takes the value of a variable and treats that as the name of a variable. In the above example, <emphasis>hello</emphasis>, can be used as the name of a variable by using two dollar signs. i.e. </simpara> <informalexample> <programlisting role="php"> <![CDATA[ <?php $$a = "world"; ?> ]]> </programlisting> </informalexample> <simpara> At this point two variables have been defined and stored in the PHP symbol tree: <varname>$a</varname> with contents "hello" and <varname>$hello</varname> with contents "world". Therefore, this statement: </simpara> <informalexample> <programlisting role="php"> <![CDATA[ <?php echo "$a ${$a}"; ?> ]]> </programlisting> </informalexample> <simpara> produces the exact same output as: </simpara> <informalexample> <programlisting role="php"> <![CDATA[ <?php echo "$a $hello"; ?> ]]> </programlisting> </informalexample> <simpara> i.e. they both produce: <computeroutput>hello world</computeroutput>. </simpara> <simpara> In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write <varname>$$a[1]</varname> then the parser needs to know if you meant to use <varname>$a[1]</varname> as a variable, or if you wanted <varname>$$a</varname> as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: <varname>${$a[1]}</varname> for the first case and <varname>${$a}[1]</varname> for the second. </simpara> <warning> <simpara> Please note that variable variables cannot be used with PHP's <link linkend="language.variables.superglobals">Superglobal arrays</link>. This means you cannot do things like <varname>${$_GET}</varname>. If you are looking for a way to handle availability of superglobals and the old <varname>HTTP_*_VARS</varname>, you might want to try <link linkend="language.references">referencing</link> them. </simpara> </warning> </sect1> <sect1 id="language.variables.external"> <title>Variables from outside PHP</title> <sect2 id="language.variables.external.form"> <title>HTML Forms (GET and POST)</title> <simpara> When a form is submitted to a PHP script, the information from that form is automatically made available to the script. There are many ways to access this information, for example: </simpara> <para> <example> <title>A simple HTML form</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> Depending on your particular setup and personal preferences, there are many ways to access data from your HTML forms. Some examples are: </para> <para> <example> <title>Accessing data from a simple POST HTML form</title> <programlisting role="html"> <![CDATA[ <?php // Available since PHP 4.1.0 print $_POST['username']; print $_REQUEST['username']; import_request_variables('p', 'p_'); print $p_username; // Available since PHP 3. As of PHP 5.0.0, these long predefined // variables can be disabled with the register_long_arrays directive. print $HTTP_POST_VARS['username']; // Available if the PHP directive register_globals = on. As of // PHP 4.2.0 the default value of register_globals = off. // Using/relying on this method is not preferred. print $username; ?> ]]> </programlisting> </example> </para> <para> Using a GET form is similar except you'll use the appropriate GET predefined variable instead. GET also applies to the QUERY_STRING (the information after the '?' in an URL). So, for example, <literal>http://www.example.com/test.php?id=3</literal> contains GET data which is accessible with <varname>$_GET['id']</varname>. See also <link linkend="reserved.variables.request">$_REQUEST</link> and <function>import_request_variables</function>. </para> <note> <para> <link linkend="language.variables.superglobals">Superglobal arrays</link>, like <varname>$_POST</varname> and <varname>$_GET</varname>, became available in PHP 4.1.0 </para> </note> <para> As shown, before PHP 4.2.0 the default value for <link linkend="ini.register-globals">register_globals</link> was <emphasis>on</emphasis>. And, in PHP 3 it was always on. The PHP community is encouraging all to not rely on this directive as it's preferred to assume it's <emphasis>off</emphasis> and code accordingly. </para> <note> <para> The <link linkend="ini.magic-quotes-gpc">magic_quotes_gpc</link> configuration directive affects Get, Post and Cookie values. If turned on, value (It's "PHP!") will automagically become (It\'s \"PHP!\"). Escaping is needed for DB insertion. See also <function>addslashes</function>, <function>stripslashes</function> and <link linkend="ini.magic-quotes-sybase">magic_quotes_sybase</link>. </para> </note> <simpara> PHP also understands arrays in the context of form variables (see the <link linkend="faq.html">related faq</link>). You may, for example, group related variables together, or use this feature to retrieve values from a multiple select input. For example, let's post a form to itself and upon submission display the data: </simpara> <para> <example> <title>More complex form variables</title> <programlisting role="php"> <![CDATA[ <?php if (isset($_POST['action']) && $_POST['action'] == 'submitted') { print '<pre>'; print_r($_POST); print '<a href="'. $_SERVER['PHP_SELF'] .'">Please try again</a>'; print '</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äu</option> </select><br> <input type="hidden" name="action" value="submitted"> <input type="submit" name="submit" value="submit me!"> </form> <?php } ?> ]]> </programlisting> </example> </para> <para> In PHP 3, the array form variable usage is limited to single-dimensional arrays. In PHP 4, no such restriction applies. </para> <sect3 id="language.variables.external.form.submit"> <title>IMAGE SUBMIT variable names</title> <simpara> When submitting a form, it is possible to use an image instead of the standard submit button with a tag like: </simpara> <informalexample> <programlisting role="html"> <![CDATA[ <input type="image" src="image.gif" name="sub"> ]]> </programlisting> </informalexample> <simpara> When the user clicks somewhere on the image, the accompanying form will be transmitted to the server with two additional variables, sub_x and sub_y. These contain the coordinates of the user click within the image. The experienced may note that the actual variable names sent by the browser contains a period rather than an underscore, but PHP converts the period to an underscore automatically. </simpara> </sect3> </sect2> <sect2 id="language.variables.external.cookies"> <title>HTTP Cookies</title> <simpara> PHP transparently supports HTTP cookies as defined by <ulink url="&spec.cookies;">Netscape's Spec</ulink>. Cookies are a mechanism for storing data in the remote browser and thus tracking or identifying return users. You can set cookies using the <function>setcookie</function> function. Cookies are part of the HTTP header, so the SetCookie function must be called before any output is sent to the browser. This is the same restriction as for the <function>header</function> function. Cookie data is then available in the appropriate cookie data arrays, such as <varname>$_COOKIE</varname>, <varname>$HTTP_COOKIE_VARS</varname> as well as in <varname>$_REQUEST</varname>. See the <function>setcookie</function> manual page for more details and examples. </simpara> <simpara> If you wish to assign multiple values to a single cookie variable, you may assign it as an array. For example: </simpara> <informalexample> <programlisting role="php"> <![CDATA[ <?php setcookie("MyCookie[foo]", "Testing 1", time()+3600); setcookie("MyCookie[bar]", "Testing 2", time()+3600); ?> ]]> </programlisting> </informalexample> <simpara> That will create two separate cookies although MyCookie will now be a single array in your script. If you want to set just one cookie with multiple values, consider using <function>serialize</function> or <function>explode</function> on the value first. </simpara> <simpara> Note that a cookie will replace a previous cookie by the same name in your browser unless the path or domain is different. So, for a shopping cart application you may want to keep a counter and pass this along. i.e. </simpara> <example> <title>A <function>setcookie</function> example</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 id="language.variables.external.dot-in-names"> <title>Dots in incoming variable names</title> <para> Typically, PHP does not alter the names of variables when they are passed into a script. However, it should be noted that the dot (period, full stop) is not a valid character in a PHP variable name. For the reason, look at it: <programlisting role="php"> <![CDATA[ <?php $varname.ext; /* invalid variable name */ ?> ]]> </programlisting> Now, what the parser sees is a variable named <varname>$varname</varname>, followed by the string concatenation operator, followed by the barestring (i.e. unquoted string which doesn't match any known key or reserved words) 'ext'. Obviously, this doesn't have the intended result. </para> <para> For this reason, it is important to note that PHP will automatically replace any dots in incoming variable names with underscores. </para> </sect2> <sect2 id="language.variables.determining-type-of"> <title>Determining variable types</title> <para> Because PHP determines the types of variables and converts them (generally) as needed, it is not always obvious what type a given variable is at any one time. PHP includes several functions which find out what type a variable is, such as: <function>gettype</function>, <function>is_array</function>, <function>is_float</function>, <function>is_int</function>, <function>is_object</function>, and <function>is_string</function>. See also the chapter on <link linkend="language.types">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:"../../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 -->
« previous php.doc.ar (#578) next »