language/variables.xml
22583751fbfdaa3eaa41aeb6470d1343f5cb2c78
22583751fbfdaa3eaa41aeb6470d1343f5cb2c78
...
...
@@ -12,8 +12,9 @@
12
12
</simpara>
13
13
14
14
<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
17
18
by any number of letters, numbers, or underscores. As a regular
18
19
expression, it would be expressed thus:
19
20
<code>^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$</code>
...
...
@@ -21,8 +22,10 @@
21
22
22
23
<note>
23
24
<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.
26
29
</simpara>
27
30
</note>
28
31
...
...
@@ -38,32 +41,62 @@
38
41
39
42
&tip.userlandnaming;
40
43
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 variable names</title>
46
+
<programlisting role="php">
49
47
<![CDATA[
50
48
<?php
51
49
$var = 'Bob';
52
50
$Var = 'Joe';
53
51
echo "$var, $Var"; // outputs "Bob, Joe"
54
52
55
-
$4site = 'not yet'; // invalid; starts with a number
56
53
$_4site = 'not yet'; // valid; starts with an underscore
57
54
$täyte = 'mansikka'; // valid; 'ä' is (Extended) ASCII 228.
58
55
?>
59
56
]]>
60
-
</programlisting>
61
-
</informalexample>
62
-
</para>
57
+
</programlisting>
58
+
</example>
59
+
60
+
<example>
61
+
<title>Invalid variable names</title>
62
+
<programlisting role="php">
63
+
<![CDATA[
64
+
<?php
65
+
$4site = 'not yet'; // invalid; starts with a number
66
+
]]>
67
+
</programlisting>
68
+
</example>
69
+
70
+
<simpara>
71
+
PHP accepts a sequence of any bytes as a variable name. Variable
72
+
names that do not follow the above-mentioned naming rules can only be
73
+
accessed dynamically at runtime. See
74
+
<link linkend="language.variables.variable">variable variables</link>
75
+
for information on how to access them.
76
+
</simpara>
77
+
78
+
<example>
79
+
<title>Accessing obscure variable names</title>
80
+
<programlisting role="php">
81
+
<![CDATA[
82
+
<?php
83
+
${'invalid-name'} = 'bar';
84
+
$name = 'invalid-name';
85
+
echo ${'invalid-name'}, " ", $$name;
86
+
?>
87
+
]]>
88
+
</programlisting>
89
+
&example.outputs;
90
+
<screen>
91
+
<![CDATA[
92
+
bar bar
93
+
]]>
94
+
</screen>
95
+
</example>
63
96
64
97
<para>
65
98
By default, variables are always assigned by value. That is to say,
66
-
when you assign an expression to a variable, the entire value of
99
+
when an expression is assigned to a variable, the entire value of
67
100
the original expression is copied into the destination
68
101
variable. This means, for instance, that after assigning one
69
102
variable's value to another, changing one of those variables will
...
...
@@ -100,7 +133,7 @@ echo $foo; // $foo is altered too.
100
133
</para>
101
134
102
135
<para>
103
-
One important thing to note is that only named variables may be
136
+
One important thing to note is that only variables may be
104
137
assigned by reference.
105
138
<informalexample>
106
139
<programlisting role="php">
...
...
@@ -115,7 +148,7 @@ function test()
115
148
return 25;
116
149
}
117
150
118
-
$bar = &test(); // Invalid.
151
+
$bar = &test(); // Invalid because test() doesn't return a variable by reference.
119
152
?>
120
153
]]>
121
154
</programlisting>
...
...
@@ -123,79 +156,80 @@ $bar = &test(); // Invalid.
123
156
</para>
124
157
125
158
<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.
159
+
It is not necessary to declare variables in PHP, however, it is a very
160
+
good practice. Accessing an undefined variable will result in an
161
+
<constant>E_WARNING</constant> (prior to PHP 8.0.0, <constant>E_NOTICE</constant>).
162
+
An undefined variable has a default value of &null;.
163
+
The <function>isset</function> language
164
+
construct can be used to detect if a variable has already been initialized.
130
165
</para>
131
166
<para>
132
167
<example>
133
-
<title>Default values of uninitialized variables</title>
168
+
<title>Default value of an uninitialized variable</title>
134
169
<programlisting role="php">
135
170
<![CDATA[
136
171
<?php
137
-
// Unset AND unreferenced (no use context) variable; outputs NULL
172
+
// Unset AND unreferenced (no use context) variable.
138
173
var_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/double 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);
163
174
?>
164
175
]]>
165
176
</programlisting>
177
+
&example.outputs;
178
+
<screen>
179
+
<![CDATA[
180
+
Warning: Undefined variable $unset_var in ...
181
+
NULL
182
+
]]>
183
+
</screen>
166
184
</example>
167
185
</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
-
<link linkend="errorfunc.constants.errorlevels.e-notice">E_NOTICE</link> level error is issued in case of
173
-
working with uninitialized variables, however not in the case of appending
174
-
elements to the uninitialized array. <function>isset</function> language
175
-
construct can be used to detect if a variable has been already initialized.
176
-
</para>
186
+
187
+
<simpara>
188
+
PHP allows array autovivification (automatic creation of new arrays)
189
+
from an undefined variable.
190
+
Appending an element to an undefined variable will create a new array and
191
+
will not generate a warning.
192
+
</simpara>
193
+
<example>
194
+
<title>Autovivification of an array from an undefined variable</title>
195
+
<programlisting role="php">
196
+
<![CDATA[
197
+
<?php
198
+
$unset_array[] = 'value'; // Does not generate a warning.
199
+
?>
200
+
]]>
201
+
</programlisting>
202
+
</example>
203
+
204
+
<warning>
205
+
<simpara>
206
+
Relying on the default value of an uninitialized variable is problematic
207
+
when including one file in another which uses the same
208
+
variable name.
209
+
</simpara>
210
+
</warning>
211
+
212
+
<simpara>
213
+
A variable can be destroyed by using the <function>unset</function>
214
+
language construct.
215
+
</simpara>
216
+
217
+
<simpara>
218
+
For information on variable-related functions, see the
219
+
<link linkend="ref.var">Variable Functions Reference</link>.
220
+
</simpara>
177
221
</sect1>
178
222
179
223
<sect1 xml:id="language.variables.predefined">
180
224
<title>Predefined Variables</title>
181
225
182
226
<para>
183
-
PHP provides a large number of predefined variables to any script
184
-
which it runs. Many of these variables, however, cannot be fully
185
-
documented as they are dependent upon which server is running, the
186
-
version and setup of the server, and other factors. Some of these
187
-
variables will not be available when PHP is run on the
188
-
<link linkend="features.commandline">command line</link>.
189
-
Refer to the <link linkend="reserved.variables">list of predefined variables</link>
190
-
for details.
191
-
</para>
192
-
193
-
<para>
227
+
PHP provides a number of
228
+
<link linkend="reserved.variables">predefined variables</link>.
194
229
PHP also provides an additional set of predefined arrays
195
230
containing variables from the web server (if applicable), the
196
-
environment, and user input. These arrays are rather special
197
-
in that they are automatically global - i.e., automatically
198
-
available in every scope. For this reason, they are often known as
231
+
environment, and user input. These arrays are automatically available in
232
+
every scope. For this reason, they are often known as
199
233
"superglobals". (There is no mechanism in PHP for
200
234
user-defined superglobals.) Refer to the
201
235
<link linkend="language.variables.superglobals">list of superglobals</link>
...
...
@@ -224,54 +258,61 @@ var_dump($unset_obj);
224
258
225
259
<simpara>
226
260
The scope of a variable is the context within which it is defined.
227
-
For the most part all PHP variables only have a single scope.
228
-
This single scope spans included and required files as well. For
229
-
example:
261
+
PHP has a function scope and a global scope.
262
+
Any variable defined outside a function is limited to the global scope.
263
+
When a file is included, the code it contains inherits the variable scope
264
+
of the line on which the include occurs.
230
265
</simpara>
231
-
<informalexample>
266
+
<example>
267
+
<title>Example of global variable scope</title>
232
268
<programlisting role="php">
233
269
<![CDATA[
234
270
<?php
235
271
$a = 1;
236
-
include 'b.inc';
272
+
include 'b.inc'; // Variable $a will be available within b.inc
237
273
?>
238
274
]]>
239
275
</programlisting>
240
-
</informalexample>
276
+
</example>
277
+
241
278
<simpara>
242
-
Here the <varname>$a</varname> variable will be available within
243
-
the included <filename>b.inc</filename> script. However, within
244
-
user-defined functions a local function scope is introduced. Any
245
-
variable used inside a function is by default limited to the local
246
-
function scope. For example:
279
+
Any variable created inside a named function or an
280
+
<link linkend="functions.anonymous">anonymous</link> function
281
+
is limited to the scope of the function body.
282
+
However, <link linkend="functions.arrow">arrow functions</link>
283
+
bind variables from the parent scope to make them available inside the body.
284
+
If a file include occurs inside a function within
285
+
the calling file, the variables contained in the called file will be
286
+
available as if they had been defined inside the calling function.
247
287
</simpara>
248
288
249
-
<informalexample>
289
+
<example>
290
+
<title>Example of local variable scope</title>
250
291
<programlisting role="php">
251
292
<![CDATA[
252
293
<?php
253
-
$a = 1; /* global scope */
294
+
$a = 1; // global scope
254
295
255
296
function test()
256
297
{
257
-
echo $a; /* reference to local scope variable */
298
+
echo $a; // Variable $a is undefined as it refers to a local version of $a
258
299
}
259
-
260
-
test();
261
300
?>
262
301
]]>
263
302
</programlisting>
264
-
</informalexample>
303
+
</example>
265
304
266
305
<simpara>
267
-
This script will not produce any output because the echo statement
306
+
The example above will generate an undefined variable <constant>E_WARNING</constant>
307
+
(or an <constant>E_NOTICE</constant> prior to PHP 8.0.0).
308
+
This is because the echo statement
268
309
refers to a local version of the <varname>$a</varname> variable,
269
-
and it has not been assigned a value within this scope. You may
270
-
notice that this is a little bit different from the C language in
310
+
and it has not been assigned a value within this scope.
311
+
Note that this is a little bit different from the C language in
271
312
that global variables in C are automatically available to
272
313
functions unless specifically overridden by a local definition.
273
314
This can cause some problems in that people may inadvertently
274
-
change a global variable. In PHP global variables must be
315
+
change a global variable. In PHP global variables must be
275
316
declared global inside a function if they are going to be used in
276
317
that function.
277
318
</simpara>
...
...
@@ -279,7 +320,12 @@ test();
279
320
<sect2 xml:id="language.variables.scope.global">
280
321
<title>The <literal>global</literal> keyword</title>
281
322
<simpara>
282
-
First, an example use of <literal>global</literal>:
323
+
The <literal>global</literal> keyword is used to bind a variable
324
+
from a global scope into a local scope. The keyword can be used with
325
+
a list of variables or a single variable. A local variable will be created
326
+
referencing the global variable of the same name. If the global
327
+
variable does not exist, the variable will be created in global scope and
328
+
assigned &null;.
283
329
</simpara>
284
330
<para>
285
331
<example>
...
...
@@ -302,20 +348,26 @@ echo $b;
302
348
?>
303
349
]]>
304
350
</programlisting>
351
+
&example.outputs;
352
+
<screen>
353
+
<![CDATA[
354
+
3
355
+
]]>
356
+
</screen>
305
357
</example>
306
358
</para>
307
359
308
360
<simpara>
309
-
The above script will output <literal>3</literal>. By declaring
361
+
By declaring
310
362
<varname>$a</varname> and <varname>$b</varname> global within the
311
363
function, all references to either variable will refer to the
312
-
global version. There is no limit to the number of global
364
+
global version. There is no limit to the number of global
313
365
variables that can be manipulated by a function.
314
366
</simpara>
315
367
316
368
<simpara>
317
369
A second way to access variables from the global scope is to use
318
-
the special PHP-defined <varname>$GLOBALS</varname> array. The
370
+
the special PHP-defined <varname>$GLOBALS</varname> array. The
319
371
previous example can be rewritten as:
320
372
</simpara>
321
373
<para>
...
...
@@ -365,10 +417,10 @@ function test_superglobal()
365
417
</example>
366
418
</para>
367
419
<note>
368
-
<para>
420
+
<simpara>
369
421
Using <literal>global</literal> keyword outside a function is not an
370
422
error. It can be used if the file is included from inside a function.
371
-
</para>
423
+
</simpara>
372
424
</note>
373
425
</sect2>
374
426
...
...
@@ -376,9 +428,9 @@ function test_superglobal()
376
428
<title>Using <literal>static</literal> variables</title>
377
429
<simpara>
378
430
Another important feature of variable scoping is the
379
-
<emphasis>static</emphasis> variable. A static variable exists
431
+
<emphasis>static</emphasis> variable. A static variable exists
380
432
only in a local function scope, but it does not lose its value
381
-
when program execution leaves this scope. Consider the following
433
+
when program execution leaves this scope. Consider the following
382
434
example:
383
435
</simpara>
384
436
<para>
...
...
@@ -401,9 +453,9 @@ function test()
401
453
<simpara>
402
454
This function is quite useless since every time it is called it
403
455
sets <varname>$a</varname> to <literal>0</literal> and prints
404
-
<literal>0</literal>. The <varname>$a</varname>++ which increments the
456
+
<literal>0</literal>. The <varname>$a</varname>++ which increments the
405
457
variable serves no purpose since as soon as the function exits the
406
-
<varname>$a</varname> variable disappears. To make a useful
458
+
<varname>$a</varname> variable disappears. To make a useful
407
459
counting function which will not lose track of the current count,
408
460
the <varname>$a</varname> variable is declared static:
409
461
</simpara>
...
...
@@ -432,10 +484,7 @@ function test()
432
484
433
485
<simpara>
434
486
Static variables also provide one way to deal with recursive
435
-
functions. A recursive function is one which calls itself. Care
436
-
must be taken when writing a recursive function because it is
437
-
possible to make it recurse indefinitely. You must make sure you
438
-
have an adequate way of terminating the recursion. The following
487
+
functions. The following
439
488
simple function recursively counts to 10, using the static
440
489
variable <varname>$count</varname> to know when to stop:
441
490
</simpara>
...
...
@@ -463,9 +512,9 @@ function test()
463
512
</para>
464
513
465
514
<para>
466
-
Static variables can be assigned values which are the
467
-
result of constant expressions, but dynamic expressions, such as function
468
-
calls, will cause a parse error.
515
+
Prior to PHP 8.3.0, static variables could only be initialized using
516
+
a constant expression. As of PHP 8.3.0, dynamic expressions
517
+
(e.g. function calls) are also allowed:
469
518
</para>
470
519
<para>
471
520
<example>
...
...
@@ -476,7 +525,7 @@ function test()
476
525
function foo(){
477
526
static $int = 0; // correct
478
527
static $int = 1+2; // correct
479
-
static $int = sqrt(121); // wrong (as it is a function)
528
+
static $int = sqrt(121); // correct as of PHP 8.3.0
480
529
481
530
$int++;
482
531
echo $int;
...
...
@@ -487,12 +536,46 @@ function foo(){
487
536
</example>
488
537
</para>
489
538
539
+
<simpara>
540
+
Static variables inside anonymous functions persist only within that
541
+
specific function instance. If the anonymous function is recreated on each
542
+
call, the static variable will be reinitialized.
543
+
</simpara>
544
+
<example>
545
+
<title>Static variables in anonymous functions</title>
546
+
<programlisting role="php">
547
+
<![CDATA[
548
+
<?php
549
+
function exampleFunction($input) {
550
+
$result = (static function () use ($input) {
551
+
static $counter = 0;
552
+
$counter++;
553
+
return "Input: $input, Counter: $counter\n";
554
+
});
555
+
556
+
return $result();
557
+
}
558
+
559
+
// Calls to exampleFunction will recreate the anonymous function, so the static
560
+
// variable does not retain its value.
561
+
echo exampleFunction('A'); // Outputs: Input: A, Counter: 1
562
+
echo exampleFunction('B'); // Outputs: Input: B, Counter: 1
563
+
?>
564
+
]]>
565
+
</programlisting>
566
+
</example>
567
+
490
568
<para>
491
569
As of PHP 8.1.0, when a method using static variables is inherited (but not overridden),
492
570
the inherited method will now share static variables with the parent method.
493
571
This means that static variables in methods now behave the same way as static properties.
494
572
</para>
495
573
574
+
<simpara>
575
+
As of PHP 8.3.0, static variables can be initialized with arbitrary expressions.
576
+
This means that method calls, for example, can be used to initialize static variables.
577
+
</simpara>
578
+
496
579
<example>
497
580
<title>Usage of static Variables in Inherited Methods</title>
498
581
<programlisting role="php">
...
...
@@ -514,12 +597,6 @@ var_dump(Bar::counter()); // int(4), prior to PHP 8.1.0 int(2)
514
597
]]>
515
598
</programlisting>
516
599
</example>
517
-
518
-
<note>
519
-
<para>
520
-
Static declarations are resolved in compile-time.
521
-
</para>
522
-
</note>
523
600
</sect2>
524
601
525
602
<sect2 xml:id="language.variables.scope.references">
...
...
@@ -541,13 +618,13 @@ var_dump(Bar::counter()); // int(4), prior to PHP 8.1.0 int(2)
541
618
<?php
542
619
function test_global_ref() {
543
620
global $obj;
544
-
$new = new stdclass;
621
+
$new = new stdClass;
545
622
$obj = &$new;
546
623
}
547
624
548
625
function test_global_noref() {
549
626
global $obj;
550
-
$new = new stdclass;
627
+
$new = new stdClass;
551
628
$obj = $new;
552
629
}
553
630
...
...
@@ -585,7 +662,7 @@ function &get_instance_ref() {
585
662
echo 'Static object: ';
586
663
var_dump($obj);
587
664
if (!isset($obj)) {
588
-
$new = new stdclass;
665
+
$new = new stdClass;
589
666
// Assign a reference to the static variable
590
667
$obj = &$new;
591
668
}
...
...
@@ -603,7 +680,7 @@ function &get_instance_noref() {
603
680
echo 'Static object: ';
604
681
var_dump($obj);
605
682
if (!isset($obj)) {
606
-
$new = new stdclass;
683
+
$new = new stdClass;
607
684
// Assign the object to the static variable
608
685
$obj = $new;
609
686
}
...
...
@@ -640,8 +717,8 @@ Static object: object(stdClass)#3 (1) {
640
717
641
718
<simpara>
642
719
This example demonstrates that when assigning a reference to a static
643
-
variable, it's not <emphasis>remembered</emphasis> when you call the
644
-
<literal>&get_instance_ref()</literal> function a second time.
720
+
variable, it is not <emphasis>remembered</emphasis> when the
721
+
<literal>&get_instance_ref()</literal> function is called a second time.
645
722
</simpara>
646
723
</sect2>
647
724
</sect1>
...
...
@@ -651,8 +728,8 @@ Static object: object(stdClass)#3 (1) {
651
728
652
729
<simpara>
653
730
Sometimes it is convenient to be able to have variable variable
654
-
names. That is, a variable name which can be set and used
655
-
dynamically. A normal variable is set with a statement such as:
731
+
names. That is, a variable name which can be set and used
732
+
dynamically. A normal variable is set with a statement such as:
656
733
</simpara>
657
734
658
735
<informalexample>
...
...
@@ -667,7 +744,7 @@ $a = 'hello';
667
744
668
745
<simpara>
669
746
A variable variable takes the value of a variable and treats that
670
-
as the name of a variable. In the above example,
747
+
as the name of a variable. In the above example,
671
748
<emphasis>hello</emphasis>, can be used as the name of a variable
672
749
by using two dollar signs. i.e.
673
750
</simpara>
...
...
@@ -685,7 +762,7 @@ $$a = 'world';
685
762
<simpara>
686
763
At this point two variables have been defined and stored in the
687
764
PHP symbol tree: <varname>$a</varname> with contents "hello" and
688
-
<varname>$hello</varname> with contents "world". Therefore, this
765
+
<varname>$hello</varname> with contents "world". Therefore, this
689
766
statement:
690
767
</simpara>
691
768
...
...
@@ -693,7 +770,7 @@ $$a = 'world';
693
770
<programlisting role="php">
694
771
<![CDATA[
695
772
<?php
696
-
echo "$a ${$a}";
773
+
echo "$a {$$a}";
697
774
?>
698
775
]]>
699
776
</programlisting>
...
...
@@ -718,12 +795,12 @@ echo "$a $hello";
718
795
</simpara>
719
796
720
797
<simpara>
721
-
In order to use variable variables with arrays, you have to
722
-
resolve an ambiguity problem. That is, if you write
723
-
<varname>$$a[1]</varname> then the parser needs to know if you
724
-
meant to use <varname>$a[1]</varname> as a variable, or if you
725
-
wanted <varname>$$a</varname> as the variable and then the [1]
726
-
index from that variable. The syntax for resolving this ambiguity
798
+
In order to use variable variables with arrays,
799
+
an ambiguity problem has to be resolved. That is, if the parser sees
800
+
<varname>$$a[1]</varname> then it needs to know if
801
+
<varname>$a[1]</varname> was meant to be used as a variable, or if
802
+
<varname>$$a</varname> was wanted as the variable and then the <literal>[1]</literal>
803
+
index from that variable. The syntax for resolving this ambiguity
727
804
is: <varname>${$a[1]}</varname> for the first case and
728
805
<varname>${$a}[1]</varname> for the second.
729
806
</simpara>
...
...
@@ -731,7 +808,7 @@ echo "$a $hello";
731
808
<simpara>
732
809
Class properties may also be accessed using variable property names. The
733
810
variable property name will be resolved within the scope from which the
734
-
call is made. For instance, if you have an expression such as
811
+
call is made. For instance, if there is an expression such as
735
812
<varname>$foo->$bar</varname>, then the local scope will be examined for
736
813
<varname>$bar</varname> and its value will be used as the name of the
737
814
property of <varname>$foo</varname>. This is also true if
...
...
@@ -739,7 +816,7 @@ echo "$a $hello";
739
816
</simpara>
740
817
741
818
<simpara>
742
-
Curly braces may also be used, to clearly delimit the property
819
+
Curly braces may also be used to clearly delimit the property
743
820
name. They are most useful when accessing values within a property that
744
821
contains an array, when the property name is made of multiple parts,
745
822
or when the property name contains characters that are not
...
...
@@ -753,15 +830,15 @@ echo "$a $hello";
753
830
<programlisting role="php">
754
831
<![CDATA[
755
832
<?php
756
-
class foo {
757
-
var $bar = 'I am bar.';
758
-
var $arr = array('I am A.', 'I am B.', 'I am C.');
759
-
var $r = 'I am r.';
833
+
class Foo {
834
+
public $bar = 'I am bar.';
835
+
public $arr = ['I am A.', 'I am B.', 'I am C.'];
836
+
public $r = 'I am r.';
760
837
}
761
838
762
-
$foo = new foo();
839
+
$foo = new Foo();
763
840
$bar = 'bar';
764
-
$baz = array('foo', 'bar', 'baz', 'quux');
841
+
$baz = ['foo', 'bar', 'baz', 'quux'];
765
842
echo $foo->$bar . "\n";
766
843
echo $foo->{$baz[1]} . "\n";
767
844
...
...
@@ -771,16 +848,20 @@ echo $foo->{$start . $end} . "\n";
771
848
772
849
$arr = 'arr';
773
850
echo $foo->{$arr[1]} . "\n";
851
+
echo $foo->{$arr}[1] . "\n";
774
852
775
853
?>
776
854
]]>
777
855
</programlisting>
778
856
&example.outputs;
779
857
<screen>
858
+
<![CDATA[
780
859
I am bar.
781
860
I am bar.
782
861
I am bar.
783
862
I am r.
863
+
I am B.
864
+
]]>
784
865
</screen>
785
866
</example>
786
867
</para>
...
...
@@ -824,7 +905,7 @@ I am r.
824
905
</para>
825
906
826
907
<para>
827
-
There are only two ways to access data from your HTML forms.
908
+
There are only two ways to access data from HTML forms.
828
909
Currently available methods are listed below:
829
910
</para>
830
911
...
...
@@ -843,9 +924,9 @@ echo $_REQUEST['username'];
843
924
</para>
844
925
845
926
<para>
846
-
Using a GET form is similar except you'll use the appropriate
847
-
GET predefined variable instead. GET also applies to the
848
-
<literal>QUERY_STRING</literal> (the information after the '?' in a URL). So,
927
+
Using a GET form is similar except the appropriate
928
+
GET predefined variable can be used instead. GET also applies to the
929
+
<literal>QUERY_STRING</literal> (the information after the '?' in a URL). So,
849
930
for example, <literal>http://www.example.com/test.php?id=3</literal>
850
931
contains GET data which is accessible with <varname>$_GET['id']</varname>.
851
932
See also <varname>$_REQUEST</varname>.
...
...
@@ -861,9 +942,9 @@ echo $_REQUEST['username'];
861
942
862
943
<simpara>
863
944
PHP also understands arrays in the context of form variables
864
-
(see the <link linkend="faq.html">related faq</link>). You may,
865
-
for example, group related variables together, or use this
866
-
feature to retrieve values from a multiple select input. For
945
+
(see the <link linkend="faq.html">related faq</link>).
946
+
For example, related variables may be grouped together, or this
947
+
feature may be used to retrieve values from a multiple select input. For
867
948
example, let's post a form to itself and upon submission display
868
949
the data:
869
950
</simpara>
...
...
@@ -925,7 +1006,7 @@ if ($_POST) {
925
1006
form will be transmitted to the server with two additional
926
1007
variables, <varname>sub_x</varname> and <varname>sub_y</varname>.
927
1008
These contain the coordinates of the
928
-
user click within the image. The experienced may note that the
1009
+
user click within the image. The experienced may note that the
929
1010
actual variable names sent by the browser contains a period
930
1011
rather than an underscore, but PHP converts the period to an
931
1012
underscore automatically.
...
...
@@ -939,13 +1020,13 @@ if ($_POST) {
939
1020
940
1021
<simpara>
941
1022
PHP transparently supports HTTP cookies as defined by <link
942
-
xlink:href="&url.rfc;6265">RFC 6265</link>. Cookies are a
1023
+
xlink:href="&url.rfc;6265">RFC 6265</link>. Cookies are a
943
1024
mechanism for storing data in the remote browser and thus
944
-
tracking or identifying return users. You can set cookies using
945
-
the <function>setcookie</function> function. Cookies are part of
1025
+
tracking or identifying return users. It is possible to set cookies using
1026
+
the <function>setcookie</function> function. Cookies are part of
946
1027
the HTTP header, so the SetCookie function must be called before
947
-
any output is sent to the browser. This is the same restriction
948
-
as for the <function>header</function> function. Cookie data
1028
+
any output is sent to the browser. This is the same restriction
1029
+
as for the <function>header</function> function. Cookie data
949
1030
is then available in the appropriate cookie data arrays, such
950
1031
as <varname>$_COOKIE</varname> as well as in <varname>$_REQUEST</varname>.
951
1032
See the <function>setcookie</function> manual page for more details and
...
...
@@ -960,8 +1041,8 @@ if ($_POST) {
960
1041
</note>
961
1042
962
1043
<simpara>
963
-
If you wish to assign multiple values to a single cookie variable, you
964
-
may assign it as an array. For example:
1044
+
If multiple values should be assigned to a single cookie variable,
1045
+
they can be assigned as an array. For example:
965
1046
</simpara>
966
1047
967
1048
<informalexample>
...
...
@@ -977,16 +1058,16 @@ if ($_POST) {
977
1058
978
1059
<simpara>
979
1060
That will create two separate cookies although <varname>MyCookie</varname> will now
980
-
be a single array in your script. If you want to set just one cookie
1061
+
be a single array in the script. If just one cookie should be set
981
1062
with multiple values, consider using <function>serialize</function> or
982
1063
<function>explode</function> on the value first.
983
1064
</simpara>
984
1065
985
1066
<simpara>
986
1067
Note that a cookie will replace a previous cookie by the same
987
-
name in your browser unless the path or domain is different. So,
988
-
for a shopping cart application you may want to keep a counter
989
-
and pass this along. i.e.
1068
+
name in the browser unless the path or domain is different. So,
1069
+
for a shopping cart application a counter may be kept,
1070
+
and passed along. I.e.
990
1071
</simpara>
991
1072
992
1073
<example>
...
...
@@ -1044,12 +1125,12 @@ $varname.ext; /* invalid variable name */
1044
1125
<para>
1045
1126
Because PHP determines the types of variables and converts them
1046
1127
(generally) as needed, it is not always obvious what type a given
1047
-
variable is at any one time. PHP includes several functions
1128
+
variable is at any one time. PHP includes several functions
1048
1129
which find out what type a variable is, such as:
1049
1130
<function>gettype</function>, <function>is_array</function>,
1050
1131
<function>is_float</function>, <function>is_int</function>,
1051
1132
<function>is_object</function>, and
1052
-
<function>is_string</function>. See also the chapter on
1133
+
<function>is_string</function>. See also the chapter on
1053
1134
<link linkend="language.types">Types</link>.
1054
1135
</para>
1055
1136
<para>
1056
1137