1212 </simpara >
1313
1414 <para >
15- Variable names follow the same rules as other labels in PHP. A
16- valid variable name starts with a letter or underscore, followed
15+ A valid variable name starts with a letter
16+ (<literal >A-Z</literal >, <literal >a-z</literal >, or the bytes from 128 through 255)
17+ or underscore, followed
1718 by any number of letters, numbers, or underscores. As a regular
1819 expression, it would be expressed thus:
1920 <code >^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$</code >
2021 </para >
21-
22+
2223 <note >
2324 <simpara >
24- For our purposes here, a letter is a-z, A-Z, and the bytes
25- from 128 through 255 (<literal >0x80-0xff</literal >).
25+ PHP doesn't support Unicode variable names, however, some character
26+ encodings (such as UTF-8) encode characters in such a way that all bytes
27+ of a multi-byte character fall within the allowed range, thus making it a
28+ valid variable name.
2629 </simpara >
2730 </note >
2831
3841
3942 &tip.userlandnaming;
4043
41- <para >
42- For information on variable related functions, see the
43- <link linkend =" ref.var" >Variable Functions Reference</link >.
44- </para >
45-
46- <para >
47- <informalexample >
48- <programlisting role =" php" >
44+ <example >
45+ <title >Valid and invalid variable names</title >
46+ <programlisting role =" php" >
4947<![CDATA[
5048<?php
5149$var = 'Bob';
@@ -57,9 +55,35 @@ $_4site = 'not yet'; // valid; starts with an underscore
5755$täyte = 'mansikka'; // valid; 'ä' is (Extended) ASCII 228.
5856?>
5957]]>
60- </programlisting >
61- </informalexample >
62- </para >
58+ </programlisting >
59+ </example >
60+
61+ <simpara >
62+ PHP accepts a sequence of any bytes as a variable name. Variable
63+ names that do not follow the above-mentioned naming rules can only be
64+ accessed dynamically at runtime. See
65+ <link linkend =" language.variables.variable" >variable variables</link >
66+ for information on how to access them.
67+ </simpara >
68+
69+ <example >
70+ <title >Accessing obscure variable names</title >
71+ <programlisting role =" php" >
72+ <![CDATA[
73+ <?php
74+ ${'invalid-name'} = 'bar';
75+ $name = 'invalid-name';
76+ echo ${'invalid-name'}, " ", $$name;
77+ ?>
78+ ]]>
79+ </programlisting >
80+ &example.outputs;
81+ <screen >
82+ <![CDATA[
83+ bar bar
84+ ]]>
85+ </screen >
86+ </example >
6387
6488 <para >
6589 By default, variables are always assigned by value. That is to say,
@@ -100,7 +124,7 @@ echo $foo; // $foo is altered too.
100124 </para >
101125
102126 <para >
103- One important thing to note is that only named variables may be
127+ One important thing to note is that only variables may be
104128 assigned by reference.
105129 <informalexample >
106130 <programlisting role =" php" >
@@ -115,66 +139,71 @@ function test()
115139 return 25;
116140}
117141
118- $bar = &test(); // Invalid.
142+ $bar = &test(); // Invalid because test() doesn't return a variable by reference .
119143?>
120144]]>
121145 </programlisting >
122146 </informalexample >
123147 </para >
124148
125149 <para >
126- It is not necessary to initialize variables in PHP however it is a very
127- good practice. Uninitialized variables have a default value of their type depending on the context in which they are used
128- - booleans default to &false; , integers and floats default to zero, strings (e.g. used in <function >echo</function >) are
129- set as an empty string and arrays become to an empty array.
150+ It is not necessary to declare variables in PHP, however, it is a very
151+ good practice. Accessing an undefined variable will result in an
152+ <constant >E_WARNING</constant > (prior to PHP 8.0.0, <constant >E_NOTICE</constant >).
153+ An undefined variable has a default value of &null; .
154+ The <function >isset</function > language
155+ construct can be used to detect if a variable has already been initialized.
130156 </para >
131157 <para >
132158 <example >
133- <title >Default values of uninitialized variables </title >
159+ <title >Default value of an uninitialized variable </title >
134160 <programlisting role =" php" >
135161<![CDATA[
136162<?php
137- // Unset AND unreferenced (no use context) variable; outputs NULL
163+ // Unset AND unreferenced (no use context) variable.
138164var_dump($unset_var);
139-
140- // Boolean usage; outputs 'false' (See ternary operators for more on this syntax)
141- echo $unset_bool ? "true\n" : "false\n";
142-
143- // String usage; outputs 'string(3) "abc"'
144- $unset_str .= 'abc';
145- var_dump($unset_str);
146-
147- // Integer usage; outputs 'int(25)'
148- $unset_int += 25; // 0 + 25 => 25
149- var_dump($unset_int);
150-
151- // Float usage; outputs 'float(1.25)'
152- $unset_float += 1.25;
153- var_dump($unset_float);
154-
155- // Array usage; outputs array(1) { [3]=> string(3) "def" }
156- $unset_arr[3] = "def"; // array() + array(3 => "def") => array(3 => "def")
157- var_dump($unset_arr);
158-
159- // Object usage; creates new stdClass object (see http://www.php.net/manual/en/reserved.classes.php)
160- // Outputs: object(stdClass)#1 (1) { ["foo"]=> string(3) "bar" }
161- $unset_obj->foo = 'bar';
162- var_dump($unset_obj);
163165?>
164166]]>
165167 </programlisting >
168+ &example.outputs;
169+ <screen >
170+ <![CDATA[
171+ Warning: Undefined variable $unset_var in ...
172+ NULL
173+ ]]>
174+ </screen >
166175 </example >
167176 </para >
168- <para >
169- Relying on the default value of an uninitialized variable is problematic
170- in the case of including one file into another which uses the same
171- variable name.
172- <constant >E_WARNING</constant > (prior to PHP 8.0.0, <constant >E_NOTICE</constant >)
173- level error is issued in case of
174- working with uninitialized variables, however not in the case of appending
175- elements to the uninitialized array. <function >isset</function > language
176- construct can be used to detect if a variable has been already initialized.
177- </para >
177+
178+ <simpara >
179+ PHP allows array autovivification (automatic creation of new arrays)
180+ from an undefined variable.
181+ Appending an element to an undefined variable will create a new array and
182+ will not generate a warning.
183+ </simpara >
184+ <example >
185+ <title >Autovivification of an array from an undefined variable</title >
186+ <programlisting role =" php" >
187+ <![CDATA[
188+ <?php
189+ $unset_array[] = 'value'; // Does not generate a warning.
190+ ?>
191+ ]]>
192+ </programlisting >
193+ </example >
194+
195+ <warning >
196+ <simpara >
197+ Relying on the default value of an uninitialized variable is problematic
198+ when including one file in another which uses the same
199+ variable name.
200+ </simpara >
201+ </warning >
202+
203+ <simpara >
204+ For information on variable-related functions, see the
205+ <link linkend =" ref.var" >Variable Functions Reference</link >.
206+ </simpara >
178207 </sect1 >
179208
180209 <sect1 xml : id =" language.variables.predefined" >
0 commit comments