language/operators.xml
52407313885d27a4e891e08dd2e2481bcc39e244
...
...
@@ -1,2621 +1,49 @@
1
1
<?xml version="1.0" encoding="utf-8"?>
2
2
<!-- $Revision$ -->
3
-
<chapter xml:id="language.operators" xmlns="http://docbook.org/ns/docbook">
4
-
<title>Operators</title>
5
-
<simpara>
6
-
An operator is something that takes one or more values (or
7
-
expressions, in programming jargon) and yields another value (so that the
8
-
construction itself becomes an expression).
9
-
</simpara>
10
-
<para>
11
-
Operators can be grouped according to the number of values they take. Unary
12
-
operators take only one value, for example <literal>!</literal> (the
13
-
<link linkend="language.operators.logical">logical not operator</link>) or
14
-
<literal>++</literal> (the
15
-
<link linkend="language.operators.increment">increment operator</link>).
16
-
Binary operators take two values, such as the familiar
17
-
<link linkend="language.operators.arithmetic">arithmetical operators</link>
18
-
<literal>+</literal> (plus) and <literal>-</literal> (minus), and the
19
-
majority of PHP operators fall into this category. Finally, there is a
20
-
single <link linkend="language.operators.comparison.ternary">ternary
21
-
operator</link>, <literal>? :</literal>, which takes three values; this is
22
-
usually referred to simply as "the ternary operator" (although it could
23
-
perhaps more properly be called the conditional operator).
24
-
</para>
25
-
<para>
26
-
A full list of PHP operators follows in the section
27
-
<link linkend="language.operators.precedence">Operator Precedence</link>.
28
-
The section also explains operator precedence and associativity, which govern
29
-
exactly how expressions containing several different operators are
30
-
evaluated.
31
-
</para>
32
-

33
-
<sect1 xml:id="language.operators.precedence">
34
-
<title>Operator Precedence</title>
35
-
<para>
36
-
The precedence of an operator specifies how "tightly" it binds two
37
-
expressions together. For example, in the expression <literal>1 +
38
-
5 * 3</literal>, the answer is <literal>16</literal> and not
39
-
<literal>18</literal> because the multiplication ("*") operator
40
-
has a higher precedence than the addition ("+") operator.
41
-
Parentheses may be used to force precedence, if necessary. For
42
-
instance: <literal>(1 + 5) * 3</literal> evaluates to
43
-
<literal>18</literal>.
44
-
</para>
45
-
<para>
46
-
When operators have equal precedence their associativity decides
47
-
how the operators are grouped. For example "-" is left-associative, so
48
-
<literal>1 - 2 - 3</literal> is grouped as <literal>(1 - 2) - 3</literal>
49
-
and evaluates to <literal>-4</literal>. "=" on the other hand is
50
-
right-associative, so <literal>$a = $b = $c</literal> is grouped as
51
-
<literal>$a = ($b = $c)</literal>.
52
-
</para>
53
-
<para>
54
-
Operators of equal precedence that are non-associative cannot be used
55
-
next to each other, for example <literal>1 &lt; 2 &gt; 1</literal> is
56
-
illegal in PHP. The expression <literal>1 &lt;= 1 == 1</literal> on the
57
-
other hand is legal, because the <literal>==</literal> operator has lesser
58
-
precedence than the <literal>&lt;=</literal> operator.
59
-
</para>
60
-
<para>
61
-
Use of parentheses, even when not strictly necessary, can often increase
62
-
readability of the code by making grouping explicit rather than relying
63
-
on the implicit operator precedence and associativity.
64
-
</para>
65
-
<para>
66
-
The following table lists the operators in order of precedence, with
67
-
the highest-precedence ones at the top. Operators on the same line
68
-
have equal precedence, in which case associativity decides grouping.
69
-
<table>
70
-
<title>Operator Precedence</title>
71
-
<tgroup cols="2">
72
-
<thead>
73
-
<row>
74
-
<entry>Associativity</entry>
75
-
<entry>Operators</entry>
76
-
<entry>Additional Information</entry>
77
-
</row>
78
-
</thead>
79
-
<tbody>
80
-
<row>
81
-
<entry>(n/a)</entry>
82
-
<entry>
83
-
<literal>clone</literal>
84
-
<literal>new</literal>
85
-
</entry>
86
-
<entry><link linkend="language.oop5.cloning">clone</link> and <link linkend="language.oop5.basic.new">new</link></entry>
87
-
</row>
88
-
<row>
89
-
<entry>right</entry>
90
-
<entry><literal>**</literal></entry>
91
-
<entry><link linkend="language.operators.arithmetic">arithmetic</link></entry>
92
-
</row>
93
-
<row>
94
-
<entry>(n/a)</entry>
95
-
<entry>
96
-
<literal>++</literal>
97
-
<literal>--</literal>
98
-
<literal>~</literal>
99
-
<literal>(int)</literal>
100
-
<literal>(float)</literal>
101
-
<literal>(string)</literal>
102
-
<literal>(array)</literal>
103
-
<literal>(object)</literal>
104
-
<literal>(bool)</literal>
105
-
<literal>@</literal>
106
-
</entry>
107
-
<entry>
108
-
<link linkend="language.types">types</link> and <link linkend="language.operators.increment">increment/decrement</link>
109
-
</entry>
110
-
</row>
111
-
<row>
112
-
<entry>left</entry>
113
-
<entry><literal>instanceof</literal></entry>
114
-
<entry>
115
-
<link linkend="language.types">types</link>
116
-
</entry>
117
-
</row>
118
-
<row>
119
-
<entry>(n/a)</entry>
120
-
<entry><literal>!</literal></entry>
121
-
<entry>
122
-
<link linkend="language.operators.logical">logical</link>
123
-
</entry>
124
-
</row>
125
-
<row>
126
-
<entry>left</entry>
127
-
<entry>
128
-
<literal>*</literal>
129
-
<literal>/</literal>
130
-
<literal>%</literal>
131
-
</entry>
132
-
<entry>
133
-
<link linkend="language.operators.arithmetic">arithmetic</link>
134
-
</entry>
135
-
</row>
136
-
<row>
137
-
<entry>left</entry>
138
-
<entry>
139
-
<literal>+</literal>
140
-
<literal>-</literal>
141
-
<literal>.</literal>
142
-
</entry>
143
-
<entry>
144
-
<link linkend="language.operators.arithmetic">arithmetic</link>&listendand;
145
-
<link linkend="language.operators.string">string</link></entry>
146
-
</row>
147
-
<row>
148
-
<entry>left</entry>
149
-
<entry>
150
-
<literal>&lt;&lt;</literal>
151
-
<literal>&gt;&gt;</literal>
152
-
</entry>
153
-
<entry>
154
-
<link linkend="language.operators.bitwise">bitwise</link>
155
-
</entry>
156
-
</row>
157
-
<row>
158
-
<entry>non-associative</entry>
159
-
<entry>
160
-
<literal>&lt;</literal>
161
-
<literal>&lt;=</literal>
162
-
<literal>&gt;</literal>
163
-
<literal>&gt;=</literal>
164
-
</entry>
165
-
<entry>
166
-
<link linkend="language.operators.comparison">comparison</link>
167
-
</entry>
168
-
</row>
169
-
<row>
170
-
<entry>non-associative</entry>
171
-
<entry>
172
-
<literal>==</literal>
173
-
<literal>!=</literal>
174
-
<literal>===</literal>
175
-
<literal>!==</literal>
176
-
<literal>&lt;&gt;</literal>
177
-
<literal>&lt;=&gt;</literal>
178
-
</entry>
179
-
<entry>
180
-
<link linkend="language.operators.comparison">comparison</link>
181
-
</entry>
182
-
</row>
183
-
<row>
184
-
<entry>left</entry>
185
-
<entry><literal>&amp;</literal></entry>
186
-
<entry>
187
-
<link linkend="language.operators.bitwise">bitwise</link>&listendand;
188
-
<link linkend="language.references">references</link></entry>
189
-
</row>
190
-
<row>
191
-
<entry>left</entry>
192
-
<entry><literal>^</literal></entry>
193
-
<entry>
194
-
<link linkend="language.operators.bitwise">bitwise</link>
195
-
</entry>
196
-
</row>
197
-
<row>
198
-
<entry>left</entry>
199
-
<entry><literal>|</literal></entry>
200
-
<entry>
201
-
<link linkend="language.operators.bitwise">bitwise</link>
202
-
</entry>
203
-
</row>
204
-
<row>
205
-
<entry>left</entry>
206
-
<entry><literal>&amp;&amp;</literal></entry>
207
-
<entry>
208
-
<link linkend="language.operators.logical">logical</link>
209
-
</entry>
210
-
</row>
211
-
<row>
212
-
<entry>left</entry>
213
-
<entry><literal>||</literal></entry>
214
-
<entry>
215
-
<link linkend="language.operators.logical">logical</link>
216
-
</entry>
217
-
</row>
218
-
<row>
219
-
<entry>right</entry>
220
-
<entry><literal>??</literal></entry>
221
-
<entry>
222
-
<link linkend="language.operators.comparison.coalesce">null coalescing</link>
223
-
</entry>
224
-
</row>
225
-
<row>
226
-
<entry>left</entry>
227
-
<entry><literal>? :</literal></entry>
228
-
<entry>
229
-
<link linkend="language.operators.comparison.ternary">ternary</link>
230
-
</entry>
231
-
</row>
232
-
<row>
233
-
<entry>right</entry>
234
-
<entry>
235
-
<literal>=</literal>
236
-
<literal>+=</literal>
237
-
<literal>-=</literal>
238
-
<literal>*=</literal>
239
-
<literal>**=</literal>
240
-
<literal>/=</literal>
241
-
<literal>.=</literal>
242
-
<literal>%=</literal>
243
-
<literal>&amp;=</literal>
244
-
<literal>|=</literal>
245
-
<literal>^=</literal>
246
-
<literal>&lt;&lt;=</literal>
247
-
<literal>&gt;&gt;=</literal>
248
-
<literal>??=</literal>
249
-
</entry>
250
-
<entry>
251
-
<link linkend="language.operators.assignment">assignment</link>
252
-
</entry>
253
-
</row>
254
-
<row>
255
-
<entry>(n/a)</entry>
256
-
<entry><literal>yield from</literal></entry>
257
-
<entry>
258
-
<link linkend="control-structures.yield.from">yield from</link>
259
-
</entry>
260
-
</row>
261
-
<row>
262
-
<entry>(n/a)</entry>
263
-
<entry><literal>yield</literal></entry>
264
-
<entry>
265
-
<link linkend="control-structures.yield">yield</link>
266
-
</entry>
267
-
</row>
268
-
<row>
269
-
<entry>(n/a)</entry>
270
-
<entry><literal>print</literal></entry>
271
-
<entry><function>print</function></entry>
272
-
</row>
273
-
<row>
274
-
<entry>left</entry>
275
-
<entry><literal>and</literal></entry>
276
-
<entry>
277
-
<link linkend="language.operators.logical">logical</link>
278
-
</entry>
279
-
</row>
280
-
<row>
281
-
<entry>left</entry>
282
-
<entry><literal>xor</literal></entry>
283
-
<entry>
284
-
<link linkend="language.operators.logical">logical</link>
285
-
</entry>
286
-
</row>
287
-
<row>
288
-
<entry>left</entry>
289
-
<entry><literal>or</literal></entry>
290
-
<entry>
291
-
<link linkend="language.operators.logical">logical</link>
292
-
</entry>
293
-
</row>
294
-
</tbody>
295
-
</tgroup>
296
-
</table>
297
-
</para>
298
-
<para>
299
-
<example>
300
-
<title>Associativity</title>
301
-
<programlisting role="php">
302
-
<![CDATA[
303
-
<?php
304
-
$a = 3 * 3 % 5; // (3 * 3) % 5 = 4
305
-
// ternary operator associativity differs from C/C++
306
-
$a = true ? 0 : true ? 1 : 2; // (true ? 0 : true) ? 1 : 2 = 2
307
-

308
-
$a = 1;
309
-
$b = 2;
310
-
$a = $b += 3; // $a = ($b += 3) -> $a = 5, $b = 5
311
-
?>
312
-
]]>
313
-
</programlisting>
314
-
</example>
315
-
</para>
316
-
<para>
317
-
Operator precedence and associativity only determine how expressions
318
-
are grouped, they do not specify an order of evaluation. PHP does not
319
-
(in the general case) specify in which order an expression is evaluated
320
-
and code that assumes a specific order of evaluation should be avoided,
321
-
because the behavior can change between versions of PHP or depending on
322
-
the surrounding code.
323
-
<example>
324
-
<title>Undefined order of evaluation</title>
325
-
<programlisting role="php">
326
-
<![CDATA[
327
-
<?php
328
-
$a = 1;
329
-
echo $a + $a++; // may print either 2 or 3
330
-

331
-
$i = 1;
332
-
$array[$i] = $i++; // may set either index 1 or 2
333
-
?>
334
-
]]>
335
-
</programlisting>
336
-
</example>
337
-
<example>
338
-
<title><literal>+</literal>, <literal>-</literal> and <literal>.</literal> have the same precedence</title>
339
-
<programlisting role="php">
340
-
<![CDATA[
341
-
<?php
342
-
$x = 4;
343
-
// this line might result in unexpected output:
344
-
echo "x minus one equals " . $x-1 . ", or so I hope\n";
345
-
// because it is evaluated like this line:
346
-
echo (("x minus one equals " . $x) - 1) . ", or so I hope\n";
347
-
// the desired precedence can be enforced by using parentheses:
348
-
echo "x minus one equals " . ($x-1) . ", or so I hope\n";
349
-
?>
350
-
]]>
351
-
</programlisting>
352
-
&example.outputs;
353
-
<screen>
354
-
<![CDATA[
355
-
-1, or so I hope
356
-
-1, or so I hope
357
-
x minus one equals 3, or so I hope
358
-
]]>
359
-
</screen>
360
-
</example>
361
-
</para>
362
-
<note>
363
-
<para>
364
-
Although <literal>=</literal> has a lower precedence than
365
-
most other operators, PHP will still allow expressions
366
-
similar to the following: <literal>if (!$a = foo())</literal>,
367
-
in which case the return value of <literal>foo()</literal> is
368
-
put into <varname>$a</varname>.
369
-
</para>
370
-
</note>
371
-
</sect1>
372
-

373
-
<sect1 xml:id="language.operators.arithmetic">
374
-
<title>Arithmetic Operators</title>
375
-
<simpara>
376
-
Remember basic arithmetic from school? These work just
377
-
like those.
378
-
</simpara>
379
-
<table>
380
-
<title>Arithmetic Operators</title>
381
-
<tgroup cols="3">
382
-
<thead>
383
-
<row>
384
-
<entry>Example</entry>
385
-
<entry>Name</entry>
386
-
<entry>Result</entry>
387
-
</row>
388
-
</thead>
389
-
<tbody>
390
-
<row>
391
-
<entry>+$a</entry>
392
-
<entry>Identity</entry>
393
-
<entry>
394
-
Conversion of <varname>$a</varname> to <type>int</type> or
395
-
<type>float</type> as appropriate.
396
-
</entry>
397
-
</row>
398
-
<row>
399
-
<entry>-$a</entry>
400
-
<entry>Negation</entry>
401
-
<entry>Opposite of <varname>$a</varname>.</entry>
402
-
</row>
403
-
<row>
404
-
<entry>$a + $b</entry>
405
-
<entry>Addition</entry>
406
-
<entry>Sum of <varname>$a</varname> and <varname>$b</varname>.</entry>
407
-
</row>
408
-
<row>
409
-
<entry>$a - $b</entry>
410
-
<entry>Subtraction</entry>
411
-
<entry>Difference of <varname>$a</varname> and <varname>$b</varname>.</entry>
412
-
</row>
413
-
<row>
414
-
<entry>$a * $b</entry>
415
-
<entry>Multiplication</entry>
416
-
<entry>Product of <varname>$a</varname> and <varname>$b</varname>.</entry>
417
-
</row>
418
-
<row>
419
-
<entry>$a / $b</entry>
420
-
<entry>Division</entry>
421
-
<entry>Quotient of <varname>$a</varname> and <varname>$b</varname>.</entry>
422
-
</row>
423
-
<row>
424
-
<entry>$a % $b</entry>
425
-
<entry>Modulo</entry>
426
-
<entry>Remainder of <varname>$a</varname> divided by <varname>$b</varname>.</entry>
427
-
</row>
428
-
<row>
429
-
<entry>$a ** $b</entry>
430
-
<entry>Exponentiation</entry>
431
-
<entry>Result of raising <varname>$a</varname> to the <varname>$b</varname>'th power.</entry>
432
-
</row>
433
-
</tbody>
434
-
</tgroup>
435
-
</table>
436
-
<simpara>
437
-
The division operator ("/") returns a float value unless the two operands
438
-
are integers (or strings that get converted to integers) and the numbers
439
-
are evenly divisible, in which case an integer value will be returned. For
440
-
integer division, see <function>intdiv</function>.
441
-
</simpara>
442
-
<simpara>
443
-
Operands of modulo are converted to integers (by stripping the decimal
444
-
part) before processing. For floating-point modulo, see
445
-
<function>fmod</function>.
446
-
</simpara>
447
-
<para>
448
-
The result of the modulo operator <literal>%</literal> has the same sign
449
-
as the dividend — that is, the result of <literal>$a % $b</literal>
450
-
will have the same sign as <varname>$a</varname>. For example:
451
-
<informalexample>
452
-
<programlisting role="php">
453
-
<![CDATA[
454
-
<?php
455
-

456
-
echo (5 % 3)."\n"; // prints 2
457
-
echo (5 % -3)."\n"; // prints 2
458
-
echo (-5 % 3)."\n"; // prints -2
459
-
echo (-5 % -3)."\n"; // prints -2
460
-

461
-
?>
462
-
]]>
463
-
</programlisting>
464
-
</informalexample>
465
-
</para>
466
-
<sect2 role="seealso">
467
-
&reftitle.seealso;
468
-
<para>
469
-
<simplelist>
470
-
<member><link linkend="ref.math">Math functions</link></member>
471
-
</simplelist>
472
-
</para>
473
-
</sect2>
474
-
</sect1>
475
-

476
-
<sect1 xml:id="language.operators.assignment">
477
-
<title>Assignment Operators</title>
478
-
<simpara>
479
-
The basic assignment operator is "=". Your first inclination might
480
-
be to think of this as "equal to". Don't. It really means that the
481
-
left operand gets set to the value of the expression on the
482
-
right (that is, "gets set to").
483
-
</simpara>
484
-
<para>
485
-
The value of an assignment expression is the value assigned. That
486
-
is, the value of "<literal>$a = 3</literal>" is 3. This allows you to do some tricky
487
-
things:
488
-
<informalexample>
489
-
<programlisting role="php">
490
-
<![CDATA[
491
-
<?php
492
-

493
-
$a = ($b = 4) + 5; // $a is equal to 9 now, and $b has been set to 4.
494
-

495
-
?>
496
-
]]>
497
-
</programlisting>
498
-
</informalexample>
499
-
</para>
500
-
<para>
501
-
In addition to the basic assignment operator, there are "combined
502
-
operators" for all of the <link linkend="language.operators">binary
503
-
arithmetic</link>, array union and string operators that allow you to use a value in an
504
-
expression and then set its value to the result of that expression. For
505
-
example:
506
-
<informalexample>
507
-
<programlisting role="php">
508
-
<![CDATA[
509
-
<?php
510
-

511
-
$a = 3;
512
-
$a += 5; // sets $a to 8, as if we had said: $a = $a + 5;
513
-
$b = "Hello ";
514
-
$b .= "There!"; // sets $b to "Hello There!", just like $b = $b . "There!";
515
-

516
-
?>
517
-
]]>
518
-
</programlisting>
519
-
</informalexample>
520
-
</para>
521
-
<para>
522
-
Note that the assignment copies the original variable to the new
523
-
one (assignment by value), so changes to one will not affect the
524
-
other. This may also have relevance if you need to copy something
525
-
like a large array inside a tight loop.
526
-
</para>
527
-
<para>
528
-
An exception to the usual assignment by value behaviour within PHP occurs
529
-
with <type>object</type>s, which are assigned by reference.
530
-
Objects may be explicitly copied via the <link
531
-
linkend="language.oop5.cloning">clone</link> keyword.
532
-
</para>
533
-

534
-
<sect2 xml:id="language.operators.assignment.reference">
535
-
<title>Assignment by Reference</title>
536
-
<para>
537
-
Assignment by reference is also supported, using the
538
-
"<computeroutput>$var = &amp;$othervar;</computeroutput>" syntax.
539
-
Assignment by reference means that both variables end up pointing at the
540
-
same data, and nothing is copied anywhere.
541
-
</para>
542
-
<para>
543
-
<example>
544
-
<title>Assigning by reference</title>
545
-
<programlisting role="php">
546
-
<![CDATA[
547
-
<?php
548
-
$a = 3;
549
-
$b = &$a; // $b is a reference to $a
550
-

551
-
print "$a\n"; // prints 3
552
-
print "$b\n"; // prints 3
553
-

554
-
$a = 4; // change $a
555
-

556
-
print "$a\n"; // prints 4
557
-
print "$b\n"; // prints 4 as well, since $b is a reference to $a, which has
558
-
// been changed
559
-
?>
560
-
]]>
561
-
</programlisting>
562
-
</example>
563
-
</para>
564
-
<para>
565
-
The <link linkend="language.oop5.basic.new">new</link>
566
-
operator returns a reference automatically, as such assigning the result of
567
-
<link linkend="language.oop5.basic.new">new</link> by reference is an error.
568
-
</para>
569
-
<para>
570
-
<informalexample>
571
-
<programlisting role="php">
572
-
<![CDATA[
573
-
<?php
574
-
class C {}
575
-

576
-
$o = &new C;
577
-
?>
578
-
]]>
579
-
</programlisting>
580
-
&example.outputs;
581
-
<screen>
582
-
<![CDATA[
583
-
Parse error: syntax error, unexpected 'new' (T_NEW) in …
584
-
]]>
585
-
</screen>
586
-
</informalexample>
587
-
</para>
588
-
<para>
589
-
More information on references and their potential uses can be found in
590
-
the <link linkend="language.references">References Explained</link>
591
-
section of the manual.
592
-
</para>
593
-
</sect2>
594
-

595
-
<sect2 xml:id="language.operators.assignment.arithmetic">
596
-
<title>Arithmetic Assignment Operators</title>
597
-
<informaltable>
598
-
<tgroup cols="3">
599
-
<thead>
600
-
<row>
601
-
<entry>Example</entry>
602
-
<entry>Equivalent</entry>
603
-
<entry>Operation</entry>
604
-
</row>
605
-
</thead>
606
-
<tbody>
607
-
<row>
608
-
<entry>$a += $b</entry>
609
-
<entry>$a = $a + b</entry>
610
-
<entry>Addition</entry>
611
-
</row>
612
-
<row>
613
-
<entry>$a -= $b</entry>
614
-
<entry>$a = $a - $b</entry>
615
-
<entry>Subtraction</entry>
616
-
</row>
617
-
<row>
618
-
<entry>$a *= $b</entry>
619
-
<entry>$a = $a * $b</entry>
620
-
<entry>Multiplication</entry>
621
-
</row>
622
-
<row>
623
-
<entry>$a /= $b</entry>
624
-
<entry>$a = $a / $b</entry>
625
-
<entry>Division</entry>
626
-
</row>
627
-
<row>
628
-
<entry>$a %= $b</entry>
629
-
<entry>$a = $a % $b</entry>
630
-
<entry>Modulus</entry>
631
-
</row>
632
-
</tbody>
633
-
</tgroup>
634
-
</informaltable>
635
-
</sect2>
636
-

637
-
<sect2 xml:id="language.operators.assignment.bitwise">
638
-
<title>Bitwise Assignment Operators</title>
639
-
<informaltable>
640
-
<tgroup cols="3">
641
-
<thead>
642
-
<row>
643
-
<entry>Example</entry>
644
-
<entry>Equivalent</entry>
645
-
<entry>Operation</entry>
646
-
</row>
647
-
</thead>
648
-
<tbody>
649
-
<row>
650
-
<entry>$a &amp;= $b</entry>
651
-
<entry>$a = $a &amp; $b</entry>
652
-
<entry>Bitwise And</entry>
653
-
</row>
654
-
<row>
655
-
<entry>$a |= $b</entry>
656
-
<entry>$a = $a | $b</entry>
657
-
<entry>Bitwise Or</entry>
658
-
</row>
659
-
<row>
660
-
<entry>$a ^= $b</entry>
661
-
<entry>$a = $a ^ $b</entry>
662
-
<entry>Bitwise Xor</entry>
663
-
</row>
664
-
<row>
665
-
<entry>$a &lt;&lt;= $b</entry>
666
-
<entry>$a = $a &lt;&lt; $b</entry>
667
-
<entry>Left Shift</entry>
668
-
</row>
669
-
<row>
670
-
<entry>$a &gt;&gt;= $b</entry>
671
-
<entry>$a = $a &gt;&gt; $b</entry>
672
-
<entry>Right Shift</entry>
673
-
</row>
674
-
</tbody>
675
-
</tgroup>
676
-
</informaltable>
677
-
</sect2>
678
-

679
-
<sect2 xml:id="language.operators.assignment.other">
680
-
<title>Other Assignment Operators</title>
681
-
<informaltable>
682
-
<tgroup cols="3">
683
-
<thead>
684
-
<row>
685
-
<entry>Example</entry>
686
-
<entry>Equivalent</entry>
687
-
<entry>Operation</entry>
688
-
</row>
689
-
</thead>
690
-
<tbody>
691
-
<row>
692
-
<entry>$a .= $b</entry>
693
-
<entry>$a = $a . $b</entry>
694
-
<entry>String Concatenation</entry>
695
-
</row>
696
-
<row>
697
-
<entry>$a ??= $b</entry>
698
-
<entry>$a = $a ?? $b</entry>
699
-
<entry>Null Coalesce</entry>
700
-
</row>
701
-
</tbody>
702
-
</tgroup>
703
-
</informaltable>
704
-
</sect2>
705
-

706
-
<sect2 role="seealso" xml:id="language.operators.assignment.see-also">
707
-
&reftitle.seealso;
708
-
<para>
709
-
<simplelist>
710
-
<member><link linkend="language.operators.arithmetic">arithmetic operators</link></member>
711
-
<member><link linkend="language.operators.bitwise">bitwise operators</link></member>
712
-
<member><link linkend="language.operators.comparison.coalesce">null coalescing operator</link></member>
713
-
</simplelist>
714
-
</para>
715
-
</sect2>
716
-
</sect1>
717
-

718
-
<sect1 xml:id="language.operators.bitwise">
719
-
<title>Bitwise Operators</title>
720
-
<simpara>
721
-
Bitwise operators allow evaluation and manipulation of specific
722
-
bits within an integer.
723
-
</simpara>
724
-
<table>
725
-
<title>Bitwise Operators</title>
726
-
<tgroup cols="3">
727
-
<thead>
728
-
<row>
729
-
<entry>Example</entry>
730
-
<entry>Name</entry>
731
-
<entry>Result</entry>
732
-
</row>
733
-
</thead>
734
-
<tbody>
735
-
<row>
736
-
<entry><userinput>$a &amp; $b</userinput></entry>
737
-
<entry>And</entry>
738
-
<entry>Bits that are set in both <varname>$a</varname> and <varname>$b</varname> are set.</entry>
739
-
</row>
740
-
<row>
741
-
<entry><userinput>$a | $b</userinput></entry>
742
-
<entry>Or (inclusive or)</entry>
743
-
<entry>Bits that are set in either <varname>$a</varname> or <varname>$b</varname> are set.</entry>
744
-
</row>
745
-
<row>
746
-
<entry><userinput>$a ^ $b</userinput></entry>
747
-
<entry>Xor (exclusive or)</entry>
748
-
<entry>
749
-
Bits that are set in <varname>$a</varname> or <varname>$b</varname> but not both are set.
750
-
</entry>
751
-
</row>
752
-
<row>
753
-
<entry><userinput>~ $a</userinput></entry>
754
-
<entry>Not</entry>
755
-
<entry>
756
-
Bits that are set in <varname>$a</varname> are not set, and vice versa.
757
-
</entry>
758
-
</row>
759
-
<row>
760
-
<entry><userinput>$a &lt;&lt; $b</userinput></entry>
761
-
<entry>Shift left</entry>
762
-
<entry>
763
-
Shift the bits of <varname>$a</varname> <varname>$b</varname> steps to the left (each step means
764
-
"multiply by two")
765
-
</entry>
766
-
</row>
767
-
<row>
768
-
<entry><userinput>$a &gt;&gt; $b</userinput></entry>
769
-
<entry>Shift right</entry>
770
-
<entry>
771
-
Shift the bits of <varname>$a</varname> <varname>$b</varname> steps to the right (each step means
772
-
"divide by two")
773
-
</entry>
774
-
</row>
775
-
</tbody>
776
-
</tgroup>
777
-
</table>
778
-
<para>
779
-
Bit shifting in PHP is arithmetic.
780
-
Bits shifted off either end are discarded.
781
-
Left shifts have zeros shifted in on the right while the sign
782
-
bit is shifted out on the left, meaning the sign of an operand
783
-
is not preserved.
784
-
Right shifts have copies of the sign bit shifted in on the left,
785
-
meaning the sign of an operand is preserved.
786
-
</para>
787
-
<para>
788
-
Use parentheses to ensure the desired
789
-
<link linkend="language.operators.precedence">precedence</link>.
790
-
For example, <literal>$a &amp; $b == true</literal> evaluates
791
-
the equivalency then the bitwise and; while
792
-
<literal>($a &amp; $b) == true</literal> evaluates the bitwise and
793
-
then the equivalency.
794
-
</para>
795
-
<para>
796
-
If both operands for the <literal>&amp;</literal>, <literal>|</literal> and
797
-
<literal>^</literal> operators are strings, then the operation will be
798
-
performed on the ASCII values of the characters that make up the strings and
799
-
the result will be a string. In all other cases, both operands will be
800
-
<link linkend="language.types.integer.casting">converted to integers</link>
801
-
and the result will be an integer.
802
-
</para>
803
-
<para>
804
-
If the operand for the <literal>~</literal> operator is a string, the
805
-
operation will be performed on the ASCII values of the characters that make
806
-
up the string and the result will be a string, otherwise the operand and the
807
-
result will be treated as integers.
808
-
</para>
809
-
<para>
810
-
Both operands and the result for the <literal>&lt;&lt;</literal> and
811
-
<literal>&gt;&gt;</literal> operators are always treated as integers.
812
-
</para>
813
-
<para>
814
-
<informalexample>
815
-
<para>
816
-
<literallayout>
817
-
PHP's error_reporting ini setting uses bitwise values,
818
-
providing a real-world demonstration of turning
819
-
bits off. To show all errors, except for notices,
820
-
the php.ini file instructions say to use:
821
-
<userinput>E_ALL &amp; ~E_NOTICE</userinput>
822
-
</literallayout>
823
-
</para>
824
-
<para>
825
-
<literallayout>
826
-
This works by starting with E_ALL:
827
-
<computeroutput>00000000000000000111011111111111</computeroutput>
828
-
Then taking the value of E_NOTICE...
829
-
<computeroutput>00000000000000000000000000001000</computeroutput>
830
-
... and inverting it via <literal>~</literal>:
831
-
<computeroutput>11111111111111111111111111110111</computeroutput>
832
-
Finally, it uses AND (&amp;) to find the bits turned
833
-
on in both values:
834
-
<computeroutput>00000000000000000111011111110111</computeroutput>
835
-
</literallayout>
836
-
</para>
837
-
<para>
838
-
<literallayout>
839
-
Another way to accomplish that is using XOR (<literal>^</literal>)
840
-
to find bits that are on in only one value or the other:
841
-
<userinput>E_ALL ^ E_NOTICE</userinput>
842
-
</literallayout>
843
-
</para>
844
-
</informalexample>
845
-
</para>
846
-
<para>
847
-
<informalexample>
848
-
<para>
849
-
<literallayout>
850
-
error_reporting can also be used to demonstrate turning bits on.
851
-
The way to show just errors and recoverable errors is:
852
-
<userinput>E_ERROR | E_RECOVERABLE_ERROR</userinput>
853
-
</literallayout>
854
-
</para>
855
-
<para>
856
-
<literallayout>
857
-
This process combines E_ERROR
858
-
<computeroutput>00000000000000000000000000000001</computeroutput>
859
-
and
860
-
<computeroutput>00000000000000000001000000000000</computeroutput>
861
-
using the OR (<literal>|</literal>) operator
862
-
to get the bits turned on in either value:
863
-
<computeroutput>00000000000000000001000000000001</computeroutput>
864
-
</literallayout>
865
-
</para>
866
-
</informalexample>
867
-
</para>
868
-
<para>
869
-
<example>
870
-
<title>Bitwise AND, OR and XOR operations on integers</title>
871
-
<programlisting role="php">
872
-
<![CDATA[
873
-
<?php
874
-
/*
875
-
* Ignore the top section,
876
-
* it is just formatting to make output clearer.
877
-
*/
878
-

879
-
$format = '(%1$2d = %1$04b) = (%2$2d = %2$04b)'
880
-
. ' %3$s (%4$2d = %4$04b)' . "\n";
881
-

882
-
echo <<<EOH
883
-
--------- --------- -- ---------
884
-
result value op test
885
-
--------- --------- -- ---------
886
-
EOH;
887
-

888
-

889
-
/*
890
-
* Here are the examples.
891
-
*/
892
-

893
-
$values = array(0, 1, 2, 4, 8);
894
-
$test = 1 + 4;
895
-

896
-
echo "\n Bitwise AND \n";
897
-
foreach ($values as $value) {
898
-
$result = $value & $test;
899
-
printf($format, $result, $value, '&', $test);
900
-
}
901
-

902
-
echo "\n Bitwise Inclusive OR \n";
903
-
foreach ($values as $value) {
904
-
$result = $value | $test;
905
-
printf($format, $result, $value, '|', $test);
906
-
}
907
-

908
-
echo "\n Bitwise Exclusive OR (XOR) \n";
909
-
foreach ($values as $value) {
910
-
$result = $value ^ $test;
911
-
printf($format, $result, $value, '^', $test);
912
-
}
913
-
?>
914
-
]]>
915
-
</programlisting>
916
-
&example.outputs;
917
-
<screen>
918
-
<![CDATA[
919
-
--------- --------- -- ---------
920
-
result value op test
921
-
--------- --------- -- ---------
922
-
Bitwise AND
923
-
( 0 = 0000) = ( 0 = 0000) & ( 5 = 0101)
924
-
( 1 = 0001) = ( 1 = 0001) & ( 5 = 0101)
925
-
( 0 = 0000) = ( 2 = 0010) & ( 5 = 0101)
926
-
( 4 = 0100) = ( 4 = 0100) & ( 5 = 0101)
927
-
( 0 = 0000) = ( 8 = 1000) & ( 5 = 0101)
928
-

929
-
Bitwise Inclusive OR
930
-
( 5 = 0101) = ( 0 = 0000) | ( 5 = 0101)
931
-
( 5 = 0101) = ( 1 = 0001) | ( 5 = 0101)
932
-
( 7 = 0111) = ( 2 = 0010) | ( 5 = 0101)
933
-
( 5 = 0101) = ( 4 = 0100) | ( 5 = 0101)
934
-
(13 = 1101) = ( 8 = 1000) | ( 5 = 0101)
935
-

936
-
Bitwise Exclusive OR (XOR)
937
-
( 5 = 0101) = ( 0 = 0000) ^ ( 5 = 0101)
938
-
( 4 = 0100) = ( 1 = 0001) ^ ( 5 = 0101)
939
-
( 7 = 0111) = ( 2 = 0010) ^ ( 5 = 0101)
940
-
( 1 = 0001) = ( 4 = 0100) ^ ( 5 = 0101)
941
-
(13 = 1101) = ( 8 = 1000) ^ ( 5 = 0101)
942
-
]]>
943
-
</screen>
944
-
</example>
945
-
</para>
946
-
<para>
947
-
<example>
948
-
<title>Bitwise XOR operations on strings</title>
949
-
<programlisting role="php">
950
-
<![CDATA[
951
-
<?php
952
-
echo 12 ^ 9; // Outputs '5'
953
-

954
-
echo "12" ^ "9"; // Outputs the Backspace character (ascii 8)
955
-
// ('1' (ascii 49)) ^ ('9' (ascii 57)) = #8
956
-

957
-
echo "hallo" ^ "hello"; // Outputs the ascii values #0 #4 #0 #0 #0
958
-
// 'a' ^ 'e' = #4
959
-

960
-
echo 2 ^ "3"; // Outputs 1
961
-
// 2 ^ ((int)"3") == 1
962
-

963
-
echo "2" ^ 3; // Outputs 1
964
-
// ((int)"2") ^ 3 == 1
965
-
?>
966
-
]]>
967
-
</programlisting>
968
-
</example>
969
-
</para>
970
-
<para>
971
-
<example>
972
-
<title>Bit shifting on integers</title>
973
-
<programlisting role="php">
974
-
<![CDATA[
975
-
<?php
976
-
/*
977
-
* Here are the examples.
978
-
*/
979
-

980
-
echo "\n--- BIT SHIFT RIGHT ON POSITIVE INTEGERS ---\n";
981
-

982
-
$val = 4;
983
-
$places = 1;
984
-
$res = $val >> $places;
985
-
p($res, $val, '>>', $places, 'copy of sign bit shifted into left side');
986
-

987
-
$val = 4;
988
-
$places = 2;
989
-
$res = $val >> $places;
990
-
p($res, $val, '>>', $places);
991
-

992
-
$val = 4;
993
-
$places = 3;
994
-
$res = $val >> $places;
995
-
p($res, $val, '>>', $places, 'bits shift out right side');
996
-

997
-
$val = 4;
998
-
$places = 4;
999
-
$res = $val >> $places;
1000
-
p($res, $val, '>>', $places, 'same result as above; can not shift beyond 0');
1001
-

1002
-

1003
-
echo "\n--- BIT SHIFT RIGHT ON NEGATIVE INTEGERS ---\n";
1004
-

1005
-
$val = -4;
1006
-
$places = 1;
1007
-
$res = $val >> $places;
1008
-
p($res, $val, '>>', $places, 'copy of sign bit shifted into left side');
1009
-

1010
-
$val = -4;
1011
-
$places = 2;
1012
-
$res = $val >> $places;
1013
-
p($res, $val, '>>', $places, 'bits shift out right side');
1014
-

1015
-
$val = -4;
1016
-
$places = 3;
1017
-
$res = $val >> $places;
1018
-
p($res, $val, '>>', $places, 'same result as above; can not shift beyond -1');
1019
-

1020
-

1021
-
echo "\n--- BIT SHIFT LEFT ON POSITIVE INTEGERS ---\n";
1022
-

1023
-
$val = 4;
1024
-
$places = 1;
1025
-
$res = $val << $places;
1026
-
p($res, $val, '<<', $places, 'zeros fill in right side');
1027
-

1028
-
$val = 4;
1029
-
$places = (PHP_INT_SIZE * 8) - 4;
1030
-
$res = $val << $places;
1031
-
p($res, $val, '<<', $places);
1032
-

1033
-
$val = 4;
1034
-
$places = (PHP_INT_SIZE * 8) - 3;
1035
-
$res = $val << $places;
1036
-
p($res, $val, '<<', $places, 'sign bits get shifted out');
1037
-

1038
-
$val = 4;
1039
-
$places = (PHP_INT_SIZE * 8) - 2;
1040
-
$res = $val << $places;
1041
-
p($res, $val, '<<', $places, 'bits shift out left side');
1042
-

1043
-

1044
-
echo "\n--- BIT SHIFT LEFT ON NEGATIVE INTEGERS ---\n";
1045
-

1046
-
$val = -4;
1047
-
$places = 1;
1048
-
$res = $val << $places;
1049
-
p($res, $val, '<<', $places, 'zeros fill in right side');
1050
-

1051
-
$val = -4;
1052
-
$places = (PHP_INT_SIZE * 8) - 3;
1053
-
$res = $val << $places;
1054
-
p($res, $val, '<<', $places);
1055
-

1056
-
$val = -4;
1057
-
$places = (PHP_INT_SIZE * 8) - 2;
1058
-
$res = $val << $places;
1059
-
p($res, $val, '<<', $places, 'bits shift out left side, including sign bit');
1060
-

1061
-

1062
-
/*
1063
-
* Ignore this bottom section,
1064
-
* it is just formatting to make output clearer.
1065
-
*/
1066
-

1067
-
function p($res, $val, $op, $places, $note = '') {
1068
-
$format = '%0' . (PHP_INT_SIZE * 8) . "b\n";
1069
-

1070
-
printf("Expression: %d = %d %s %d\n", $res, $val, $op, $places);
1071
-

1072
-
echo " Decimal:\n";
1073
-
printf(" val=%d\n", $val);
1074
-
printf(" res=%d\n", $res);
1075
-

1076
-
echo " Binary:\n";
1077
-
printf(' val=' . $format, $val);
1078
-
printf(' res=' . $format, $res);
1079
-

1080
-
if ($note) {
1081
-
echo " NOTE: $note\n";
1082
-
}
1083
-

1084
-
echo "\n";
1085
-
}
1086
-
?>
1087
-
]]>
1088
-
</programlisting>
1089
-
&example.outputs.32bit;
1090
-
<screen>
1091
-
<![CDATA[
1092
-

1093
-
--- BIT SHIFT RIGHT ON POSITIVE INTEGERS ---
1094
-
Expression: 2 = 4 >> 1
1095
-
Decimal:
1096
-
val=4
1097
-
res=2
1098
-
Binary:
1099
-
val=00000000000000000000000000000100
1100
-
res=00000000000000000000000000000010
1101
-
NOTE: copy of sign bit shifted into left side
1102
-

1103
-
Expression: 1 = 4 >> 2
1104
-
Decimal:
1105
-
val=4
1106
-
res=1
1107
-
Binary:
1108
-
val=00000000000000000000000000000100
1109
-
res=00000000000000000000000000000001
1110
-

1111
-
Expression: 0 = 4 >> 3
1112
-
Decimal:
1113
-
val=4
1114
-
res=0
1115
-
Binary:
1116
-
val=00000000000000000000000000000100
1117
-
res=00000000000000000000000000000000
1118
-
NOTE: bits shift out right side
1119
-

1120
-
Expression: 0 = 4 >> 4
1121
-
Decimal:
1122
-
val=4
1123
-
res=0
1124
-
Binary:
1125
-
val=00000000000000000000000000000100
1126
-
res=00000000000000000000000000000000
1127
-
NOTE: same result as above; can not shift beyond 0
1128
-

1129
-

1130
-
--- BIT SHIFT RIGHT ON NEGATIVE INTEGERS ---
1131
-
Expression: -2 = -4 >> 1
1132
-
Decimal:
1133
-
val=-4
1134
-
res=-2
1135
-
Binary:
1136
-
val=11111111111111111111111111111100
1137
-
res=11111111111111111111111111111110
1138
-
NOTE: copy of sign bit shifted into left side
1139
-

1140
-
Expression: -1 = -4 >> 2
1141
-
Decimal:
1142
-
val=-4
1143
-
res=-1
1144
-
Binary:
1145
-
val=11111111111111111111111111111100
1146
-
res=11111111111111111111111111111111
1147
-
NOTE: bits shift out right side
1148
-

1149
-
Expression: -1 = -4 >> 3
1150
-
Decimal:
1151
-
val=-4
1152
-
res=-1
1153
-
Binary:
1154
-
val=11111111111111111111111111111100
1155
-
res=11111111111111111111111111111111
1156
-
NOTE: same result as above; can not shift beyond -1
1157
-

1158
-

1159
-
--- BIT SHIFT LEFT ON POSITIVE INTEGERS ---
1160
-
Expression: 8 = 4 << 1
1161
-
Decimal:
1162
-
val=4
1163
-
res=8
1164
-
Binary:
1165
-
val=00000000000000000000000000000100
1166
-
res=00000000000000000000000000001000
1167
-
NOTE: zeros fill in right side
1168
-

1169
-
Expression: 1073741824 = 4 << 28
1170
-
Decimal:
1171
-
val=4
1172
-
res=1073741824
1173
-
Binary:
1174
-
val=00000000000000000000000000000100
1175
-
res=01000000000000000000000000000000
1176
-

1177
-
Expression: -2147483648 = 4 << 29
1178
-
Decimal:
1179
-
val=4
1180
-
res=-2147483648
1181
-
Binary:
1182
-
val=00000000000000000000000000000100
1183
-
res=10000000000000000000000000000000
1184
-
NOTE: sign bits get shifted out
1185
-

1186
-
Expression: 0 = 4 << 30
1187
-
Decimal:
1188
-
val=4
1189
-
res=0
1190
-
Binary:
1191
-
val=00000000000000000000000000000100
1192
-
res=00000000000000000000000000000000
1193
-
NOTE: bits shift out left side
1194
-

1195
-

1196
-
--- BIT SHIFT LEFT ON NEGATIVE INTEGERS ---
1197
-
Expression: -8 = -4 << 1
1198
-
Decimal:
1199
-
val=-4
1200
-
res=-8
1201
-
Binary:
1202
-
val=11111111111111111111111111111100
1203
-
res=11111111111111111111111111111000
1204
-
NOTE: zeros fill in right side
1205
-

1206
-
Expression: -2147483648 = -4 << 29
1207
-
Decimal:
1208
-
val=-4
1209
-
res=-2147483648
1210
-
Binary:
1211
-
val=11111111111111111111111111111100
1212
-
res=10000000000000000000000000000000
1213
-

1214
-
Expression: 0 = -4 << 30
1215
-
Decimal:
1216
-
val=-4
1217
-
res=0
1218
-
Binary:
1219
-
val=11111111111111111111111111111100
1220
-
res=00000000000000000000000000000000
1221
-
NOTE: bits shift out left side, including sign bit
1222
-
]]>
1223
-
</screen>
1224
-
&example.outputs.64bit;
1225
-
<screen>
1226
-
<![CDATA[
1227
-

1228
-
--- BIT SHIFT RIGHT ON POSITIVE INTEGERS ---
1229
-
Expression: 2 = 4 >> 1
1230
-
Decimal:
1231
-
val=4
1232
-
res=2
1233
-
Binary:
1234
-
val=0000000000000000000000000000000000000000000000000000000000000100
1235
-
res=0000000000000000000000000000000000000000000000000000000000000010
1236
-
NOTE: copy of sign bit shifted into left side
1237
-

1238
-
Expression: 1 = 4 >> 2
1239
-
Decimal:
1240
-
val=4
1241
-
res=1
1242
-
Binary:
1243
-
val=0000000000000000000000000000000000000000000000000000000000000100
1244
-
res=0000000000000000000000000000000000000000000000000000000000000001
1245
-

1246
-
Expression: 0 = 4 >> 3
1247
-
Decimal:
1248
-
val=4
1249
-
res=0
1250
-
Binary:
1251
-
val=0000000000000000000000000000000000000000000000000000000000000100
1252
-
res=0000000000000000000000000000000000000000000000000000000000000000
1253
-
NOTE: bits shift out right side
1254
-

1255
-
Expression: 0 = 4 >> 4
1256
-
Decimal:
1257
-
val=4
1258
-
res=0
1259
-
Binary:
1260
-
val=0000000000000000000000000000000000000000000000000000000000000100
1261
-
res=0000000000000000000000000000000000000000000000000000000000000000
1262
-
NOTE: same result as above; can not shift beyond 0
1263
-

1264
-

1265
-
--- BIT SHIFT RIGHT ON NEGATIVE INTEGERS ---
1266
-
Expression: -2 = -4 >> 1
1267
-
Decimal:
1268
-
val=-4
1269
-
res=-2
1270
-
Binary:
1271
-
val=1111111111111111111111111111111111111111111111111111111111111100
1272
-
res=1111111111111111111111111111111111111111111111111111111111111110
1273
-
NOTE: copy of sign bit shifted into left side
1274
-

1275
-
Expression: -1 = -4 >> 2
1276
-
Decimal:
1277
-
val=-4
1278
-
res=-1
1279
-
Binary:
1280
-
val=1111111111111111111111111111111111111111111111111111111111111100
1281
-
res=1111111111111111111111111111111111111111111111111111111111111111
1282
-
NOTE: bits shift out right side
1283
-

1284
-
Expression: -1 = -4 >> 3
1285
-
Decimal:
1286
-
val=-4
1287
-
res=-1
1288
-
Binary:
1289
-
val=1111111111111111111111111111111111111111111111111111111111111100
1290
-
res=1111111111111111111111111111111111111111111111111111111111111111
1291
-
NOTE: same result as above; can not shift beyond -1
1292
-

1293
-

1294
-
--- BIT SHIFT LEFT ON POSITIVE INTEGERS ---
1295
-
Expression: 8 = 4 << 1
1296
-
Decimal:
1297
-
val=4
1298
-
res=8
1299
-
Binary:
1300
-
val=0000000000000000000000000000000000000000000000000000000000000100
1301
-
res=0000000000000000000000000000000000000000000000000000000000001000
1302
-
NOTE: zeros fill in right side
1303
-

1304
-
Expression: 4611686018427387904 = 4 << 60
1305
-
Decimal:
1306
-
val=4
1307
-
res=4611686018427387904
1308
-
Binary:
1309
-
val=0000000000000000000000000000000000000000000000000000000000000100
1310
-
res=0100000000000000000000000000000000000000000000000000000000000000
1311
-

1312
-
Expression: -9223372036854775808 = 4 << 61
1313
-
Decimal:
1314
-
val=4
1315
-
res=-9223372036854775808
1316
-
Binary:
1317
-
val=0000000000000000000000000000000000000000000000000000000000000100
1318
-
res=1000000000000000000000000000000000000000000000000000000000000000
1319
-
NOTE: sign bits get shifted out
1320
-

1321
-
Expression: 0 = 4 << 62
1322
-
Decimal:
1323
-
val=4
1324
-
res=0
1325
-
Binary:
1326
-
val=0000000000000000000000000000000000000000000000000000000000000100
1327
-
res=0000000000000000000000000000000000000000000000000000000000000000
1328
-
NOTE: bits shift out left side
1329
-

1330
-

1331
-
--- BIT SHIFT LEFT ON NEGATIVE INTEGERS ---
1332
-
Expression: -8 = -4 << 1
1333
-
Decimal:
1334
-
val=-4
1335
-
res=-8
1336
-
Binary:
1337
-
val=1111111111111111111111111111111111111111111111111111111111111100
1338
-
res=1111111111111111111111111111111111111111111111111111111111111000
1339
-
NOTE: zeros fill in right side
1340
-

1341
-
Expression: -9223372036854775808 = -4 << 61
1342
-
Decimal:
1343
-
val=-4
1344
-
res=-9223372036854775808
1345
-
Binary:
1346
-
val=1111111111111111111111111111111111111111111111111111111111111100
1347
-
res=1000000000000000000000000000000000000000000000000000000000000000
1348
-

1349
-
Expression: 0 = -4 << 62
1350
-
Decimal:
1351
-
val=-4
1352
-
res=0
1353
-
Binary:
1354
-
val=1111111111111111111111111111111111111111111111111111111111111100
1355
-
res=0000000000000000000000000000000000000000000000000000000000000000
1356
-
NOTE: bits shift out left side, including sign bit
1357
-
]]>
1358
-
</screen>
1359
-
</example>
1360
-
</para>
1361
-
<warning>
1362
-
<para>
1363
-
Use functions from the <link linkend="book.gmp">gmp</link> extension for
1364
-
bitwise manipulation on numbers beyond <literal>PHP_INT_MAX</literal>.
1365
-
</para>
1366
-
</warning>
1367
-

1368
-
<sect2 role="seealso">
1369
-
&reftitle.seealso;
1370
-
<para>
1371
-
<simplelist>
1372
-
<!-- <link linkend="language.oop5.basic.class.class">::class</link> -->
1373
-
<member><function>pack</function></member>
1374
-
<member><function>unpack</function></member>
1375
-
<member><function>gmp_and</function></member>
1376
-
<member><function>gmp_or</function></member>
1377
-
<member><function>gmp_xor</function></member>
1378
-
<member><function>gmp_testbit</function></member>
1379
-
<member><function>gmp_clrbit</function></member>
1380
-
</simplelist>
1381
-
</para>
1382
-
</sect2>
1383
-
</sect1>
1384
-

1385
-
<sect1 xml:id="language.operators.comparison">
1386
-
<title>Comparison Operators</title>
1387
-
<simpara>
1388
-
Comparison operators, as their name implies, allow you to compare
1389
-
two values. You may also be interested in viewing
1390
-
<link linkend="types.comparisons">the type comparison tables</link>,
1391
-
as they show examples of various type related comparisons.
1392
-
</simpara>
1393
-
<table>
1394
-
<title>Comparison Operators</title>
1395
-
<tgroup cols="3">
1396
-
<thead>
1397
-
<row>
1398
-
<entry>Example</entry>
1399
-
<entry>Name</entry>
1400
-
<entry>Result</entry>
1401
-
</row>
1402
-
</thead>
1403
-
<tbody>
1404
-
<row>
1405
-
<entry>$a == $b</entry>
1406
-
<entry>Equal</entry>
1407
-
<entry>&true; if <varname>$a</varname> is equal to <varname>$b</varname> after type juggling.</entry>
1408
-
</row>
1409
-
<row>
1410
-
<entry>$a === $b</entry>
1411
-
<entry>Identical</entry>
1412
-
<entry>
1413
-
&true; if <varname>$a</varname> is equal to <varname>$b</varname>, and they are of the same
1414
-
type.
1415
-
</entry>
1416
-
</row>
1417
-
<row>
1418
-
<entry>$a != $b</entry>
1419
-
<entry>Not equal</entry>
1420
-
<entry>&true; if <varname>$a</varname> is not equal to <varname>$b</varname> after type juggling.</entry>
1421
-
</row>
1422
-
<row>
1423
-
<entry>$a &lt;&gt; $b</entry>
1424
-
<entry>Not equal</entry>
1425
-
<entry>&true; if <varname>$a</varname> is not equal to <varname>$b</varname> after type juggling.</entry>
1426
-
</row>
1427
-
<row>
1428
-
<entry>$a !== $b</entry>
1429
-
<entry>Not identical</entry>
1430
-
<entry>
1431
-
&true; if <varname>$a</varname> is not equal to <varname>$b</varname>, or they are not of the same
1432
-
type.
1433
-
</entry>
1434
-
</row>
1435
-
<row>
1436
-
<entry>$a &lt; $b</entry>
1437
-
<entry>Less than</entry>
1438
-
<entry>&true; if <varname>$a</varname> is strictly less than <varname>$b</varname>.</entry>
1439
-
</row>
1440
-
<row>
1441
-
<entry>$a &gt; $b</entry>
1442
-
<entry>Greater than</entry>
1443
-
<entry>&true; if <varname>$a</varname> is strictly greater than <varname>$b</varname>.</entry>
1444
-
</row>
1445
-
<row>
1446
-
<entry>$a &lt;= $b</entry>
1447
-
<entry>Less than or equal to </entry>
1448
-
<entry>&true; if <varname>$a</varname> is less than or equal to <varname>$b</varname>.</entry>
1449
-
</row>
1450
-
<row>
1451
-
<entry>$a &gt;= $b</entry>
1452
-
<entry>Greater than or equal to </entry>
1453
-
<entry>&true; if <varname>$a</varname> is greater than or equal to <varname>$b</varname>.</entry>
1454
-
</row>
1455
-
<row>
1456
-
<entry>$a &lt;=&gt; $b</entry>
1457
-
<entry>Spaceship</entry>
1458
-
<entry>
1459
-
An <type>int</type> less than, equal to, or greater than zero when
1460
-
<varname>$a</varname> is less than, equal to, or greater than
1461
-
<varname>$b</varname>, respectively.
1462
-
</entry>
1463
-
</row>
1464
-
</tbody>
1465
-
</tgroup>
1466
-
</table>
1467
-
<para>
1468
-
If both operands are
1469
-
<link linkend="language.types.numeric-strings">numeric strings</link>,
1470
-
or one operand is a number and the other one is a
1471
-
<link linkend="language.types.numeric-strings">numeric string</link>,
1472
-
then the comparison is done numerically.
1473
-
These rules also apply to the
1474
-
<link linkend="control-structures.switch">switch</link> statement.
1475
-
The type conversion does not take place when the comparison is
1476
-
<literal>===</literal> or <literal>!==</literal> as this involves
1477
-
comparing the type as well as the value.
1478
-
</para>
1479
-

1480
-
<warning>
1481
-
<para>
1482
-
Prior to PHP 8.0.0, if a <type>string</type> is compared to a number
1483
-
or a numeric string then the <type>string</type> was converted to a
1484
-
number before performing the comparison. This can lead to surprising
1485
-
results as can be seen with the following example:
1486
-
<informalexample>
1487
-
<programlisting role="php">
1488
-
<![CDATA[
1489
-
<?php
1490
-
var_dump(0 == "a"); // 0 == 0 -> true
1491
-
var_dump("1" == "01"); // 1 == 1 -> true
1492
-
var_dump("10" == "1e1"); // 10 == 10 -> true
1493
-
var_dump(100 == "1e2"); // 100 == 100 -> true
1494
-

1495
-
switch ("a") {
1496
-
case 0:
1497
-
echo "0";
1498
-
break;
1499
-
case "a": // never reached because "a" is already matched with 0
1500
-
echo "a";
1501
-
break;
1502
-
}
1503
-
?>
1504
-
]]>
1505
-
</programlisting>
1506
-
</informalexample>
1507
-
</para>
1508
-
</warning>
1509
-

1510
-
<para>
1511
-
<informalexample>
1512
-
<programlisting role="php">
1513
-
<![CDATA[
1514
-
<?php
1515
-
// Integers
1516
-
echo 1 <=> 1; // 0
1517
-
echo 1 <=> 2; // -1
1518
-
echo 2 <=> 1; // 1
1519
-
1520
-
// Floats
1521
-
echo 1.5 <=> 1.5; // 0
1522
-
echo 1.5 <=> 2.5; // -1
1523
-
echo 2.5 <=> 1.5; // 1
1524
-
1525
-
// Strings
1526
-
echo "a" <=> "a"; // 0
1527
-
echo "a" <=> "b"; // -1
1528
-
echo "b" <=> "a"; // 1
1529
-
1530
-
echo "a" <=> "aa"; // -1
1531
-
echo "zz" <=> "aa"; // 1
1532
-
1533
-
// Arrays
1534
-
echo [] <=> []; // 0
1535
-
echo [1, 2, 3] <=> [1, 2, 3]; // 0
1536
-
echo [1, 2, 3] <=> []; // 1
1537
-
echo [1, 2, 3] <=> [1, 2, 1]; // 1
1538
-
echo [1, 2, 3] <=> [1, 2, 4]; // -1
1539
-
1540
-
// Objects
1541
-
$a = (object) ["a" => "b"];
1542
-
$b = (object) ["a" => "b"];
1543
-
echo $a <=> $b; // 0
1544
-
1545
-
$a = (object) ["a" => "b"];
1546
-
$b = (object) ["a" => "c"];
1547
-
echo $a <=> $b; // -1
1548
-
1549
-
$a = (object) ["a" => "c"];
1550
-
$b = (object) ["a" => "b"];
1551
-
echo $a <=> $b; // 1
1552
-
1553
-
// not only values are compared; keys must match
1554
-
$a = (object) ["a" => "b"];
1555
-
$b = (object) ["b" => "b"];
1556
-
echo $a <=> $b; // 1
1557
-

1558
-
?>
1559
-
]]>
1560
-
1561
-
</programlisting>
1562
-
</informalexample>
1563
-
</para>
1564
-

1565
-
<para>
1566
-
For various types, comparison is done according to the following
1567
-
table (in order).
1568
-
</para>
1569
-
<table xml:id="language.operators.comparison.types">
1570
-
<title>Comparison with Various Types</title>
1571
-
<tgroup cols="3">
1572
-
<thead>
1573
-
<row>
1574
-
<entry>Type of Operand 1</entry>
1575
-
<entry>Type of Operand 2</entry>
1576
-
<entry>Result</entry>
1577
-
</row>
1578
-
</thead>
1579
-
<tbody>
1580
-
<row>
1581
-
<entry><type>null</type> or <type>string</type></entry>
1582
-
<entry><type>string</type></entry>
1583
-
<entry>Convert &null; to "", numerical or lexical comparison</entry>
1584
-
</row>
1585
-
<row>
1586
-
<entry><type>bool</type> or <type>null</type></entry>
1587
-
<entry>anything</entry>
1588
-
<entry>Convert both sides to <type>bool</type>, &false; &lt; &true;</entry>
1589
-
</row>
1590
-
<row>
1591
-
<entry><type>object</type></entry>
1592
-
<entry><type>object</type></entry>
1593
-
<entry>Built-in classes can define its own comparison, different classes
1594
-
are uncomparable, same class see <link
1595
-
linkend="language.oop5.object-comparison">Object Comparison</link>
1596
-
</entry>
1597
-
</row>
1598
-
<row>
1599
-
<entry><type>string</type>, <type>resource</type>, <type>int</type> or <type>float</type></entry>
1600
-
<entry><type>string</type>, <type>resource</type>, <type>int</type> or <type>float</type></entry>
1601
-
<entry>Translate strings and resources to numbers, usual math</entry>
1602
-
</row>
1603
-
<row>
1604
-
<entry><type>array</type></entry>
1605
-
<entry><type>array</type></entry>
1606
-
<entry>Array with fewer members is smaller, if key from operand 1 is not
1607
-
found in operand 2 then arrays are uncomparable, otherwise - compare
1608
-
value by value (see following example)</entry>
1609
-
</row>
1610
-
<row>
1611
-
<entry><type>object</type></entry>
1612
-
<entry>anything</entry>
1613
-
<entry><type>object</type> is always greater</entry>
1614
-
</row>
1615
-
<row>
1616
-
<entry><type>array</type></entry>
1617
-
<entry>anything</entry>
1618
-
<entry><type>array</type> is always greater</entry>
1619
-
</row>
1620
-
</tbody>
1621
-
</tgroup>
1622
-
</table>
1623
-

1624
-
<para>
1625
-
<example>
1626
-
<title>Boolean/null comparison</title>
1627
-
<programlisting role="php">
1628
-
<![CDATA[
1629
-
<?php
1630
-
// Bool and null are compared as bool always
1631
-
var_dump(1 == TRUE); // TRUE - same as (bool)1 == TRUE
1632
-
var_dump(0 == FALSE); // TRUE - same as (bool)0 == FALSE
1633
-
var_dump(100 < TRUE); // FALSE - same as (bool)100 < TRUE
1634
-
var_dump(-10 < FALSE);// FALSE - same as (bool)-10 < FALSE
1635
-
var_dump(min(-100, -10, NULL, 10, 100)); // NULL - (bool)NULL < (bool)-100 is FALSE < TRUE
1636
-
?>
1637
-
]]>
1638
-
</programlisting>
1639
-
</example>
1640
-
</para>
1641
-

1642
-

1643
-
<para>
1644
-
<example>
1645
-
<title>Transcription of standard array comparison</title>
1646
-
<programlisting role="php">
1647
-
<![CDATA[
1648
-
<?php
1649
-
// Arrays are compared like this with standard comparison operators
1650
-
function standard_array_compare($op1, $op2)
1651
-
{
1652
-
if (count($op1) < count($op2)) {
1653
-
return -1; // $op1 < $op2
1654
-
} elseif (count($op1) > count($op2)) {
1655
-
return 1; // $op1 > $op2
1656
-
}
1657
-
foreach ($op1 as $key => $val) {
1658
-
if (!array_key_exists($key, $op2)) {
1659
-
return null; // uncomparable
1660
-
} elseif ($val < $op2[$key]) {
1661
-
return -1;
1662
-
} elseif ($val > $op2[$key]) {
1663
-
return 1;
1664
-
}
1665
-
}
1666
-
return 0; // $op1 == $op2
1667
-
}
1668
-
?>
1669
-
]]>
1670
-
</programlisting>
1671
-
</example>
1672
-
</para>
1673
-

1674
-
<warning>
1675
-
<title>Comparison of floating point numbers</title>
1676
-

1677
-
<para>
1678
-
Because of the way <type>float</type>s are represented internally, you
1679
-
should not test two <type>float</type>s for equality.
1680
-
</para>
1681
-

1682
-
<para>
1683
-
See the documentation for <type>float</type> for more information.
1684
-
</para>
1685
-
</warning>
1686
-

1687
-
<sect2 role="seealso">
1688
-
&reftitle.seealso;
1689
-
<para>
1690
-
<simplelist>
1691
-
<member><function>strcasecmp</function></member>
1692
-
<member><function>strcmp</function></member>
1693
-
<member><link linkend="language.operators.array">Array operators</link></member>
1694
-
<member><link linkend="language.types">Types</link></member>
1695
-
</simplelist>
1696
-
</para>
1697
-
</sect2>
1698
-

1699
-
<sect2 xml:id="language.operators.comparison.ternary">
1700
-
<title>Ternary Operator</title>
1701
-
<para>
1702
-
Another conditional operator is the "?:" (or ternary) operator.
1703
-
<example>
1704
-
<title>Assigning a default value</title>
1705
-
<programlisting role="php">
1706
-
<![CDATA[
1707
-
<?php
1708
-
// Example usage for: Ternary Operator
1709
-
$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];
1710
-

1711
-
// The above is identical to this if/else statement
1712
-
if (empty($_POST['action'])) {
1713
-
$action = 'default';
1714
-
} else {
1715
-
$action = $_POST['action'];
1716
-
}
1717
-

1718
-
?>
1719
-
]]>
1720
-
</programlisting>
1721
-
</example>
1722
-
The expression <literal>(expr1) ? (expr2) : (expr3)</literal>
1723
-
evaluates to <replaceable>expr2</replaceable> if
1724
-
<replaceable>expr1</replaceable> evaluates to &true;, and
1725
-
<replaceable>expr3</replaceable> if
1726
-
<replaceable>expr1</replaceable> evaluates to &false;.
1727
-
</para>
1728
-
<para>
1729
-
It is possible to leave out the middle part of the ternary operator.
1730
-
Expression <literal>expr1 ?: expr3</literal> returns
1731
-
<replaceable>expr1</replaceable> if <replaceable>expr1</replaceable>
1732
-
evaluates to &true;, and <replaceable>expr3</replaceable> otherwise.
1733
-
</para>
1734
-
<note>
1735
-
<simpara>
1736
-
Please note that the ternary operator is an expression, and that it
1737
-
doesn't evaluate to a variable, but to the result of an expression. This
1738
-
is important to know if you want to return a variable by reference.
1739
-
The statement <literal>return $var == 42 ? $a : $b;</literal> in a
1740
-
return-by-reference function will therefore not work and a warning is
1741
-
issued.
1742
-
</simpara>
1743
-
</note>
1744
-
<note>
1745
-
<para>
1746
-
It is recommended to avoid "stacking" ternary expressions.
1747
-
PHP's behaviour when using more than one ternary operator within a single
1748
-
statement is non-obvious compared to other languages.
1749
-
Indeed prior to PHP 8.0.0, ternary expressions were evaluated from left to
1750
-
right, instead of right to left like most other programming languages.
1751
-
<example>
1752
-
<title>Non-obvious Ternary Behaviour</title>
1753
-
<programlisting role="php">
1754
-
<![CDATA[
1755
-
<?php
1756
-
// on first glance, the following appears to output 'true'
1757
-
echo (true?'true':false?'t':'f');
1758
-

1759
-
// however, the actual output of the above is 't' prior to PHP 8.0.0
1760
-
// this is because ternary expressions are evaluated from left to right
1761
-

1762
-
// the following is a more obvious version of the same code as above
1763
-
echo ((true ? 'true' : false) ? 't' : 'f');
1764
-

1765
-
// here, one can see that the first expression is evaluated to 'true', which
1766
-
// in turn evaluates to (bool)true, thus returning the true branch of the
1767
-
// second ternary expression.
1768
-
?>
1769
-
]]>
1770
-
</programlisting>
1771
-
</example>
1772
-
</para>
1773
-
</note>
1774
-
</sect2>
1775
-
1776
-
<sect2 xml:id="language.operators.comparison.coalesce">
1777
-
<title>Null Coalescing Operator</title>
1778
-
<para>
1779
-
Further exists the "??" (or null coalescing) operator.
1780
-
<example>
1781
-
<title>Assigning a default value</title>
1782
-
<programlisting role="php">
1783
-
<![CDATA[
1784
-
<?php
1785
-
// Example usage for: Null Coalesce Operator
1786
-
$action = $_POST['action'] ?? 'default';
1787
-

1788
-
// The above is identical to this if/else statement
1789
-
if (isset($_POST['action'])) {
1790
-
$action = $_POST['action'];
1791
-
} else {
1792
-
$action = 'default';
1793
-
}
1794
-

1795
-
?>
1796
-
]]>
1797
-
</programlisting>
1798
-
</example>
1799
-
The expression <literal>(expr1) ?? (expr2)</literal> evaluates to
1800
-
<replaceable>expr2</replaceable> if <replaceable>expr1</replaceable> is
1801
-
&null;, and <replaceable>expr1</replaceable> otherwise.
1802
-
</para>
1803
-
<para>
1804
-
In particular, this operator does not emit a notice or warning if the left-hand side
1805
-
value does not exist, just like <function>isset</function>. This is especially
1806
-
useful on array keys.
1807
-
</para>
1808
-
<note>
1809
-
<simpara>
1810
-
Please note that the null coalescing operator is an expression, and that it
1811
-
doesn't evaluate to a variable, but to the result of an expression. This
1812
-
is important to know if you want to return a variable by reference.
1813
-
The statement <literal>return $foo ?? $bar;</literal> in a
1814
-
return-by-reference function will therefore not work and a warning is
1815
-
issued.
1816
-
</simpara>
1817
-
</note>
1818
-
<note>
1819
-
<para>
1820
-
Please note that the null coalescing operator allows for simple nesting:
1821
-
<example>
1822
-
<title>Nesting null coalescing operator</title>
1823
-
<programlisting role="php">
1824
-
<![CDATA[
1825
-
<?php
1826
-

1827
-
$foo = null;
1828
-
$bar = null;
1829
-
$baz = 1;
1830
-
$qux = 2;
1831
-

1832
-
echo $foo ?? $bar ?? $baz ?? $qux; // outputs 1
1833
-

1834
-
?>
1835
-
]]>
1836
-
</programlisting>
1837
-
</example>
1838
-
</para>
1839
-
</note>
1840
-
</sect2>
1841
-
</sect1>
1842
-

1843
-
<sect1 xml:id="language.operators.errorcontrol">
1844
-
<title>Error Control Operators</title>
1845
-
<simpara>
1846
-
PHP supports one error control operator: the at sign (@). When
1847
-
prepended to an expression in PHP, any error messages that might
1848
-
be generated by that expression will be ignored.
1849
-
</simpara>
1850
-
<simpara>
1851
-
If you have set a custom error handler function with
1852
-
<function>set_error_handler</function> then it will still get
1853
-
called, but this custom error handler can (and should) call <function>error_reporting</function>
1854
-
which will return 0 when the call that triggered the error was preceded by an @.
1855
-
</simpara>
1856
-
<simpara>
1857
-
Any error message generated by the expression is available in the <literal>"message"</literal>
1858
-
element of the array returned by <function>error_get_last</function>.
1859
-
The result of that function will change on each error, so it needs to be checked early.
1860
-
</simpara>
1861
-
<para>
1862
-
<informalexample>
1863
-
<programlisting role="php">
1864
-
<![CDATA[
1865
-
<?php
1866
-
/* Intentional file error */
1867
-
$my_file = @file ('non_existent_file') or
1868
-
die ("Failed opening file: error was '" . error_get_last()['message'] . "'");
1869
-

1870
-
// this works for any expression, not just functions:
1871
-
$value = @$cache[$key];
1872
-
// will not issue a notice if the index $key doesn't exist.
1873
-

1874
-
?>
1875
-
]]>
1876
-
</programlisting>
1877
-
</informalexample>
1878
-
</para>
1879
-
<note>
1880
-
<simpara>
1881
-
The @-operator works only on
1882
-
<link linkend="language.expressions">expressions</link>. A simple rule
1883
-
of thumb is: if you can take the value of something, you can prepend
1884
-
the @ operator to it. For instance, you can prepend it to variables,
1885
-
function and <function>include</function> calls, constants, and
1886
-
so forth. You cannot prepend it to function or class definitions,
1887
-
or conditional structures such as <literal>if</literal> and
1888
-
&foreach;, and so forth.
1889
-
</simpara>
1890
-
</note>
1891
-
<warning>
1892
-
<para>
1893
-
Currently the "@" error-control operator prefix will even disable
1894
-
error reporting for critical errors that will terminate script
1895
-
execution. Among other things, this means that if you use "@" to
1896
-
suppress errors from a certain function and either it isn't
1897
-
available or has been mistyped, the script will die right there
1898
-
with no indication as to why.
1899
-
</para>
1900
-
</warning>
1901
-

1902
-
<sect2 role="seealso">
1903
-
&reftitle.seealso;
1904
-
<para>
1905
-
<simplelist>
1906
-
<member><function>error_reporting</function></member>
1907
-
<member><link linkend="ref.errorfunc">Error Handling and Logging functions</link></member>
1908
-
</simplelist>
1909
-
</para>
1910
-
</sect2>
1911
-
</sect1>
1912
-

1913
-
<sect1 xml:id="language.operators.execution">
1914
-
<title>Execution Operators</title>
1915
-
<para>
1916
-
PHP supports one execution operator: backticks (``). Note that
1917
-
these are not single-quotes! PHP will attempt to execute the
1918
-
contents of the backticks as a shell command; the output will be
1919
-
returned (i.e., it won't simply be dumped to output; it can be
1920
-
assigned to a variable). Use of the backtick operator is identical
1921
-
to <function>shell_exec</function>.
1922
-
<informalexample>
1923
-
<programlisting role="php">
1924
-
<![CDATA[
1925
-
<?php
1926
-
$output = `ls -al`;
1927
-
echo "<pre>$output</pre>";
1928
-
?>
1929
-
]]>
1930
-
</programlisting>
1931
-
</informalexample>
1932
-
</para>
1933
-
<note>
1934
-
<para>
1935
-
The backtick operator is disabled when
1936
-
<function>shell_exec</function> is disabled.
1937
-
</para>
1938
-
</note>
1939
-
<note>
1940
-
<para>
1941
-
Unlike some other languages, backticks have no special meaning
1942
-
within double-quoted strings.
1943
-
</para>
1944
-
</note>
1945
-

1946
-
<sect2 role="seealso">
1947
-
&reftitle.seealso;
1948
-
<para>
1949
-
<simplelist>
1950
-
<member><link linkend="ref.exec">Program Execution functions</link></member>
1951
-
<member><function>popen</function></member>
1952
-
<member><function>proc_open</function></member>
1953
-
<member><link linkend="features.commandline">Using PHP from the commandline</link></member>
1954
-
</simplelist>
1955
-
</para>
1956
-
</sect2>
1957
-
</sect1>
1958
-

1959
-
<sect1 xml:id="language.operators.increment">
1960
-
<title>Incrementing/Decrementing Operators</title>
1961
-
<para>
1962
-
PHP supports C-style pre- and post-increment and decrement
1963
-
operators.
1964
-
</para>
1965
-
<note>
1966
-
<simpara>
1967
-
The increment/decrement operators only affect numbers and strings.
1968
-
Arrays, objects, booleans and resources are not affected.
1969
-
Decrementing &null; values has no effect too, but incrementing them
1970
-
results in <literal>1</literal>.
1971
-
</simpara>
1972
-
</note>
1973
-
<table>
1974
-
<title>Increment/decrement Operators</title>
1975
-
<tgroup cols="3">
1976
-
<thead>
1977
-
<row>
1978
-
<entry>Example</entry>
1979
-
<entry>Name</entry>
1980
-
<entry>Effect</entry>
1981
-
</row>
1982
-
</thead>
1983
-
<tbody>
1984
-
<row>
1985
-
<entry>++$a</entry>
1986
-
<entry>Pre-increment</entry>
1987
-
<entry>Increments <varname>$a</varname> by one, then returns <varname>$a</varname>.</entry>
1988
-
</row>
1989
-
<row>
1990
-
<entry>$a++</entry>
1991
-
<entry>Post-increment</entry>
1992
-
<entry>Returns <varname>$a</varname>, then increments <varname>$a</varname> by one.</entry>
1993
-
</row>
1994
-
<row>
1995
-
<entry>--$a</entry>
1996
-
<entry>Pre-decrement</entry>
1997
-
<entry>Decrements <varname>$a</varname> by one, then returns <varname>$a</varname>.</entry>
1998
-
</row>
1999
-
<row>
2000
-
<entry>$a--</entry>
2001
-
<entry>Post-decrement</entry>
2002
-
<entry>Returns <varname>$a</varname>, then decrements <varname>$a</varname> by one.</entry>
2003
-
</row>
2004
-
</tbody>
2005
-
</tgroup>
2006
-
</table>
2007
-
<para>
2008
-
Here's a simple example script:
2009
-
<informalexample>
2010
-
<programlisting role="php">
2011
-
<![CDATA[
2012
-
<?php
2013
-
echo "<h3>Postincrement</h3>";
2014
-
$a = 5;
2015
-
echo "Should be 5: " . $a++ . "<br />\n";
2016
-
echo "Should be 6: " . $a . "<br />\n";
2017
-

2018
-
echo "<h3>Preincrement</h3>";
2019
-
$a = 5;
2020
-
echo "Should be 6: " . ++$a . "<br />\n";
2021
-
echo "Should be 6: " . $a . "<br />\n";
2022
-

2023
-
echo "<h3>Postdecrement</h3>";
2024
-
$a = 5;
2025
-
echo "Should be 5: " . $a-- . "<br />\n";
2026
-
echo "Should be 4: " . $a . "<br />\n";
2027
-

2028
-
echo "<h3>Predecrement</h3>";
2029
-
$a = 5;
2030
-
echo "Should be 4: " . --$a . "<br />\n";
2031
-
echo "Should be 4: " . $a . "<br />\n";
2032
-
?>
2033
-
]]>
2034
-
</programlisting>
2035
-
</informalexample>
2036
-
</para>
2037
-
<para>
2038
-
PHP follows Perl's convention when dealing with arithmetic operations
2039
-
on character variables and not C's. For example, in PHP and Perl
2040
-
<literal>$a = 'Z'; $a++;</literal> turns <literal>$a</literal> into <literal>'AA'</literal>, while in C
2041
-
<literal>a = 'Z'; a++;</literal> turns <literal>a</literal> into <literal>'['</literal>
2042
-
(ASCII value of <literal>'Z'</literal> is 90, ASCII value of <literal>'['</literal> is 91).
2043
-
Note that character variables can be incremented but not decremented and
2044
-
even so only plain ASCII alphabets and digits (a-z, A-Z and 0-9) are supported.
2045
-
Incrementing/decrementing other character variables has no effect, the
2046
-
original string is unchanged.
2047
-
<example>
2048
-
<title>Arithmetic Operations on Character Variables</title>
2049
-
<programlisting role="php">
2050
-
<![CDATA[
2051
-
<?php
2052
-
echo '== Alphabets ==' . PHP_EOL;
2053
-
$s = 'W';
2054
-
for ($n=0; $n<6; $n++) {
2055
-
echo ++$s . PHP_EOL;
2056
-
}
2057
-
// Digit characters behave differently
2058
-
echo '== Digits ==' . PHP_EOL;
2059
-
$d = 'A8';
2060
-
for ($n=0; $n<6; $n++) {
2061
-
echo ++$d . PHP_EOL;
2062
-
}
2063
-
$d = 'A08';
2064
-
for ($n=0; $n<6; $n++) {
2065
-
echo ++$d . PHP_EOL;
2066
-
}
2067
-
?>
2068
-
]]>
2069
-
</programlisting>
2070
-
&example.outputs;
2071
-
<screen>
2072
-
<![CDATA[
2073
-
== Characters ==
2074
-
X
2075
-
Y
2076
-
Z
2077
-
AA
2078
-
AB
2079
-
AC
2080
-
== Digits ==
2081
-
A9
2082
-
B0
2083
-
B1
2084
-
B2
2085
-
B3
2086
-
B4
2087
-
A09
2088
-
A10
2089
-
A11
2090
-
A12
2091
-
A13
2092
-
A14
2093
-
]]>
2094
-
</screen>
2095
-
</example>
2096
-
</para>
2097
-
<para>
2098
-
Incrementing or decrementing booleans has no effect.
2099
-
</para>
2100
-
</sect1>
2101
-

2102
-
<sect1 xml:id="language.operators.logical">
2103
-
<title>Logical Operators</title>
2104
-

2105
-
<table>
2106
-
<title>Logical Operators</title>
2107
-
<tgroup cols="3">
2108
-
<thead>
2109
-
<row>
2110
-
<entry>Example</entry>
2111
-
<entry>Name</entry>
2112
-
<entry>Result</entry>
2113
-
</row>
2114
-
</thead>
2115
-
<tbody>
2116
-
<row>
2117
-
<entry>$a and $b</entry>
2118
-
<entry>And</entry>
2119
-
<entry>&true; if both <varname>$a</varname> and <varname>$b</varname> are &true;.</entry>
2120
-
</row>
2121
-
<row>
2122
-
<entry>$a or $b</entry>
2123
-
<entry>Or</entry>
2124
-
<entry>&true; if either <varname>$a</varname> or <varname>$b</varname> is &true;.</entry>
2125
-
</row>
2126
-
<row>
2127
-
<entry>$a xor $b</entry>
2128
-
<entry>Xor</entry>
2129
-
<entry>&true; if either <varname>$a</varname> or <varname>$b</varname> is &true;, but not both.</entry>
2130
-
</row>
2131
-
<row>
2132
-
<entry>! $a</entry>
2133
-
<entry>Not</entry>
2134
-
<entry>&true; if <varname>$a</varname> is not &true;.</entry>
2135
-
</row>
2136
-
<row>
2137
-
<entry>$a &amp;&amp; $b</entry>
2138
-
<entry>And</entry>
2139
-
<entry>&true; if both <varname>$a</varname> and <varname>$b</varname> are &true;.</entry>
2140
-
</row>
2141
-
<row>
2142
-
<entry>$a || $b</entry>
2143
-
<entry>Or</entry>
2144
-
<entry>&true; if either <varname>$a</varname> or <varname>$b</varname> is &true;.</entry>
2145
-
</row>
2146
-
</tbody>
2147
-
</tgroup>
2148
-
</table>
2149
-
<simpara>
2150
-
The reason for the two different variations of "and" and "or"
2151
-
operators is that they operate at different precedences. (See
2152
-
<link linkend="language.operators.precedence">Operator
2153
-
Precedence</link>.)
2154
-
</simpara>
2155
-
<example>
2156
-
<title>Logical operators illustrated</title>
2157
-
<programlisting role="php">
2158
-
<![CDATA[
2159
-
<?php
2160
-

2161
-
// --------------------
2162
-
// foo() will never get called as those operators are short-circuit
2163
-

2164
-
$a = (false && foo());
2165
-
$b = (true || foo());
2166
-
$c = (false and foo());
2167
-
$d = (true or foo());
2168
-

2169
-
// --------------------
2170
-
// "||" has a greater precedence than "or"
2171
-

2172
-
// The result of the expression (false || true) is assigned to $e
2173
-
// Acts like: ($e = (false || true))
2174
-
$e = false || true;
2175
-

2176
-
// The constant false is assigned to $f before the "or" operation occurs
2177
-
// Acts like: (($f = false) or true)
2178
-
$f = false or true;
2179
-

2180
-
var_dump($e, $f);
2181
-

2182
-
// --------------------
2183
-
// "&&" has a greater precedence than "and"
2184
-

2185
-
// The result of the expression (true && false) is assigned to $g
2186
-
// Acts like: ($g = (true && false))
2187
-
$g = true && false;
2188
-

2189
-
// The constant true is assigned to $h before the "and" operation occurs
2190
-
// Acts like: (($h = true) and false)
2191
-
$h = true and false;
2192
-

2193
-
var_dump($g, $h);
2194
-
?>
2195
-
]]>
2196
-
</programlisting>
2197
-
&example.outputs.similar;
2198
-
<screen>
2199
-
<![CDATA[
2200
-
bool(true)
2201
-
bool(false)
2202
-
bool(false)
2203
-
bool(true)
2204
-
]]>
2205
-
</screen>
2206
-
</example>
2207
-
</sect1>
2208
-

2209
-
<sect1 xml:id="language.operators.string">
2210
-
<title>String Operators</title>
2211
-
<simpara>
2212
-
There are two <type>string</type> operators. The first is the
2213
-
concatenation operator ('.'), which returns the concatenation of its
2214
-
right and left arguments. The second is the concatenating assignment
2215
-
operator ('<literal>.=</literal>'), which appends the argument on the right side to
2216
-
the argument on the left side. Please read <link
2217
-
linkend="language.operators.assignment">Assignment
2218
-
Operators</link> for more information.
2219
-
</simpara>
2220
-

2221
-
<para>
2222
-
<informalexample>
2223
-
<programlisting role="php">
2224
-
<![CDATA[
2225
-
<?php
2226
-
$a = "Hello ";
2227
-
$b = $a . "World!"; // now $b contains "Hello World!"
2228
-

2229
-
$a = "Hello ";
2230
-
$a .= "World!"; // now $a contains "Hello World!"
2231
-
?>
2232
-
]]>
2233
-
</programlisting>
2234
-
</informalexample>
2235
-
</para>
2236
-

2237
-
<sect2 role="seealso">
2238
-
&reftitle.seealso;
2239
-
<para>
2240
-
<simplelist>
2241
-
<member><link linkend="language.types.string">String type</link></member>
2242
-
<member><link linkend="ref.strings">String functions</link></member>
2243
-
</simplelist>
2244
-
</para>
2245
-
</sect2>
2246
-
</sect1>
2247
-

2248
-
<sect1 xml:id="language.operators.array">
2249
-
<title>Array Operators</title>
2250
-
<table>
2251
-
<title>Array Operators</title>
2252
-
<tgroup cols="3">
2253
-
<thead>
2254
-
<row>
2255
-
<entry>Example</entry>
2256
-
<entry>Name</entry>
2257
-
<entry>Result</entry>
2258
-
</row>
2259
-
</thead>
2260
-
<tbody>
2261
-
<row>
2262
-
<entry>$a + $b</entry>
2263
-
<entry>Union</entry>
2264
-
<entry>Union of <varname>$a</varname> and <varname>$b</varname>.</entry>
2265
-
</row>
2266
-
<row>
2267
-
<entry>$a == $b</entry>
2268
-
<entry>Equality</entry>
2269
-
<entry>&true; if <varname>$a</varname> and <varname>$b</varname> have the same key/value pairs.</entry>
2270
-
</row>
2271
-
<row>
2272
-
<entry>$a === $b</entry>
2273
-
<entry>Identity</entry>
2274
-
<entry>&true; if <varname>$a</varname> and <varname>$b</varname> have the same key/value pairs in the same
2275
-
order and of the same types.</entry>
2276
-
</row>
2277
-
<row>
2278
-
<entry>$a != $b</entry>
2279
-
<entry>Inequality</entry>
2280
-
<entry>&true; if <varname>$a</varname> is not equal to <varname>$b</varname>.</entry>
2281
-
</row>
2282
-
<row>
2283
-
<entry>$a &lt;&gt; $b</entry>
2284
-
<entry>Inequality</entry>
2285
-
<entry>&true; if <varname>$a</varname> is not equal to <varname>$b</varname>.</entry>
2286
-
</row>
2287
-
<row>
2288
-
<entry>$a !== $b</entry>
2289
-
<entry>Non-identity</entry>
2290
-
<entry>&true; if <varname>$a</varname> is not identical to <varname>$b</varname>.</entry>
2291
-
</row>
2292
-
</tbody>
2293
-
</tgroup>
2294
-
</table>
2295
-
<para>
2296
-
The <literal>+</literal> operator returns the right-hand array appended
2297
-
to the left-hand array; for keys that exist in both arrays, the elements
2298
-
from the left-hand array will be used, and the matching elements from the
2299
-
right-hand array will be ignored.
2300
-
</para>
2301
-
<para>
2302
-
<informalexample>
2303
-
<programlisting role="php">
2304
-
<![CDATA[
2305
-
<?php
2306
-
$a = array("a" => "apple", "b" => "banana");
2307
-
$b = array("a" => "pear", "b" => "strawberry", "c" => "cherry");
2308
-

2309
-
$c = $a + $b; // Union of $a and $b
2310
-
echo "Union of \$a and \$b: \n";
2311
-
var_dump($c);
2312
-

2313
-
$c = $b + $a; // Union of $b and $a
2314
-
echo "Union of \$b and \$a: \n";
2315
-
var_dump($c);
2316
-

2317
-
$a += $b; // Union of $a += $b is $a and $b
2318
-
echo "Union of \$a += \$b: \n";
2319
-
var_dump($a);
2320
-
?>
2321
-
]]>
2322
-
</programlisting>
2323
-
</informalexample>
2324
-
When executed, this script will print the following:
2325
-
<screen role="php">
2326
-
<![CDATA[
2327
-
Union of $a and $b:
2328
-
array(3) {
2329
-
["a"]=>
2330
-
string(5) "apple"
2331
-
["b"]=>
2332
-
string(6) "banana"
2333
-
["c"]=>
2334
-
string(6) "cherry"
2335
-
}
2336
-
Union of $b and $a:
2337
-
array(3) {
2338
-
["a"]=>
2339
-
string(4) "pear"
2340
-
["b"]=>
2341
-
string(10) "strawberry"
2342
-
["c"]=>
2343
-
string(6) "cherry"
2344
-
}
2345
-
Union of $a += $b:
2346
-
array(3) {
2347
-
["a"]=>
2348
-
string(5) "apple"
2349
-
["b"]=>
2350
-
string(6) "banana"
2351
-
["c"]=>
2352
-
string(6) "cherry"
2353
-
}
2354
-
]]>
2355
-
</screen>
2356
-
</para>
2357
-
<para>
2358
-
Elements of arrays are equal for the comparison if they have the
2359
-
same key and value.
2360
-
</para>
2361
-
<para>
2362
-
<example>
2363
-
<title>Comparing arrays</title>
2364
-
<programlisting role="php">
2365
-
<![CDATA[
2366
-
<?php
2367
-
$a = array("apple", "banana");
2368
-
$b = array(1 => "banana", "0" => "apple");
2369
-

2370
-
var_dump($a == $b); // bool(true)
2371
-
var_dump($a === $b); // bool(false)
2372
-
?>
2373
-
]]>
2374
-
</programlisting>
2375
-
</example>
2376
-
</para>
2377
-

2378
-
<sect2 role="seealso">
2379
-
&reftitle.seealso;
2380
-
<para>
2381
-
<simplelist>
2382
-
<member><link linkend="language.types.array">Array type</link></member>
2383
-
<member><link linkend="ref.array">Array functions</link></member>
2384
-
</simplelist>
2385
-
</para>
2386
-
</sect2>
2387
-
</sect1>
2388
-

2389
-
<sect1 xml:id="language.operators.type">
2390
-
<title>Type Operators</title>
2391
-
<para>
2392
-
<literal>instanceof</literal> is used to determine whether a PHP variable
2393
-
is an instantiated object of a certain
2394
-
<link linkend="language.oop5.basic.class">class</link>:
2395
-
<example>
2396
-
<title>Using <literal>instanceof</literal> with classes</title>
2397
-
<programlisting role="php">
2398
-
<![CDATA[
2399
-
<?php
2400
-
class MyClass
2401
-
{
2402
-
}
2403
-

2404
-
class NotMyClass
2405
-
{
2406
-
}
2407
-
$a = new MyClass;
2408
-

2409
-
var_dump($a instanceof MyClass);
2410
-
var_dump($a instanceof NotMyClass);
2411
-
?>
2412
-
]]>
2413
-
</programlisting>
2414
-
&example.outputs;
2415
-
<screen>
2416
-
<![CDATA[
2417
-
bool(true)
2418
-
bool(false)
2419
-
]]>
2420
-
</screen>
2421
-
</example>
2422
-
</para>
2423
-
<para>
2424
-
<literal>instanceof</literal> can also be used to determine whether a variable
2425
-
is an instantiated object of a class that inherits from a parent class:
2426
-
<example>
2427
-
<title>Using <literal>instanceof</literal> with inherited classes</title>
2428
-
<programlisting role="php">
2429
-
<![CDATA[
2430
-
<?php
2431
-
class ParentClass
2432
-
{
2433
-
}
2434
-

2435
-
class MyClass extends ParentClass
2436
-
{
2437
-
}
2438
-

2439
-
$a = new MyClass;
2440
-

2441
-
var_dump($a instanceof MyClass);
2442
-
var_dump($a instanceof ParentClass);
2443
-
?>
2444
-
]]>
2445
-
</programlisting>
2446
-
&example.outputs;
2447
-
<screen>
2448
-
<![CDATA[
2449
-
bool(true)
2450
-
bool(true)
2451
-
]]>
2452
-
</screen>
2453
-
</example>
2454
-
</para>
2455
-
<para>
2456
-
To check if an object is <emphasis>not</emphasis> an instanceof a class, the
2457
-
<link linkend="language.operators.logical">logical <literal>not</literal>
2458
-
operator</link> can be used.
2459
-
<example>
2460
-
<title>Using <literal>instanceof</literal> to check if object is <emphasis>not</emphasis> an
2461
-
instanceof a class</title>
2462
-
<programlisting role="php">
2463
-
<![CDATA[
2464
-
<?php
2465
-
class MyClass
2466
-
{
2467
-
}
2468
-

2469
-
$a = new MyClass;
2470
-
var_dump(!($a instanceof stdClass));
2471
-
?>
2472
-
]]>
2473
-
</programlisting>
2474
-
&example.outputs;
2475
-
<screen>
2476
-
<![CDATA[
2477
-
bool(true)
2478
-
]]>
2479
-
</screen>
2480
-
</example>
2481
-
</para>
2482
-
<para>
2483
-
Lastly, <literal>instanceof</literal> can also be used to determine whether
2484
-
a variable is an instantiated object of a class that implements an
2485
-
<link linkend="language.oop5.interfaces">interface</link>:
2486
-
<example>
2487
-
<title>Using <literal>instanceof</literal> with interfaces</title>
2488
-
<programlisting role="php">
2489
-
<![CDATA[
2490
-
<?php
2491
-
interface MyInterface
2492
-
{
2493
-
}
2494
-

2495
-
class MyClass implements MyInterface
2496
-
{
2497
-
}
2498
-

2499
-
$a = new MyClass;
2500
-

2501
-
var_dump($a instanceof MyClass);
2502
-
var_dump($a instanceof MyInterface);
2503
-
?>
2504
-
]]>
2505
-
</programlisting>
2506
-
&example.outputs;
2507
-
<screen>
2508
-
<![CDATA[
2509
-
bool(true)
2510
-
bool(true)
2511
-
]]>
2512
-
</screen>
2513
-
</example>
2514
-
</para>
2515
-
<para>
2516
-
Although <literal>instanceof</literal> is usually used with a literal classname,
2517
-
it can also be used with another object or a string variable:
2518
-
<example>
2519
-
<title>Using <literal>instanceof</literal> with other variables</title>
2520
-
<programlisting role="php">
2521
-
<![CDATA[
2522
-
<?php
2523
-
interface MyInterface
2524
-
{
2525
-
}
2526
-

2527
-
class MyClass implements MyInterface
2528
-
{
2529
-
}
2530
-

2531
-
$a = new MyClass;
2532
-
$b = new MyClass;
2533
-
$c = 'MyClass';
2534
-
$d = 'NotMyClass';
2535
-

2536
-
var_dump($a instanceof $b); // $b is an object of class MyClass
2537
-
var_dump($a instanceof $c); // $c is a string 'MyClass'
2538
-
var_dump($a instanceof $d); // $d is a string 'NotMyClass'
2539
-
?>
2540
-
]]>
2541
-
</programlisting>
2542
-
&example.outputs;
2543
-
<screen>
2544
-
<![CDATA[
2545
-
bool(true)
2546
-
bool(true)
2547
-
bool(false)
2548
-
]]>
2549
-
</screen>
2550
-
</example>
2551
-
</para>
2552
-
<para>
2553
-
instanceof does not throw any error if the variable being tested is not
2554
-
an object, it simply returns &false;. Constants, however, were not allowed
2555
-
prior to PHP 7.3.0.
2556
-
<example>
2557
-
<title>Using <literal>instanceof</literal> to test other variables</title>
2558
-
<programlisting role="php">
2559
-
<![CDATA[
2560
-
<?php
2561
-
$a = 1;
2562
-
$b = NULL;
2563
-
$c = imagecreate(5, 5);
2564
-
var_dump($a instanceof stdClass); // $a is an integer
2565
-
var_dump($b instanceof stdClass); // $b is NULL
2566
-
var_dump($c instanceof stdClass); // $c is a resource
2567
-
var_dump(FALSE instanceof stdClass);
2568
-
?>
2569
-
]]>
2570
-
</programlisting>
2571
-
&example.outputs;
2572
-
<screen>
2573
-
<![CDATA[
2574
-
bool(false)
2575
-
bool(false)
2576
-
bool(false)
2577
-
PHP Fatal error: instanceof expects an object instance, constant given
2578
-
]]>
2579
-
</screen>
2580
-
</example>
2581
-
</para>
2582
-
<para>
2583
-
As of PHP 7.3.0, constants are allowed on the left-hand-side of the
2584
-
<literal>instanceof</literal> operator.
2585
-
<example>
2586
-
<title>Using <literal>instanceof</literal> to test constants</title>
2587
-
<programlisting role="php">
2588
-
<![CDATA[
2589
-
<?php
2590
-
var_dump(FALSE instanceof stdClass);
2591
-
?>
2592
-
]]>
2593
-
</programlisting>
2594
-
&example.outputs.73;
2595
-
<screen>
2596
-
<![CDATA[
2597
-
bool(false)
2598
-
]]>
2599
-
</screen>
2600
-
</example>
2601
-
</para>
2602
-
<simpara>
2603
-
The <literal>instanceof</literal> operator has a functional variant
2604
-
with the <function>is_a</function> function.
2605
-
</simpara>
2606
-

2607
-
<sect2 role="seealso">
2608
-
&reftitle.seealso;
2609
-
<para>
2610
-
<simplelist>
2611
-
<member><function>get_class</function></member>
2612
-
<member><function>is_a</function></member>
2613
-
</simplelist>
2614
-
</para>
2615
-
</sect2>
2616
-
</sect1>
2617
-
</chapter>
2618
-

3
+
<chapter xml:id="language.operators" xmlns="http://docbook.org/ns/docbook">
4
+
<title>Operators</title>
5
+
<simpara>
6
+
An operator is something that takes one or more values (or
7
+
expressions, in programming jargon) and yields another value (so that the
8
+
construction itself becomes an expression).
9
+
</simpara>
10
+
<para>
11
+
Operators can be grouped according to the number of values they take. Unary
12
+
operators take only one value, for example <literal>!</literal> (the
13
+
<link linkend="language.operators.logical">logical not operator</link>) or
14
+
<literal>++</literal> (the
15
+
<link linkend="language.operators.increment">increment operator</link>).
16
+
Binary operators take two values, such as the familiar
17
+
<link linkend="language.operators.arithmetic">arithmetical operators</link>
18
+
<literal>+</literal> (plus) and <literal>-</literal> (minus), and the
19
+
majority of PHP operators fall into this category. Finally, there is a
20
+
single <link linkend="language.operators.comparison.ternary">ternary
21
+
operator</link>, <literal>? :</literal>, which takes three values; this is
22
+
usually referred to simply as "the ternary operator" (although it could
23
+
perhaps more properly be called the conditional operator).
24
+
</para>
25
+
<para>
26
+
A full list of PHP operators follows in the section
27
+
<link linkend="language.operators.precedence">Operator Precedence</link>.
28
+
The section also explains operator precedence and associativity, which govern
29
+
exactly how expressions containing several different operators are
30
+
evaluated.
31
+
</para>
32
+

33
+
&language.operators.precedence;
34
+
&language.operators.arithmetic;
35
+
&language.operators.increment;
36
+
&language.operators.assignment;
37
+
&language.operators.bitwise;
38
+
&language.operators.comparison;
39
+
&language.operators.errorcontrol;
40
+
&language.operators.execution;
41
+
&language.operators.logical;
42
+
&language.operators.string;
43
+
&language.operators.array;
44
+
&language.operators.type;
45
+

46
+
</chapter>
2619
47
<!-- Keep this comment at the end of the file
2620
48
Local variables:
2621
49
mode: sgml
2622
50