language/operators.xml
52407313885d27a4e891e08dd2e2481bcc39e244
...
...
@@ -1,2440 +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>non-associative</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>left</entry>
90
-
<entry><literal>[</literal></entry>
91
-
<entry><function>array</function></entry>
92
-
</row>
93
-
<row>
94
-
<entry>right</entry>
95
-
<entry><literal>**</literal></entry>
96
-
<entry><link linkend="language.operators.arithmetic">arithmetic</link></entry>
97
-
</row>
98
-
<row>
99
-
<entry>right</entry>
100
-
<entry>
101
-
<literal>++</literal>
102
-
<literal>--</literal>
103
-
<literal>~</literal>
104
-
<literal>(int)</literal>
105
-
<literal>(float)</literal>
106
-
<literal>(string)</literal>
107
-
<literal>(array)</literal>
108
-
<literal>(object)</literal>
109
-
<literal>(bool)</literal>
110
-
<literal>@</literal>
111
-
</entry>
112
-
<entry>
113
-
<link linkend="language.types">types</link> and <link linkend="language.operators.increment">increment/decrement</link>
114
-
</entry>
115
-
</row>
116
-
<row>
117
-
<entry>non-associative</entry>
118
-
<entry><literal>instanceof</literal></entry>
119
-
<entry>
120
-
<link linkend="language.types">types</link>
121
-
</entry>
122
-
</row>
123
-
<row>
124
-
<entry>right</entry>
125
-
<entry><literal>!</literal></entry>
126
-
<entry>
127
-
<link linkend="language.operators.logical">logical</link>
128
-
</entry>
129
-
</row>
130
-
<row>
131
-
<entry>left</entry>
132
-
<entry>
133
-
<literal>*</literal>
134
-
<literal>/</literal>
135
-
<literal>%</literal>
136
-
</entry>
137
-
<entry>
138
-
<link linkend="language.operators.arithmetic">arithmetic</link>
139
-
</entry>
140
-
</row>
141
-
<row>
142
-
<entry>left</entry>
143
-
<entry>
144
-
<literal>+</literal>
145
-
<literal>-</literal>
146
-
<literal>.</literal>
147
-
</entry>
148
-
<entry>
149
-
<link linkend="language.operators.arithmetic">arithmetic</link>&listendand;
150
-
<link linkend="language.operators.string">string</link></entry>
151
-
</row>
152
-
<row>
153
-
<entry>left</entry>
154
-
<entry>
155
-
<literal>&lt;&lt;</literal>
156
-
<literal>&gt;&gt;</literal>
157
-
</entry>
158
-
<entry>
159
-
<link linkend="language.operators.bitwise">bitwise</link>
160
-
</entry>
161
-
</row>
162
-
<row>
163
-
<entry>non-associative</entry>
164
-
<entry>
165
-
<literal>&lt;</literal>
166
-
<literal>&lt;=</literal>
167
-
<literal>&gt;</literal>
168
-
<literal>&gt;=</literal>
169
-
</entry>
170
-
<entry>
171
-
<link linkend="language.operators.comparison">comparison</link>
172
-
</entry>
173
-
</row>
174
-
<row>
175
-
<entry>non-associative</entry>
176
-
<entry>
177
-
<literal>==</literal>
178
-
<literal>!=</literal>
179
-
<literal>===</literal>
180
-
<literal>!==</literal>
181
-
<literal>&lt;&gt;</literal>
182
-
<literal>&lt;=&gt;</literal>
183
-
</entry>
184
-
<entry>
185
-
<link linkend="language.operators.comparison">comparison</link>
186
-
</entry>
187
-
</row>
188
-
<row>
189
-
<entry>left</entry>
190
-
<entry><literal>&amp;</literal></entry>
191
-
<entry>
192
-
<link linkend="language.operators.bitwise">bitwise</link>&listendand;
193
-
<link linkend="language.references">references</link></entry>
194
-
</row>
195
-
<row>
196
-
<entry>left</entry>
197
-
<entry><literal>^</literal></entry>
198
-
<entry>
199
-
<link linkend="language.operators.bitwise">bitwise</link>
200
-
</entry>
201
-
</row>
202
-
<row>
203
-
<entry>left</entry>
204
-
<entry><literal>|</literal></entry>
205
-
<entry>
206
-
<link linkend="language.operators.bitwise">bitwise</link>
207
-
</entry>
208
-
</row>
209
-
<row>
210
-
<entry>left</entry>
211
-
<entry><literal>&amp;&amp;</literal></entry>
212
-
<entry>
213
-
<link linkend="language.operators.logical">logical</link>
214
-
</entry>
215
-
</row>
216
-
<row>
217
-
<entry>left</entry>
218
-
<entry><literal>||</literal></entry>
219
-
<entry>
220
-
<link linkend="language.operators.logical">logical</link>
221
-
</entry>
222
-
</row>
223
-
<row>
224
-
<entry>right</entry>
225
-
<entry><literal>??</literal></entry>
226
-
<entry>
227
-
<link linkend="language.operators.comparison">comparison</link>
228
-
</entry>
229
-
</row>
230
-
<row>
231
-
<entry>left</entry>
232
-
<entry><literal>? :</literal></entry>
233
-
<entry>
234
-
<link linkend="language.operators.comparison.ternary">ternary</link>
235
-
</entry>
236
-
</row>
237
-
<row>
238
-
<entry>right</entry>
239
-
<entry>
240
-
<literal>=</literal>
241
-
<literal>+=</literal>
242
-
<literal>-=</literal>
243
-
<literal>*=</literal>
244
-
<literal>**=</literal>
245
-
<literal>/=</literal>
246
-
<literal>.=</literal>
247
-
<literal>%=</literal>
248
-
<literal>&amp;=</literal>
249
-
<literal>|=</literal>
250
-
<literal>^=</literal>
251
-
<literal>&lt;&lt;=</literal>
252
-
<literal>&gt;&gt;=</literal>
253
-
</entry>
254
-
<entry>
255
-
<link linkend="language.operators.assignment">assignment</link>
256
-
</entry>
257
-
</row>
258
-
<row>
259
-
<entry>left</entry>
260
-
<entry><literal>and</literal></entry>
261
-
<entry>
262
-
<link linkend="language.operators.logical">logical</link>
263
-
</entry>
264
-
</row>
265
-
<row>
266
-
<entry>left</entry>
267
-
<entry><literal>xor</literal></entry>
268
-
<entry>
269
-
<link linkend="language.operators.logical">logical</link>
270
-
</entry>
271
-
</row>
272
-
<row>
273
-
<entry>left</entry>
274
-
<entry><literal>or</literal></entry>
275
-
<entry>
276
-
<link linkend="language.operators.logical">logical</link>
277
-
</entry>
278
-
</row>
279
-
</tbody>
280
-
</tgroup>
281
-
</table>
282
-
</para>
283
-
<para>
284
-
<example>
285
-
<title>Associativity</title>
286
-
<programlisting role="php">
287
-
<![CDATA[
288
-
<?php
289
-
$a = 3 * 3 % 5; // (3 * 3) % 5 = 4
290
-
// ternary operator associativity differs from C/C++
291
-
$a = true ? 0 : true ? 1 : 2; // (true ? 0 : true) ? 1 : 2 = 2
292
-

293
-
$a = 1;
294
-
$b = 2;
295
-
$a = $b += 3; // $a = ($b += 3) -> $a = 5, $b = 5
296
-
?>
297
-
]]>
298
-
</programlisting>
299
-
</example>
300
-
</para>
301
-
<para>
302
-
Operator precedence and associativity only determine how expressions
303
-
are grouped, they do not specify an order of evaluation. PHP does not
304
-
(in the general case) specify in which order an expression is evaluated
305
-
and code that assumes a specific order of evaluation should be avoided,
306
-
because the behavior can change between versions of PHP or depending on
307
-
the surrounding code.
308
-
<example>
309
-
<title>Undefined order of evaluation</title>
310
-
<programlisting role="php">
311
-
<![CDATA[
312
-
<?php
313
-
$a = 1;
314
-
echo $a + $a++; // may print either 2 or 3
315
-

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

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

441
-
echo (5 % 3)."\n"; // prints 2
442
-
echo (5 % -3)."\n"; // prints 2
443
-
echo (-5 % 3)."\n"; // prints -2
444
-
echo (-5 % -3)."\n"; // prints -2
445
-

446
-
?>
447
-
]]>
448
-
</programlisting>
449
-
</informalexample>
450
-
</para>
451
-
<simpara>
452
-
See also the manual page on
453
-
<link linkend="ref.math">Math functions</link>.
454
-
</simpara>
455
-

456
-
</sect1>
457
-

458
-
<sect1 xml:id="language.operators.assignment">
459
-
<title>Assignment Operators</title>
460
-
<simpara>
461
-
The basic assignment operator is "=". Your first inclination might
462
-
be to think of this as "equal to". Don't. It really means that the
463
-
left operand gets set to the value of the expression on the
464
-
right (that is, "gets set to").
465
-
</simpara>
466
-
<para>
467
-
The value of an assignment expression is the value assigned. That
468
-
is, the value of "<literal>$a = 3</literal>" is 3. This allows you to do some tricky
469
-
things:
470
-
<informalexample>
471
-
<programlisting role="php">
472
-
<![CDATA[
473
-
<?php
474
-

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

477
-
?>
478
-
]]>
479
-
</programlisting>
480
-
</informalexample>
481
-
</para>
482
-
<para>
483
-
In addition to the basic assignment operator, there are "combined
484
-
operators" for all of the <link linkend="language.operators">binary
485
-
arithmetic</link>, array union and string operators that allow you to use a value in an
486
-
expression and then set its value to the result of that expression. For
487
-
example:
488
-
<informalexample>
489
-
<programlisting role="php">
490
-
<![CDATA[
491
-
<?php
492
-

493
-
$a = 3;
494
-
$a += 5; // sets $a to 8, as if we had said: $a = $a + 5;
495
-
$b = "Hello ";
496
-
$b .= "There!"; // sets $b to "Hello There!", just like $b = $b . "There!";
497
-

498
-
?>
499
-
]]>
500
-
</programlisting>
501
-
</informalexample>
502
-
</para>
503
-
<para>
504
-
Note that the assignment copies the original variable to the new
505
-
one (assignment by value), so changes to one will not affect the
506
-
other. This may also have relevance if you need to copy something
507
-
like a large array inside a tight loop.
508
-
</para>
509
-
<para>
510
-
An exception to the usual assignment by value behaviour within PHP occurs
511
-
with <type>object</type>s, which are assigned by reference in PHP 5.
512
-
Objects may be explicitly copied via the <link
513
-
linkend="language.oop5.cloning">clone</link> keyword.
514
-
</para>
515
-

516
-
<sect2 xml:id="language.operators.assignment.reference">
517
-
<title>Assignment by Reference</title>
518
-
<para>
519
-
Assignment by reference is also supported, using the
520
-
"<computeroutput>$var = &amp;$othervar;</computeroutput>" syntax.
521
-
Assignment by reference means that both variables end up pointing at the
522
-
same data, and nothing is copied anywhere.
523
-
</para>
524
-
<para>
525
-
<example>
526
-
<title>Assigning by reference</title>
527
-
<programlisting role="php">
528
-
<![CDATA[
529
-
<?php
530
-
$a = 3;
531
-
$b = &$a; // $b is a reference to $a
532
-

533
-
print "$a\n"; // prints 3
534
-
print "$b\n"; // prints 3
535
-

536
-
$a = 4; // change $a
537
-

538
-
print "$a\n"; // prints 4
539
-
print "$b\n"; // prints 4 as well, since $b is a reference to $a, which has
540
-
// been changed
541
-
?>
542
-
]]>
543
-
</programlisting>
544
-
</example>
545
-
</para>
546
-
<para>
547
-
As of PHP 5, the <link linkend="language.oop5.basic.new">new</link>
548
-
operator returns a reference automatically, so assigning the result of
549
-
<link linkend="language.oop5.basic.new">new</link> by reference results
550
-
in an <constant>E_DEPRECATED</constant> message in PHP 5.3 and later, and
551
-
an <constant>E_STRICT</constant> message in earlier versions.
552
-
</para>
553
-
<para>
554
-
For example, this code will result in a warning:
555
-
<informalexample>
556
-
<programlisting role="php">
557
-
<![CDATA[
558
-
<?php
559
-
class C {}
560
-

561
-
/* The following line generates the following error message:
562
-
* Deprecated: Assigning the return value of new by reference is deprecated in...
563
-
*/
564
-
$o = &new C;
565
-
?>
566
-
]]>
567
-
</programlisting>
568
-
</informalexample>
569
-
</para>
570
-
<para>
571
-
More information on references and their potential uses can be found in
572
-
the <link linkend="language.references">References Explained</link>
573
-
section of the manual.
574
-
</para>
575
-
</sect2>
576
-
</sect1>
577
-

578
-
<sect1 xml:id="language.operators.bitwise">
579
-
<title>Bitwise Operators</title>
580
-
<simpara>
581
-
Bitwise operators allow evaluation and manipulation of specific
582
-
bits within an integer.
583
-
</simpara>
584
-
<table>
585
-
<title>Bitwise Operators</title>
586
-
<tgroup cols="3">
587
-
<thead>
588
-
<row>
589
-
<entry>Example</entry>
590
-
<entry>Name</entry>
591
-
<entry>Result</entry>
592
-
</row>
593
-
</thead>
594
-
<tbody>
595
-
<row>
596
-
<entry><userinput>$a &amp; $b</userinput></entry>
597
-
<entry>And</entry>
598
-
<entry>Bits that are set in both <varname>$a</varname> and <varname>$b</varname> are set.</entry>
599
-
</row>
600
-
<row>
601
-
<entry><userinput>$a | $b</userinput></entry>
602
-
<entry>Or (inclusive or)</entry>
603
-
<entry>Bits that are set in either <varname>$a</varname> or <varname>$b</varname> are set.</entry>
604
-
</row>
605
-
<row>
606
-
<entry><userinput>$a ^ $b</userinput></entry>
607
-
<entry>Xor (exclusive or)</entry>
608
-
<entry>
609
-
Bits that are set in <varname>$a</varname> or <varname>$b</varname> but not both are set.
610
-
</entry>
611
-
</row>
612
-
<row>
613
-
<entry><userinput>~ $a</userinput></entry>
614
-
<entry>Not</entry>
615
-
<entry>
616
-
Bits that are set in <varname>$a</varname> are not set, and vice versa.
617
-
</entry>
618
-
</row>
619
-
<row>
620
-
<entry><userinput>$a &lt;&lt; $b</userinput></entry>
621
-
<entry>Shift left</entry>
622
-
<entry>
623
-
Shift the bits of <varname>$a</varname> <varname>$b</varname> steps to the left (each step means
624
-
"multiply by two")
625
-
</entry>
626
-
</row>
627
-
<row>
628
-
<entry><userinput>$a &gt;&gt; $b</userinput></entry>
629
-
<entry>Shift right</entry>
630
-
<entry>
631
-
Shift the bits of <varname>$a</varname> <varname>$b</varname> steps to the right (each step means
632
-
"divide by two")
633
-
</entry>
634
-
</row>
635
-
</tbody>
636
-
</tgroup>
637
-
</table>
638
-
<para>
639
-
Bit shifting in PHP is arithmetic.
640
-
Bits shifted off either end are discarded.
641
-
Left shifts have zeros shifted in on the right while the sign
642
-
bit is shifted out on the left, meaning the sign of an operand
643
-
is not preserved.
644
-
Right shifts have copies of the sign bit shifted in on the left,
645
-
meaning the sign of an operand is preserved.
646
-
</para>
647
-
<para>
648
-
Use parentheses to ensure the desired
649
-
<link linkend="language.operators.precedence">precedence</link>.
650
-
For example, <literal>$a &amp; $b == true</literal> evaluates
651
-
the equivalency then the bitwise and; while
652
-
<literal>($a &amp; $b) == true</literal> evaluates the bitwise and
653
-
then the equivalency.
654
-
</para>
655
-
<para>
656
-
If both operands for the <literal>&amp;</literal>, <literal>|</literal> and
657
-
<literal>^</literal> operators are strings, then the operation will be
658
-
performed on the ASCII values of the characters that make up the strings and
659
-
the result will be a string. In all other cases, both operands will be
660
-
<link linkend="language.types.integer.casting">converted to integers</link>
661
-
and the result will be an integer.
662
-
</para>
663
-
<para>
664
-
If the operand for the <literal>~</literal> operator is a string, the
665
-
operation will be performed on the ASCII values of the characters that make
666
-
up the string and the result will be a string, otherwise the operand and the
667
-
result will be treated as integers.
668
-
</para>
669
-
<para>
670
-
Both operands and the result for the <literal>&lt;&lt;</literal> and
671
-
<literal>&gt;&gt;</literal> operators are always treated as integers.
672
-
</para>
673
-
<para>
674
-
<informalexample>
675
-
<para>
676
-
<literallayout>
677
-
PHP's error_reporting ini setting uses bitwise values,
678
-
providing a real-world demonstration of turning
679
-
bits off. To show all errors, except for notices,
680
-
the php.ini file instructions say to use:
681
-
<userinput>E_ALL &amp; ~E_NOTICE</userinput>
682
-
</literallayout>
683
-
</para>
684
-
<para>
685
-
<literallayout>
686
-
This works by starting with E_ALL:
687
-
<computeroutput>00000000000000000111011111111111</computeroutput>
688
-
Then taking the value of E_NOTICE...
689
-
<computeroutput>00000000000000000000000000001000</computeroutput>
690
-
... and inverting it via <literal>~</literal>:
691
-
<computeroutput>11111111111111111111111111110111</computeroutput>
692
-
Finally, it uses AND (&amp;) to find the bits turned
693
-
on in both values:
694
-
<computeroutput>00000000000000000111011111110111</computeroutput>
695
-
</literallayout>
696
-
</para>
697
-
<para>
698
-
<literallayout>
699
-
Another way to accomplish that is using XOR (<literal>^</literal>)
700
-
to find bits that are on in only one value or the other:
701
-
<userinput>E_ALL ^ E_NOTICE</userinput>
702
-
</literallayout>
703
-
</para>
704
-
</informalexample>
705
-
</para>
706
-
<para>
707
-
<informalexample>
708
-
<para>
709
-
<literallayout>
710
-
error_reporting can also be used to demonstrate turning bits on.
711
-
The way to show just errors and recoverable errors is:
712
-
<userinput>E_ERROR | E_RECOVERABLE_ERROR</userinput>
713
-
</literallayout>
714
-
</para>
715
-
<para>
716
-
<literallayout>
717
-
This process combines E_ERROR
718
-
<computeroutput>00000000000000000000000000000001</computeroutput>
719
-
and
720
-
<computeroutput>00000000000000000001000000000000</computeroutput>
721
-
using the OR (<literal>|</literal>) operator
722
-
to get the bits turned on in either value:
723
-
<computeroutput>00000000000000000001000000000001</computeroutput>
724
-
</literallayout>
725
-
</para>
726
-
</informalexample>
727
-
</para>
728
-
<para>
729
-
<example>
730
-
<title>Bitwise AND, OR and XOR operations on integers</title>
731
-
<programlisting role="php">
732
-
<![CDATA[
733
-
<?php
734
-
/*
735
-
* Ignore the top section,
736
-
* it is just formatting to make output clearer.
737
-
*/
738
-

739
-
$format = '(%1$2d = %1$04b) = (%2$2d = %2$04b)'
740
-
. ' %3$s (%4$2d = %4$04b)' . "\n";
741
-

742
-
echo <<<EOH
743
-
--------- --------- -- ---------
744
-
result value op test
745
-
--------- --------- -- ---------
746
-
EOH;
747
-

748
-

749
-
/*
750
-
* Here are the examples.
751
-
*/
752
-

753
-
$values = array(0, 1, 2, 4, 8);
754
-
$test = 1 + 4;
755
-

756
-
echo "\n Bitwise AND \n";
757
-
foreach ($values as $value) {
758
-
$result = $value & $test;
759
-
printf($format, $result, $value, '&', $test);
760
-
}
761
-

762
-
echo "\n Bitwise Inclusive OR \n";
763
-
foreach ($values as $value) {
764
-
$result = $value | $test;
765
-
printf($format, $result, $value, '|', $test);
766
-
}
767
-

768
-
echo "\n Bitwise Exclusive OR (XOR) \n";
769
-
foreach ($values as $value) {
770
-
$result = $value ^ $test;
771
-
printf($format, $result, $value, '^', $test);
772
-
}
773
-
?>
774
-
]]>
775
-
</programlisting>
776
-
&example.outputs;
777
-
<screen>
778
-
<![CDATA[
779
-
--------- --------- -- ---------
780
-
result value op test
781
-
--------- --------- -- ---------
782
-
Bitwise AND
783
-
( 0 = 0000) = ( 0 = 0000) & ( 5 = 0101)
784
-
( 1 = 0001) = ( 1 = 0001) & ( 5 = 0101)
785
-
( 0 = 0000) = ( 2 = 0010) & ( 5 = 0101)
786
-
( 4 = 0100) = ( 4 = 0100) & ( 5 = 0101)
787
-
( 0 = 0000) = ( 8 = 1000) & ( 5 = 0101)
788
-

789
-
Bitwise Inclusive OR
790
-
( 5 = 0101) = ( 0 = 0000) | ( 5 = 0101)
791
-
( 5 = 0101) = ( 1 = 0001) | ( 5 = 0101)
792
-
( 7 = 0111) = ( 2 = 0010) | ( 5 = 0101)
793
-
( 5 = 0101) = ( 4 = 0100) | ( 5 = 0101)
794
-
(13 = 1101) = ( 8 = 1000) | ( 5 = 0101)
795
-

796
-
Bitwise Exclusive OR (XOR)
797
-
( 5 = 0101) = ( 0 = 0000) ^ ( 5 = 0101)
798
-
( 4 = 0100) = ( 1 = 0001) ^ ( 5 = 0101)
799
-
( 7 = 0111) = ( 2 = 0010) ^ ( 5 = 0101)
800
-
( 1 = 0001) = ( 4 = 0100) ^ ( 5 = 0101)
801
-
(13 = 1101) = ( 8 = 1000) ^ ( 5 = 0101)
802
-
]]>
803
-
</screen>
804
-
</example>
805
-
</para>
806
-
<para>
807
-
<example>
808
-
<title>Bitwise XOR operations on strings</title>
809
-
<programlisting role="php">
810
-
<![CDATA[
811
-
<?php
812
-
echo 12 ^ 9; // Outputs '5'
813
-

814
-
echo "12" ^ "9"; // Outputs the Backspace character (ascii 8)
815
-
// ('1' (ascii 49)) ^ ('9' (ascii 57)) = #8
816
-

817
-
echo "hallo" ^ "hello"; // Outputs the ascii values #0 #4 #0 #0 #0
818
-
// 'a' ^ 'e' = #4
819
-

820
-
echo 2 ^ "3"; // Outputs 1
821
-
// 2 ^ ((int)"3") == 1
822
-

823
-
echo "2" ^ 3; // Outputs 1
824
-
// ((int)"2") ^ 3 == 1
825
-
?>
826
-
]]>
827
-
</programlisting>
828
-
</example>
829
-
</para>
830
-
<para>
831
-
<example>
832
-
<title>Bit shifting on integers</title>
833
-
<programlisting role="php">
834
-
<![CDATA[
835
-
<?php
836
-
/*
837
-
* Here are the examples.
838
-
*/
839
-

840
-
echo "\n--- BIT SHIFT RIGHT ON POSITIVE INTEGERS ---\n";
841
-

842
-
$val = 4;
843
-
$places = 1;
844
-
$res = $val >> $places;
845
-
p($res, $val, '>>', $places, 'copy of sign bit shifted into left side');
846
-

847
-
$val = 4;
848
-
$places = 2;
849
-
$res = $val >> $places;
850
-
p($res, $val, '>>', $places);
851
-

852
-
$val = 4;
853
-
$places = 3;
854
-
$res = $val >> $places;
855
-
p($res, $val, '>>', $places, 'bits shift out right side');
856
-

857
-
$val = 4;
858
-
$places = 4;
859
-
$res = $val >> $places;
860
-
p($res, $val, '>>', $places, 'same result as above; can not shift beyond 0');
861
-

862
-

863
-
echo "\n--- BIT SHIFT RIGHT ON NEGATIVE INTEGERS ---\n";
864
-

865
-
$val = -4;
866
-
$places = 1;
867
-
$res = $val >> $places;
868
-
p($res, $val, '>>', $places, 'copy of sign bit shifted into left side');
869
-

870
-
$val = -4;
871
-
$places = 2;
872
-
$res = $val >> $places;
873
-
p($res, $val, '>>', $places, 'bits shift out right side');
874
-

875
-
$val = -4;
876
-
$places = 3;
877
-
$res = $val >> $places;
878
-
p($res, $val, '>>', $places, 'same result as above; can not shift beyond -1');
879
-

880
-

881
-
echo "\n--- BIT SHIFT LEFT ON POSITIVE INTEGERS ---\n";
882
-

883
-
$val = 4;
884
-
$places = 1;
885
-
$res = $val << $places;
886
-
p($res, $val, '<<', $places, 'zeros fill in right side');
887
-

888
-
$val = 4;
889
-
$places = (PHP_INT_SIZE * 8) - 4;
890
-
$res = $val << $places;
891
-
p($res, $val, '<<', $places);
892
-

893
-
$val = 4;
894
-
$places = (PHP_INT_SIZE * 8) - 3;
895
-
$res = $val << $places;
896
-
p($res, $val, '<<', $places, 'sign bits get shifted out');
897
-

898
-
$val = 4;
899
-
$places = (PHP_INT_SIZE * 8) - 2;
900
-
$res = $val << $places;
901
-
p($res, $val, '<<', $places, 'bits shift out left side');
902
-

903
-

904
-
echo "\n--- BIT SHIFT LEFT ON NEGATIVE INTEGERS ---\n";
905
-

906
-
$val = -4;
907
-
$places = 1;
908
-
$res = $val << $places;
909
-
p($res, $val, '<<', $places, 'zeros fill in right side');
910
-

911
-
$val = -4;
912
-
$places = (PHP_INT_SIZE * 8) - 3;
913
-
$res = $val << $places;
914
-
p($res, $val, '<<', $places);
915
-

916
-
$val = -4;
917
-
$places = (PHP_INT_SIZE * 8) - 2;
918
-
$res = $val << $places;
919
-
p($res, $val, '<<', $places, 'bits shift out left side, including sign bit');
920
-

921
-

922
-
/*
923
-
* Ignore this bottom section,
924
-
* it is just formatting to make output clearer.
925
-
*/
926
-

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

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

932
-
echo " Decimal:\n";
933
-
printf(" val=%d\n", $val);
934
-
printf(" res=%d\n", $res);
935
-

936
-
echo " Binary:\n";
937
-
printf(' val=' . $format, $val);
938
-
printf(' res=' . $format, $res);
939
-

940
-
if ($note) {
941
-
echo " NOTE: $note\n";
942
-
}
943
-

944
-
echo "\n";
945
-
}
946
-
?>
947
-
]]>
948
-
</programlisting>
949
-
&example.outputs.32bit;
950
-
<screen>
951
-
<![CDATA[
952
-

953
-
--- BIT SHIFT RIGHT ON POSITIVE INTEGERS ---
954
-
Expression: 2 = 4 >> 1
955
-
Decimal:
956
-
val=4
957
-
res=2
958
-
Binary:
959
-
val=00000000000000000000000000000100
960
-
res=00000000000000000000000000000010
961
-
NOTE: copy of sign bit shifted into left side
962
-

963
-
Expression: 1 = 4 >> 2
964
-
Decimal:
965
-
val=4
966
-
res=1
967
-
Binary:
968
-
val=00000000000000000000000000000100
969
-
res=00000000000000000000000000000001
970
-

971
-
Expression: 0 = 4 >> 3
972
-
Decimal:
973
-
val=4
974
-
res=0
975
-
Binary:
976
-
val=00000000000000000000000000000100
977
-
res=00000000000000000000000000000000
978
-
NOTE: bits shift out right side
979
-

980
-
Expression: 0 = 4 >> 4
981
-
Decimal:
982
-
val=4
983
-
res=0
984
-
Binary:
985
-
val=00000000000000000000000000000100
986
-
res=00000000000000000000000000000000
987
-
NOTE: same result as above; can not shift beyond 0
988
-

989
-

990
-
--- BIT SHIFT RIGHT ON NEGATIVE INTEGERS ---
991
-
Expression: -2 = -4 >> 1
992
-
Decimal:
993
-
val=-4
994
-
res=-2
995
-
Binary:
996
-
val=11111111111111111111111111111100
997
-
res=11111111111111111111111111111110
998
-
NOTE: copy of sign bit shifted into left side
999
-

1000
-
Expression: -1 = -4 >> 2
1001
-
Decimal:
1002
-
val=-4
1003
-
res=-1
1004
-
Binary:
1005
-
val=11111111111111111111111111111100
1006
-
res=11111111111111111111111111111111
1007
-
NOTE: bits shift out right side
1008
-

1009
-
Expression: -1 = -4 >> 3
1010
-
Decimal:
1011
-
val=-4
1012
-
res=-1
1013
-
Binary:
1014
-
val=11111111111111111111111111111100
1015
-
res=11111111111111111111111111111111
1016
-
NOTE: same result as above; can not shift beyond -1
1017
-

1018
-

1019
-
--- BIT SHIFT LEFT ON POSITIVE INTEGERS ---
1020
-
Expression: 8 = 4 << 1
1021
-
Decimal:
1022
-
val=4
1023
-
res=8
1024
-
Binary:
1025
-
val=00000000000000000000000000000100
1026
-
res=00000000000000000000000000001000
1027
-
NOTE: zeros fill in right side
1028
-

1029
-
Expression: 1073741824 = 4 << 28
1030
-
Decimal:
1031
-
val=4
1032
-
res=1073741824
1033
-
Binary:
1034
-
val=00000000000000000000000000000100
1035
-
res=01000000000000000000000000000000
1036
-

1037
-
Expression: -2147483648 = 4 << 29
1038
-
Decimal:
1039
-
val=4
1040
-
res=-2147483648
1041
-
Binary:
1042
-
val=00000000000000000000000000000100
1043
-
res=10000000000000000000000000000000
1044
-
NOTE: sign bits get shifted out
1045
-

1046
-
Expression: 0 = 4 << 30
1047
-
Decimal:
1048
-
val=4
1049
-
res=0
1050
-
Binary:
1051
-
val=00000000000000000000000000000100
1052
-
res=00000000000000000000000000000000
1053
-
NOTE: bits shift out left side
1054
-

1055
-

1056
-
--- BIT SHIFT LEFT ON NEGATIVE INTEGERS ---
1057
-
Expression: -8 = -4 << 1
1058
-
Decimal:
1059
-
val=-4
1060
-
res=-8
1061
-
Binary:
1062
-
val=11111111111111111111111111111100
1063
-
res=11111111111111111111111111111000
1064
-
NOTE: zeros fill in right side
1065
-

1066
-
Expression: -2147483648 = -4 << 29
1067
-
Decimal:
1068
-
val=-4
1069
-
res=-2147483648
1070
-
Binary:
1071
-
val=11111111111111111111111111111100
1072
-
res=10000000000000000000000000000000
1073
-

1074
-
Expression: 0 = -4 << 30
1075
-
Decimal:
1076
-
val=-4
1077
-
res=0
1078
-
Binary:
1079
-
val=11111111111111111111111111111100
1080
-
res=00000000000000000000000000000000
1081
-
NOTE: bits shift out left side, including sign bit
1082
-
]]>
1083
-
</screen>
1084
-
&example.outputs.64bit;
1085
-
<screen>
1086
-
<![CDATA[
1087
-

1088
-
--- BIT SHIFT RIGHT ON POSITIVE INTEGERS ---
1089
-
Expression: 2 = 4 >> 1
1090
-
Decimal:
1091
-
val=4
1092
-
res=2
1093
-
Binary:
1094
-
val=0000000000000000000000000000000000000000000000000000000000000100
1095
-
res=0000000000000000000000000000000000000000000000000000000000000010
1096
-
NOTE: copy of sign bit shifted into left side
1097
-

1098
-
Expression: 1 = 4 >> 2
1099
-
Decimal:
1100
-
val=4
1101
-
res=1
1102
-
Binary:
1103
-
val=0000000000000000000000000000000000000000000000000000000000000100
1104
-
res=0000000000000000000000000000000000000000000000000000000000000001
1105
-

1106
-
Expression: 0 = 4 >> 3
1107
-
Decimal:
1108
-
val=4
1109
-
res=0
1110
-
Binary:
1111
-
val=0000000000000000000000000000000000000000000000000000000000000100
1112
-
res=0000000000000000000000000000000000000000000000000000000000000000
1113
-
NOTE: bits shift out right side
1114
-

1115
-
Expression: 0 = 4 >> 4
1116
-
Decimal:
1117
-
val=4
1118
-
res=0
1119
-
Binary:
1120
-
val=0000000000000000000000000000000000000000000000000000000000000100
1121
-
res=0000000000000000000000000000000000000000000000000000000000000000
1122
-
NOTE: same result as above; can not shift beyond 0
1123
-

1124
-

1125
-
--- BIT SHIFT RIGHT ON NEGATIVE INTEGERS ---
1126
-
Expression: -2 = -4 >> 1
1127
-
Decimal:
1128
-
val=-4
1129
-
res=-2
1130
-
Binary:
1131
-
val=1111111111111111111111111111111111111111111111111111111111111100
1132
-
res=1111111111111111111111111111111111111111111111111111111111111110
1133
-
NOTE: copy of sign bit shifted into left side
1134
-

1135
-
Expression: -1 = -4 >> 2
1136
-
Decimal:
1137
-
val=-4
1138
-
res=-1
1139
-
Binary:
1140
-
val=1111111111111111111111111111111111111111111111111111111111111100
1141
-
res=1111111111111111111111111111111111111111111111111111111111111111
1142
-
NOTE: bits shift out right side
1143
-

1144
-
Expression: -1 = -4 >> 3
1145
-
Decimal:
1146
-
val=-4
1147
-
res=-1
1148
-
Binary:
1149
-
val=1111111111111111111111111111111111111111111111111111111111111100
1150
-
res=1111111111111111111111111111111111111111111111111111111111111111
1151
-
NOTE: same result as above; can not shift beyond -1
1152
-

1153
-

1154
-
--- BIT SHIFT LEFT ON POSITIVE INTEGERS ---
1155
-
Expression: 8 = 4 << 1
1156
-
Decimal:
1157
-
val=4
1158
-
res=8
1159
-
Binary:
1160
-
val=0000000000000000000000000000000000000000000000000000000000000100
1161
-
res=0000000000000000000000000000000000000000000000000000000000001000
1162
-
NOTE: zeros fill in right side
1163
-

1164
-
Expression: 4611686018427387904 = 4 << 60
1165
-
Decimal:
1166
-
val=4
1167
-
res=4611686018427387904
1168
-
Binary:
1169
-
val=0000000000000000000000000000000000000000000000000000000000000100
1170
-
res=0100000000000000000000000000000000000000000000000000000000000000
1171
-

1172
-
Expression: -9223372036854775808 = 4 << 61
1173
-
Decimal:
1174
-
val=4
1175
-
res=-9223372036854775808
1176
-
Binary:
1177
-
val=0000000000000000000000000000000000000000000000000000000000000100
1178
-
res=1000000000000000000000000000000000000000000000000000000000000000
1179
-
NOTE: sign bits get shifted out
1180
-

1181
-
Expression: 0 = 4 << 62
1182
-
Decimal:
1183
-
val=4
1184
-
res=0
1185
-
Binary:
1186
-
val=0000000000000000000000000000000000000000000000000000000000000100
1187
-
res=0000000000000000000000000000000000000000000000000000000000000000
1188
-
NOTE: bits shift out left side
1189
-

1190
-

1191
-
--- BIT SHIFT LEFT ON NEGATIVE INTEGERS ---
1192
-
Expression: -8 = -4 << 1
1193
-
Decimal:
1194
-
val=-4
1195
-
res=-8
1196
-
Binary:
1197
-
val=1111111111111111111111111111111111111111111111111111111111111100
1198
-
res=1111111111111111111111111111111111111111111111111111111111111000
1199
-
NOTE: zeros fill in right side
1200
-

1201
-
Expression: -9223372036854775808 = -4 << 61
1202
-
Decimal:
1203
-
val=-4
1204
-
res=-9223372036854775808
1205
-
Binary:
1206
-
val=1111111111111111111111111111111111111111111111111111111111111100
1207
-
res=1000000000000000000000000000000000000000000000000000000000000000
1208
-

1209
-
Expression: 0 = -4 << 62
1210
-
Decimal:
1211
-
val=-4
1212
-
res=0
1213
-
Binary:
1214
-
val=1111111111111111111111111111111111111111111111111111111111111100
1215
-
res=0000000000000000000000000000000000000000000000000000000000000000
1216
-
NOTE: bits shift out left side, including sign bit
1217
-
]]>
1218
-
</screen>
1219
-
</example>
1220
-
</para>
1221
-
<warning>
1222
-
<para>
1223
-
Shifting integers by values greater than or equal to the system long
1224
-
integer width results in undefined behavior. In other words, don't shift
1225
-
more than 31 bits on a 32-bit system, and don't shift more than 63 bits on
1226
-
a 64-bit system.
1227
-
</para>
1228
-
<para>
1229
-
Use functions from the <link linkend="book.gmp">gmp</link> extension for
1230
-
bitwise manipulation on numbers beyond <literal>PHP_INT_MAX</literal>.
1231
-
</para>
1232
-
</warning>
1233
-
<para>
1234
-
See also
1235
-
<function>pack</function>,
1236
-
<function>unpack</function>,
1237
-
<function>gmp_and</function>,
1238
-
<function>gmp_or</function>,
1239
-
<function>gmp_xor</function>,
1240
-
<function>gmp_testbit</function>,
1241
-
<function>gmp_clrbit</function>
1242
-
</para>
1243
-
</sect1>
1244
-

1245
-
<sect1 xml:id="language.operators.comparison">
1246
-
<title>Comparison Operators</title>
1247
-
<simpara>
1248
-
Comparison operators, as their name implies, allow you to compare
1249
-
two values. You may also be interested in viewing
1250
-
<link linkend="types.comparisons">the type comparison tables</link>,
1251
-
as they show examples of various type related comparisons.
1252
-
</simpara>
1253
-
<table>
1254
-
<title>Comparison Operators</title>
1255
-
<tgroup cols="3">
1256
-
<thead>
1257
-
<row>
1258
-
<entry>Example</entry>
1259
-
<entry>Name</entry>
1260
-
<entry>Result</entry>
1261
-
</row>
1262
-
</thead>
1263
-
<tbody>
1264
-
<row>
1265
-
<entry>$a == $b</entry>
1266
-
<entry>Equal</entry>
1267
-
<entry>&true; if <varname>$a</varname> is equal to <varname>$b</varname> after type juggling.</entry>
1268
-
</row>
1269
-
<row>
1270
-
<entry>$a === $b</entry>
1271
-
<entry>Identical</entry>
1272
-
<entry>
1273
-
&true; if <varname>$a</varname> is equal to <varname>$b</varname>, and they are of the same
1274
-
type.
1275
-
</entry>
1276
-
</row>
1277
-
<row>
1278
-
<entry>$a != $b</entry>
1279
-
<entry>Not equal</entry>
1280
-
<entry>&true; if <varname>$a</varname> is not equal to <varname>$b</varname> after type juggling.</entry>
1281
-
</row>
1282
-
<row>
1283
-
<entry>$a &lt;&gt; $b</entry>
1284
-
<entry>Not equal</entry>
1285
-
<entry>&true; if <varname>$a</varname> is not equal to <varname>$b</varname> after type juggling.</entry>
1286
-
</row>
1287
-
<row>
1288
-
<entry>$a !== $b</entry>
1289
-
<entry>Not identical</entry>
1290
-
<entry>
1291
-
&true; if <varname>$a</varname> is not equal to <varname>$b</varname>, or they are not of the same
1292
-
type.
1293
-
</entry>
1294
-
</row>
1295
-
<row>
1296
-
<entry>$a &lt; $b</entry>
1297
-
<entry>Less than</entry>
1298
-
<entry>&true; if <varname>$a</varname> is strictly less than <varname>$b</varname>.</entry>
1299
-
</row>
1300
-
<row>
1301
-
<entry>$a &gt; $b</entry>
1302
-
<entry>Greater than</entry>
1303
-
<entry>&true; if <varname>$a</varname> is strictly greater than <varname>$b</varname>.</entry>
1304
-
</row>
1305
-
<row>
1306
-
<entry>$a &lt;= $b</entry>
1307
-
<entry>Less than or equal to </entry>
1308
-
<entry>&true; if <varname>$a</varname> is less than or equal to <varname>$b</varname>.</entry>
1309
-
</row>
1310
-
<row>
1311
-
<entry>$a &gt;= $b</entry>
1312
-
<entry>Greater than or equal to </entry>
1313
-
<entry>&true; if <varname>$a</varname> is greater than or equal to <varname>$b</varname>.</entry>
1314
-
</row>
1315
-
<row>
1316
-
<entry>$a &lt;=&gt; $b</entry>
1317
-
<entry>Spaceship</entry>
1318
-
<entry>
1319
-
An <type>integer</type> less than, equal to, or greater than zero when
1320
-
<varname>$a</varname> is respectively less than, equal to, or greater
1321
-
than <varname>$b</varname>. Available as of PHP 7.
1322
-
</entry>
1323
-
</row>
1324
-
</tbody>
1325
-
</tgroup>
1326
-
</table>
1327
-
<para>
1328
-
If you compare a number with a string or the comparison involves numerical
1329
-
strings, then each string is
1330
-
<link linkend="language.types.string.conversion">converted to a number</link>
1331
-
and the comparison performed numerically. These rules also apply to the
1332
-
<link linkend="control-structures.switch">switch</link> statement. The
1333
-
type conversion does not take place when the comparison is === or !== as
1334
-
this involves comparing the type as well as the value.
1335
-
<informalexample>
1336
-
<programlisting role="php">
1337
-
<![CDATA[
1338
-
<?php
1339
-
var_dump(0 == "a"); // 0 == 0 -> true
1340
-
var_dump("1" == "01"); // 1 == 1 -> true
1341
-
var_dump("10" == "1e1"); // 10 == 10 -> true
1342
-
var_dump(100 == "1e2"); // 100 == 100 -> true
1343
-

1344
-
switch ("a") {
1345
-
case 0:
1346
-
echo "0";
1347
-
break;
1348
-
case "a": // never reached because "a" is already matched with 0
1349
-
echo "a";
1350
-
break;
1351
-
}
1352
-
?>
1353
-
]]>
1354
-
</programlisting>
1355
-
<programlisting role="php">
1356
-
<![CDATA[
1357
-
<?php
1358
-
// Integers
1359
-
echo 1 <=> 1; // 0
1360
-
echo 1 <=> 2; // -1
1361
-
echo 2 <=> 1; // 1
1362
-
1363
-
// Floats
1364
-
echo 1.5 <=> 1.5; // 0
1365
-
echo 1.5 <=> 2.5; // -1
1366
-
echo 2.5 <=> 1.5; // 1
1367
-
1368
-
// Strings
1369
-
echo "a" <=> "a"; // 0
1370
-
echo "a" <=> "b"; // -1
1371
-
echo "b" <=> "a"; // 1
1372
-
1373
-
echo "a" <=> "aa"; // -1
1374
-
echo "zz" <=> "aa"; // 1
1375
-
1376
-
// Arrays
1377
-
echo [] <=> []; // 0
1378
-
echo [1, 2, 3] <=> [1, 2, 3]; // 0
1379
-
echo [1, 2, 3] <=> []; // 1
1380
-
echo [1, 2, 3] <=> [1, 2, 1]; // 1
1381
-
echo [1, 2, 3] <=> [1, 2, 4]; // -1
1382
-
1383
-
// Objects
1384
-
$a = (object) ["a" => "b"];
1385
-
$b = (object) ["a" => "b"];
1386
-
echo $a <=> $b; // 0
1387
-
1388
-
$a = (object) ["a" => "b"];
1389
-
$b = (object) ["a" => "c"];
1390
-
echo $a <=> $b; // -1
1391
-
1392
-
$a = (object) ["a" => "c"];
1393
-
$b = (object) ["a" => "b"];
1394
-
echo $a <=> $b; // 1
1395
-
1396
-
// only values are compared
1397
-
$a = (object) ["a" => "b"];
1398
-
$b = (object) ["b" => "b"];
1399
-
echo $a <=> $b; // 1
1400
-

1401
-
?>
1402
-
]]>
1403
-
1404
-
</programlisting>
1405
-
</informalexample>
1406
-
</para>
1407
-

1408
-
<para>
1409
-
For various types, comparison is done according to the following
1410
-
table (in order).
1411
-
</para>
1412
-
<table xml:id="language.operators.comparison.types">
1413
-
<title>Comparison with Various Types</title>
1414
-
<tgroup cols="3">
1415
-
<thead>
1416
-
<row>
1417
-
<entry>Type of Operand 1</entry>
1418
-
<entry>Type of Operand 2</entry>
1419
-
<entry>Result</entry>
1420
-
</row>
1421
-
</thead>
1422
-
<tbody>
1423
-
<row>
1424
-
<entry><type>null</type> or <type>string</type></entry>
1425
-
<entry><type>string</type></entry>
1426
-
<entry>Convert &null; to "", numerical or lexical comparison</entry>
1427
-
</row>
1428
-
<row>
1429
-
<entry><type>bool</type> or <type>null</type></entry>
1430
-
<entry>anything</entry>
1431
-
<entry>Convert both sides to <type>bool</type>, &false; &lt; &true;</entry>
1432
-
</row>
1433
-
<row>
1434
-
<entry><type>object</type></entry>
1435
-
<entry><type>object</type></entry>
1436
-
<entry>Built-in classes can define its own comparison, different classes
1437
-
are uncomparable, same class - compare properties the same way as
1438
-
arrays (PHP 4), PHP 5 has its own <link
1439
-
linkend="language.oop5.object-comparison">explanation</link>
1440
-
</entry>
1441
-
</row>
1442
-
<row>
1443
-
<entry><type>string</type>, <type>resource</type> or <type>number</type></entry>
1444
-
<entry><type>string</type>, <type>resource</type> or <type>number</type></entry>
1445
-
<entry>Translate strings and resources to numbers, usual math</entry>
1446
-
</row>
1447
-
<row>
1448
-
<entry><type>array</type></entry>
1449
-
<entry><type>array</type></entry>
1450
-
<entry>Array with fewer members is smaller, if key from operand 1 is not
1451
-
found in operand 2 then arrays are uncomparable, otherwise - compare
1452
-
value by value (see following example)</entry>
1453
-
</row>
1454
-
<row>
1455
-
<entry><type>object</type></entry>
1456
-
<entry>anything</entry>
1457
-
<entry><type>object</type> is always greater</entry>
1458
-
</row>
1459
-
<row>
1460
-
<entry><type>array</type></entry>
1461
-
<entry>anything</entry>
1462
-
<entry><type>array</type> is always greater</entry>
1463
-
</row>
1464
-
</tbody>
1465
-
</tgroup>
1466
-
</table>
1467
-

1468
-
<para>
1469
-
<example>
1470
-
<title>Boolean/null comparison</title>
1471
-
<programlisting role="php">
1472
-
<![CDATA[
1473
-
<?php
1474
-
// Bool and null are compared as bool always
1475
-
var_dump(1 == TRUE); // TRUE - same as (bool)1 == TRUE
1476
-
var_dump(0 == FALSE); // TRUE - same as (bool)0 == FALSE
1477
-
var_dump(100 < TRUE); // FALSE - same as (bool)100 < TRUE
1478
-
var_dump(-10 < FALSE);// FALSE - same as (bool)-10 < FALSE
1479
-
var_dump(min(-100, -10, NULL, 10, 100)); // NULL - (bool)NULL < (bool)-100 is FALSE < TRUE
1480
-
?>
1481
-
]]>
1482
-
</programlisting>
1483
-
</example>
1484
-
</para>
1485
-

1486
-

1487
-
<para>
1488
-
<example>
1489
-
<title>Transcription of standard array comparison</title>
1490
-
<programlisting role="php">
1491
-
<![CDATA[
1492
-
<?php
1493
-
// Arrays are compared like this with standard comparison operators
1494
-
function standard_array_compare($op1, $op2)
1495
-
{
1496
-
if (count($op1) < count($op2)) {
1497
-
return -1; // $op1 < $op2
1498
-
} elseif (count($op1) > count($op2)) {
1499
-
return 1; // $op1 > $op2
1500
-
}
1501
-
foreach ($op1 as $key => $val) {
1502
-
if (!array_key_exists($key, $op2)) {
1503
-
return null; // uncomparable
1504
-
} elseif ($val < $op2[$key]) {
1505
-
return -1;
1506
-
} elseif ($val > $op2[$key]) {
1507
-
return 1;
1508
-
}
1509
-
}
1510
-
return 0; // $op1 == $op2
1511
-
}
1512
-
?>
1513
-
]]>
1514
-
</programlisting>
1515
-
</example>
1516
-
</para>
1517
-

1518
-
<para>
1519
-
See also <function>strcasecmp</function>,
1520
-
<function>strcmp</function>,
1521
-
<link linkend="language.operators.array">Array operators</link>,
1522
-
and the manual section on
1523
-
<link linkend="language.types">Types</link>.
1524
-
</para>
1525
-

1526
-
<warning>
1527
-
<title>Comparison of floating point numbers</title>
1528
-

1529
-
<para>
1530
-
Because of the way <type>float</type>s are represented internally, you
1531
-
should not test two <type>float</type>s for equality.
1532
-
</para>
1533
-

1534
-
<para>
1535
-
See the documentation for <type>float</type> for more information.
1536
-
</para>
1537
-
</warning>
1538
-

1539
-
<sect2 xml:id="language.operators.comparison.ternary">
1540
-
<title>Ternary Operator</title>
1541
-
<para>
1542
-
Another conditional operator is the "?:" (or ternary) operator.
1543
-
<example>
1544
-
<title>Assigning a default value</title>
1545
-
<programlisting role="php">
1546
-
<![CDATA[
1547
-
<?php
1548
-
// Example usage for: Ternary Operator
1549
-
$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];
1550
-

1551
-
// The above is identical to this if/else statement
1552
-
if (empty($_POST['action'])) {
1553
-
$action = 'default';
1554
-
} else {
1555
-
$action = $_POST['action'];
1556
-
}
1557
-

1558
-
?>
1559
-
]]>
1560
-
</programlisting>
1561
-
</example>
1562
-
The expression <literal>(expr1) ? (expr2) : (expr3)</literal>
1563
-
evaluates to <replaceable>expr2</replaceable> if
1564
-
<replaceable>expr1</replaceable> evaluates to &true;, and
1565
-
<replaceable>expr3</replaceable> if
1566
-
<replaceable>expr1</replaceable> evaluates to &false;.
1567
-
</para>
1568
-
<para>
1569
-
Since PHP 5.3, it is possible to leave out the middle part of the ternary
1570
-
operator. Expression <literal>expr1 ?: expr3</literal> returns
1571
-
<replaceable>expr1</replaceable> if <replaceable>expr1</replaceable>
1572
-
evaluates to &true;, and <replaceable>expr3</replaceable> otherwise.
1573
-
</para>
1574
-
<note>
1575
-
<simpara>
1576
-
Please note that the ternary operator is an expression, and that it
1577
-
doesn't evaluate to a variable, but to the result of an expression. This
1578
-
is important to know if you want to return a variable by reference.
1579
-
The statement <literal>return $var == 42 ? $a : $b;</literal> in a
1580
-
return-by-reference function will therefore not work and a warning is
1581
-
issued.
1582
-
</simpara>
1583
-
</note>
1584
-
<note>
1585
-
<para>
1586
-
It is recommended that you avoid "stacking" ternary expressions. PHP's
1587
-
behaviour when using more than one ternary operator within a single
1588
-
statement is non-obvious:
1589
-
<example>
1590
-
<title>Non-obvious Ternary Behaviour</title>
1591
-
<programlisting role="php">
1592
-
<![CDATA[
1593
-
<?php
1594
-
// on first glance, the following appears to output 'true'
1595
-
echo (true?'true':false?'t':'f');
1596
-

1597
-
// however, the actual output of the above is 't'
1598
-
// this is because ternary expressions are evaluated from left to right
1599
-

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

1603
-
// here, you can see that the first expression is evaluated to 'true', which
1604
-
// in turn evaluates to (bool)true, thus returning the true branch of the
1605
-
// second ternary expression.
1606
-
?>
1607
-
]]>
1608
-
</programlisting>
1609
-
</example>
1610
-
</para>
1611
-
</note>
1612
-
</sect2>
1613
-
1614
-
<sect2 xml:id="language.operators.comparison.coalesce">
1615
-
<title>Null Coalescing Operator</title>
1616
-
<para>
1617
-
Further exists the "??" (or null coalescing) operator, available as of PHP 7.
1618
-
<example>
1619
-
<title>Assigning a default value</title>
1620
-
<programlisting role="php">
1621
-
<![CDATA[
1622
-
<?php
1623
-
// Example usage for: Null Coalesce Operator
1624
-
$action = $_POST['action'] ?? 'default';
1625
-

1626
-
// The above is identical to this if/else statement
1627
-
if (isset($_POST['action'])) {
1628
-
$action = $_POST['action'];
1629
-
} else {
1630
-
$action = 'default';
1631
-
}
1632
-

1633
-
?>
1634
-
]]>
1635
-
</programlisting>
1636
-
</example>
1637
-
The expression <literal>(expr1) ?? (expr2)</literal> evaluates to
1638
-
<replaceable>expr2</replaceable> if <replaceable>expr1</replaceable> is
1639
-
&null;, and <replaceable>expr1</replaceable> otherwise.
1640
-
</para>
1641
-
<para>
1642
-
In particular, this operator does not emit a notice if the left-hand side
1643
-
value does not exist, just like <function>isset</function>. This is especially
1644
-
useful on array keys.
1645
-
</para>
1646
-
<note>
1647
-
<simpara>
1648
-
Please note that the null coalescing operator is an expression, and that it
1649
-
doesn't evaluate to a variable, but to the result of an expression. This
1650
-
is important to know if you want to return a variable by reference.
1651
-
The statement <literal>return $foo ?? $bar;</literal> in a
1652
-
return-by-reference function will therefore not work and a warning is
1653
-
issued.
1654
-
</simpara>
1655
-
</note>
1656
-
<note>
1657
-
<para>
1658
-
Please note that the null coalescing operator allows for simple nesting:
1659
-
<example>
1660
-
<title>Nesting null coalescing operator</title>
1661
-
<programlisting role="php">
1662
-
<![CDATA[
1663
-
<?php
1664
-

1665
-
$foo = null;
1666
-
$bar = null;
1667
-
$baz = 1;
1668
-
$qux = 2;
1669
-

1670
-
echo $foo ?? $bar ?? $baz ?? $qux; // outputs 1
1671
-

1672
-
?>
1673
-
]]>
1674
-
</programlisting>
1675
-
</example>
1676
-
</para>
1677
-
</note>
1678
-
</sect2>
1679
-
</sect1>
1680
-

1681
-
<sect1 xml:id="language.operators.errorcontrol">
1682
-
<title>Error Control Operators</title>
1683
-
<simpara>
1684
-
PHP supports one error control operator: the at sign (@). When
1685
-
prepended to an expression in PHP, any error messages that might
1686
-
be generated by that expression will be ignored.
1687
-
</simpara>
1688
-
<simpara>
1689
-
If you have set a custom error handler function with
1690
-
<function>set_error_handler</function> then it will still get
1691
-
called, but this custom error handler can (and should) call <function>error_reporting</function>
1692
-
which will return 0 when the call that triggered the error was preceded by an @.
1693
-
</simpara>
1694
-
<simpara>
1695
-
If the <link linkend="ini.track-errors"><option>track_errors</option></link>
1696
-
feature is enabled, any error message generated by the expression
1697
-
will be saved in the variable
1698
-
<varname>$php_errormsg</varname>.
1699
-
This variable will be overwritten on each error, so check early if you
1700
-
want to use it.
1701
-
</simpara>
1702
-
<para>
1703
-
<informalexample>
1704
-
<programlisting role="php">
1705
-
<![CDATA[
1706
-
<?php
1707
-
/* Intentional file error */
1708
-
$my_file = @file ('non_existent_file') or
1709
-
die ("Failed opening file: error was '$php_errormsg'");
1710
-

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

1715
-
?>
1716
-
]]>
1717
-
</programlisting>
1718
-
</informalexample>
1719
-
</para>
1720
-
<note>
1721
-
<simpara>
1722
-
The @-operator works only on
1723
-
<link linkend="language.expressions">expressions</link>. A simple rule
1724
-
of thumb is: if you can take the value of something, you can prepend
1725
-
the @ operator to it. For instance, you can prepend it to variables,
1726
-
function and <function>include</function> calls, constants, and
1727
-
so forth. You cannot prepend it to function or class definitions,
1728
-
or conditional structures such as <literal>if</literal> and
1729
-
&foreach;, and so forth.
1730
-
</simpara>
1731
-
</note>
1732
-
<simpara>
1733
-
See also <function>error_reporting</function> and the manual section for
1734
-
<link linkend="ref.errorfunc">Error Handling and Logging functions</link>.
1735
-
</simpara>
1736
-
<warning>
1737
-
<para>
1738
-
Currently the "@" error-control operator prefix will even disable
1739
-
error reporting for critical errors that will terminate script
1740
-
execution. Among other things, this means that if you use "@" to
1741
-
suppress errors from a certain function and either it isn't
1742
-
available or has been mistyped, the script will die right there
1743
-
with no indication as to why.
1744
-
</para>
1745
-
</warning>
1746
-
</sect1>
1747
-

1748
-
<sect1 xml:id="language.operators.execution">
1749
-
<title>Execution Operators</title>
1750
-
<para>
1751
-
PHP supports one execution operator: backticks (``). Note that
1752
-
these are not single-quotes! PHP will attempt to execute the
1753
-
contents of the backticks as a shell command; the output will be
1754
-
returned (i.e., it won't simply be dumped to output; it can be
1755
-
assigned to a variable). Use of the backtick operator is identical
1756
-
to <function>shell_exec</function>.
1757
-
<informalexample>
1758
-
<programlisting role="php">
1759
-
<![CDATA[
1760
-
<?php
1761
-
$output = `ls -al`;
1762
-
echo "<pre>$output</pre>";
1763
-
?>
1764
-
]]>
1765
-
</programlisting>
1766
-
</informalexample>
1767
-
</para>
1768
-
<note>
1769
-
<para>
1770
-
The backtick operator is disabled when &safemode; is enabled
1771
-
or <function>shell_exec</function> is disabled.
1772
-
</para>
1773
-
</note>
1774
-
<note>
1775
-
<para>
1776
-
Unlike some other languages, backticks have no special meaning
1777
-
within double-quoted strings.
1778
-
</para>
1779
-
</note>
1780
-
<para>
1781
-
See also the manual section on <link linkend="ref.exec">Program
1782
-
Execution functions</link>, <function>popen</function>
1783
-
<function>proc_open</function>, and
1784
-
<link linkend="features.commandline">Using PHP from the
1785
-
commandline</link>.
1786
-
</para>
1787
-
</sect1>
1788
-

1789
-
<sect1 xml:id="language.operators.increment">
1790
-
<title>Incrementing/Decrementing Operators</title>
1791
-
<para>
1792
-
PHP supports C-style pre- and post-increment and decrement
1793
-
operators.
1794
-
</para>
1795
-
<note>
1796
-
<simpara>
1797
-
The increment/decrement operators only affect numbers and strings.
1798
-
Arrays, objects and resources are not affected.
1799
-
Decrementing &null; values has no effect too, but incrementing them
1800
-
results in <literal>1</literal>.
1801
-
</simpara>
1802
-
</note>
1803
-
<table>
1804
-
<title>Increment/decrement Operators</title>
1805
-
<tgroup cols="3">
1806
-
<thead>
1807
-
<row>
1808
-
<entry>Example</entry>
1809
-
<entry>Name</entry>
1810
-
<entry>Effect</entry>
1811
-
</row>
1812
-
</thead>
1813
-
<tbody>
1814
-
<row>
1815
-
<entry>++$a</entry>
1816
-
<entry>Pre-increment</entry>
1817
-
<entry>Increments <varname>$a</varname> by one, then returns <varname>$a</varname>.</entry>
1818
-
</row>
1819
-
<row>
1820
-
<entry>$a++</entry>
1821
-
<entry>Post-increment</entry>
1822
-
<entry>Returns <varname>$a</varname>, then increments <varname>$a</varname> by one.</entry>
1823
-
</row>
1824
-
<row>
1825
-
<entry>--$a</entry>
1826
-
<entry>Pre-decrement</entry>
1827
-
<entry>Decrements <varname>$a</varname> by one, then returns <varname>$a</varname>.</entry>
1828
-
</row>
1829
-
<row>
1830
-
<entry>$a--</entry>
1831
-
<entry>Post-decrement</entry>
1832
-
<entry>Returns <varname>$a</varname>, then decrements <varname>$a</varname> by one.</entry>
1833
-
</row>
1834
-
</tbody>
1835
-
</tgroup>
1836
-
</table>
1837
-
<para>
1838
-
Here's a simple example script:
1839
-
<informalexample>
1840
-
<programlisting role="php">
1841
-
<![CDATA[
1842
-
<?php
1843
-
echo "<h3>Postincrement</h3>";
1844
-
$a = 5;
1845
-
echo "Should be 5: " . $a++ . "<br />\n";
1846
-
echo "Should be 6: " . $a . "<br />\n";
1847
-

1848
-
echo "<h3>Preincrement</h3>";
1849
-
$a = 5;
1850
-
echo "Should be 6: " . ++$a . "<br />\n";
1851
-
echo "Should be 6: " . $a . "<br />\n";
1852
-

1853
-
echo "<h3>Postdecrement</h3>";
1854
-
$a = 5;
1855
-
echo "Should be 5: " . $a-- . "<br />\n";
1856
-
echo "Should be 4: " . $a . "<br />\n";
1857
-

1858
-
echo "<h3>Predecrement</h3>";
1859
-
$a = 5;
1860
-
echo "Should be 4: " . --$a . "<br />\n";
1861
-
echo "Should be 4: " . $a . "<br />\n";
1862
-
?>
1863
-
]]>
1864
-
</programlisting>
1865
-
</informalexample>
1866
-
</para>
1867
-
<para>
1868
-
PHP follows Perl's convention when dealing with arithmetic operations
1869
-
on character variables and not C's. For example, in PHP and Perl
1870
-
<literal>$a = 'Z'; $a++;</literal> turns <literal>$a</literal> into <literal>'AA'</literal>, while in C
1871
-
<literal>a = 'Z'; a++;</literal> turns <literal>a</literal> into <literal>'['</literal>
1872
-
(ASCII value of <literal>'Z'</literal> is 90, ASCII value of <literal>'['</literal> is 91).
1873
-
Note that character variables can be incremented but not decremented and
1874
-
even so only plain ASCII alphabets and digits (a-z, A-Z and 0-9) are supported.
1875
-
Incrementing/decrementing other character variables has no effect, the
1876
-
original string is unchanged.
1877
-
<example>
1878
-
<title>Arithmetic Operations on Character Variables</title>
1879
-
<programlisting role="php">
1880
-
<![CDATA[
1881
-
<?php
1882
-
echo '== Alphabets ==' . PHP_EOL;
1883
-
$s = 'W';
1884
-
for ($n=0; $n<6; $n++) {
1885
-
echo ++$s . PHP_EOL;
1886
-
}
1887
-
// Digit characters behave differently
1888
-
echo '== Digits ==' . PHP_EOL;
1889
-
$d = 'A8';
1890
-
for ($n=0; $n<6; $n++) {
1891
-
echo ++$d . PHP_EOL;
1892
-
}
1893
-
$d = 'A08';
1894
-
for ($n=0; $n<6; $n++) {
1895
-
echo ++$d . PHP_EOL;
1896
-
}
1897
-
?>
1898
-
]]>
1899
-
</programlisting>
1900
-
&example.outputs;
1901
-
<screen>
1902
-
<![CDATA[
1903
-
== Characters ==
1904
-
X
1905
-
Y
1906
-
Z
1907
-
AA
1908
-
AB
1909
-
AC
1910
-
== Digits ==
1911
-
A9
1912
-
B0
1913
-
B1
1914
-
B2
1915
-
B3
1916
-
B4
1917
-
A09
1918
-
A10
1919
-
A11
1920
-
A12
1921
-
A13
1922
-
A14
1923
-
]]>
1924
-
</screen>
1925
-
</example>
1926
-
</para>
1927
-
<para>
1928
-
Incrementing or decrementing booleans has no effect.
1929
-
</para>
1930
-
</sect1>
1931
-

1932
-
<sect1 xml:id="language.operators.logical">
1933
-
<title>Logical Operators</title>
1934
-

1935
-
<table>
1936
-
<title>Logical Operators</title>
1937
-
<tgroup cols="3">
1938
-
<thead>
1939
-
<row>
1940
-
<entry>Example</entry>
1941
-
<entry>Name</entry>
1942
-
<entry>Result</entry>
1943
-
</row>
1944
-
</thead>
1945
-
<tbody>
1946
-
<row>
1947
-
<entry>$a and $b</entry>
1948
-
<entry>And</entry>
1949
-
<entry>&true; if both <varname>$a</varname> and <varname>$b</varname> are &true;.</entry>
1950
-
</row>
1951
-
<row>
1952
-
<entry>$a or $b</entry>
1953
-
<entry>Or</entry>
1954
-
<entry>&true; if either <varname>$a</varname> or <varname>$b</varname> is &true;.</entry>
1955
-
</row>
1956
-
<row>
1957
-
<entry>$a xor $b</entry>
1958
-
<entry>Xor</entry>
1959
-
<entry>&true; if either <varname>$a</varname> or <varname>$b</varname> is &true;, but not both.</entry>
1960
-
</row>
1961
-
<row>
1962
-
<entry>! $a</entry>
1963
-
<entry>Not</entry>
1964
-
<entry>&true; if <varname>$a</varname> is not &true;.</entry>
1965
-
</row>
1966
-
<row>
1967
-
<entry>$a &amp;&amp; $b</entry>
1968
-
<entry>And</entry>
1969
-
<entry>&true; if both <varname>$a</varname> and <varname>$b</varname> are &true;.</entry>
1970
-
</row>
1971
-
<row>
1972
-
<entry>$a || $b</entry>
1973
-
<entry>Or</entry>
1974
-
<entry>&true; if either <varname>$a</varname> or <varname>$b</varname> is &true;.</entry>
1975
-
</row>
1976
-
</tbody>
1977
-
</tgroup>
1978
-
</table>
1979
-
<simpara>
1980
-
The reason for the two different variations of "and" and "or"
1981
-
operators is that they operate at different precedences. (See
1982
-
<link linkend="language.operators.precedence">Operator
1983
-
Precedence</link>.)
1984
-
</simpara>
1985
-
<example>
1986
-
<title>Logical operators illustrated</title>
1987
-
<programlisting role="php">
1988
-
<![CDATA[
1989
-
<?php
1990
-

1991
-
// --------------------
1992
-
// foo() will never get called as those operators are short-circuit
1993
-

1994
-
$a = (false && foo());
1995
-
$b = (true || foo());
1996
-
$c = (false and foo());
1997
-
$d = (true or foo());
1998
-

1999
-
// --------------------
2000
-
// "||" has a greater precedence than "or"
2001
-

2002
-
// The result of the expression (false || true) is assigned to $e
2003
-
// Acts like: ($e = (false || true))
2004
-
$e = false || true;
2005
-

2006
-
// The constant false is assigned to $f before the "or" operation occurs
2007
-
// Acts like: (($f = false) or true)
2008
-
$f = false or true;
2009
-

2010
-
var_dump($e, $f);
2011
-

2012
-
// --------------------
2013
-
// "&&" has a greater precedence than "and"
2014
-

2015
-
// The result of the expression (true && false) is assigned to $g
2016
-
// Acts like: ($g = (true && false))
2017
-
$g = true && false;
2018
-

2019
-
// The constant true is assigned to $h before the "and" operation occurs
2020
-
// Acts like: (($h = true) and false)
2021
-
$h = true and false;
2022
-

2023
-
var_dump($g, $h);
2024
-
?>
2025
-
]]>
2026
-
</programlisting>
2027
-
&example.outputs.similar;
2028
-
<screen>
2029
-
<![CDATA[
2030
-
bool(true)
2031
-
bool(false)
2032
-
bool(false)
2033
-
bool(true)
2034
-
]]>
2035
-
</screen>
2036
-
</example>
2037
-
</sect1>
2038
-

2039
-
<sect1 xml:id="language.operators.string">
2040
-
<title>String Operators</title>
2041
-
<simpara>
2042
-
There are two <type>string</type> operators. The first is the
2043
-
concatenation operator ('.'), which returns the concatenation of its
2044
-
right and left arguments. The second is the concatenating assignment
2045
-
operator ('<literal>.=</literal>'), which appends the argument on the right side to
2046
-
the argument on the left side. Please read <link
2047
-
linkend="language.operators.assignment">Assignment
2048
-
Operators</link> for more information.
2049
-
</simpara>
2050
-

2051
-
<para>
2052
-
<informalexample>
2053
-
<programlisting role="php">
2054
-
<![CDATA[
2055
-
<?php
2056
-
$a = "Hello ";
2057
-
$b = $a . "World!"; // now $b contains "Hello World!"
2058
-

2059
-
$a = "Hello ";
2060
-
$a .= "World!"; // now $a contains "Hello World!"
2061
-
?>
2062
-
]]>
2063
-
</programlisting>
2064
-
</informalexample>
2065
-
</para>
2066
-
<para>
2067
-
See also the manual sections on the
2068
-
<link linkend="language.types.string">String type</link> and
2069
-
<link linkend="ref.strings">String functions</link>.
2070
-
</para>
2071
-
</sect1>
2072
-

2073
-
<sect1 xml:id="language.operators.array">
2074
-
<title>Array Operators</title>
2075
-
<table>
2076
-
<title>Array Operators</title>
2077
-
<tgroup cols="3">
2078
-
<thead>
2079
-
<row>
2080
-
<entry>Example</entry>
2081
-
<entry>Name</entry>
2082
-
<entry>Result</entry>
2083
-
</row>
2084
-
</thead>
2085
-
<tbody>
2086
-
<row>
2087
-
<entry>$a + $b</entry>
2088
-
<entry>Union</entry>
2089
-
<entry>Union of <varname>$a</varname> and <varname>$b</varname>.</entry>
2090
-
</row>
2091
-
<row>
2092
-
<entry>$a == $b</entry>
2093
-
<entry>Equality</entry>
2094
-
<entry>&true; if <varname>$a</varname> and <varname>$b</varname> have the same key/value pairs.</entry>
2095
-
</row>
2096
-
<row>
2097
-
<entry>$a === $b</entry>
2098
-
<entry>Identity</entry>
2099
-
<entry>&true; if <varname>$a</varname> and <varname>$b</varname> have the same key/value pairs in the same
2100
-
order and of the same types.</entry>
2101
-
</row>
2102
-
<row>
2103
-
<entry>$a != $b</entry>
2104
-
<entry>Inequality</entry>
2105
-
<entry>&true; if <varname>$a</varname> is not equal to <varname>$b</varname>.</entry>
2106
-
</row>
2107
-
<row>
2108
-
<entry>$a &lt;&gt; $b</entry>
2109
-
<entry>Inequality</entry>
2110
-
<entry>&true; if <varname>$a</varname> is not equal to <varname>$b</varname>.</entry>
2111
-
</row>
2112
-
<row>
2113
-
<entry>$a !== $b</entry>
2114
-
<entry>Non-identity</entry>
2115
-
<entry>&true; if <varname>$a</varname> is not identical to <varname>$b</varname>.</entry>
2116
-
</row>
2117
-
</tbody>
2118
-
</tgroup>
2119
-
</table>
2120
-
<para>
2121
-
The <literal>+</literal> operator returns the right-hand array appended
2122
-
to the left-hand array; for keys that exist in both arrays, the elements
2123
-
from the left-hand array will be used, and the matching elements from the
2124
-
right-hand array will be ignored.
2125
-
</para>
2126
-
<para>
2127
-
<informalexample>
2128
-
<programlisting role="php">
2129
-
<![CDATA[
2130
-
<?php
2131
-
$a = array("a" => "apple", "b" => "banana");
2132
-
$b = array("a" => "pear", "b" => "strawberry", "c" => "cherry");
2133
-

2134
-
$c = $a + $b; // Union of $a and $b
2135
-
echo "Union of \$a and \$b: \n";
2136
-
var_dump($c);
2137
-

2138
-
$c = $b + $a; // Union of $b and $a
2139
-
echo "Union of \$b and \$a: \n";
2140
-
var_dump($c);
2141
-

2142
-
$a += $b; // Union of $a += $b is $a and $b
2143
-
echo "Union of \$a += \$b: \n";
2144
-
var_dump($a);
2145
-
?>
2146
-
]]>
2147
-
</programlisting>
2148
-
</informalexample>
2149
-
When executed, this script will print the following:
2150
-
<screen role="php">
2151
-
<![CDATA[
2152
-
Union of $a and $b:
2153
-
array(3) {
2154
-
["a"]=>
2155
-
string(5) "apple"
2156
-
["b"]=>
2157
-
string(6) "banana"
2158
-
["c"]=>
2159
-
string(6) "cherry"
2160
-
}
2161
-
Union of $b and $a:
2162
-
array(3) {
2163
-
["a"]=>
2164
-
string(4) "pear"
2165
-
["b"]=>
2166
-
string(10) "strawberry"
2167
-
["c"]=>
2168
-
string(6) "cherry"
2169
-
}
2170
-
Union of $a += $b:
2171
-
array(3) {
2172
-
'a' =>
2173
-
string(5) "apple"
2174
-
'b' =>
2175
-
string(6) "banana"
2176
-
'c' =>
2177
-
string(6) "cherry"
2178
-
}
2179
-
]]>
2180
-
</screen>
2181
-
</para>
2182
-
<para>
2183
-
Elements of arrays are equal for the comparison if they have the
2184
-
same key and value.
2185
-
</para>
2186
-
<para>
2187
-
<example>
2188
-
<title>Comparing arrays</title>
2189
-
<programlisting role="php">
2190
-
<![CDATA[
2191
-
<?php
2192
-
$a = array("apple", "banana");
2193
-
$b = array(1 => "banana", "0" => "apple");
2194
-

2195
-
var_dump($a == $b); // bool(true)
2196
-
var_dump($a === $b); // bool(false)
2197
-
?>
2198
-
]]>
2199
-
</programlisting>
2200
-
</example>
2201
-
</para>
2202
-
<para>
2203
-
See also the manual sections on the
2204
-
<link linkend="language.types.array">Array type</link> and
2205
-
<link linkend="ref.array">Array functions</link>.
2206
-
</para>
2207
-
</sect1>
2208
-
<sect1 xml:id="language.operators.type">
2209
-
<title>Type Operators</title>
2210
-
<para>
2211
-
<literal>instanceof</literal> is used to determine whether a PHP variable
2212
-
is an instantiated object of a certain
2213
-
<link linkend="language.oop5.basic.class">class</link>:
2214
-
<example>
2215
-
<title>Using <literal>instanceof</literal> with classes</title>
2216
-
<programlisting role="php">
2217
-
<![CDATA[
2218
-
<?php
2219
-
class MyClass
2220
-
{
2221
-
}
2222
-

2223
-
class NotMyClass
2224
-
{
2225
-
}
2226
-
$a = new MyClass;
2227
-

2228
-
var_dump($a instanceof MyClass);
2229
-
var_dump($a instanceof NotMyClass);
2230
-
?>
2231
-
]]>
2232
-
</programlisting>
2233
-
&example.outputs;
2234
-
<screen>
2235
-
<![CDATA[
2236
-
bool(true)
2237
-
bool(false)
2238
-
]]>
2239
-
</screen>
2240
-
</example>
2241
-
</para>
2242
-
<para>
2243
-
<literal>instanceof</literal> can also be used to determine whether a variable
2244
-
is an instantiated object of a class that inherits from a parent class:
2245
-
<example>
2246
-
<title>Using <literal>instanceof</literal> with inherited classes</title>
2247
-
<programlisting role="php">
2248
-
<![CDATA[
2249
-
<?php
2250
-
class ParentClass
2251
-
{
2252
-
}
2253
-

2254
-
class MyClass extends ParentClass
2255
-
{
2256
-
}
2257
-

2258
-
$a = new MyClass;
2259
-

2260
-
var_dump($a instanceof MyClass);
2261
-
var_dump($a instanceof ParentClass);
2262
-
?>
2263
-
]]>
2264
-
</programlisting>
2265
-
&example.outputs;
2266
-
<screen>
2267
-
<![CDATA[
2268
-
bool(true)
2269
-
bool(true)
2270
-
]]>
2271
-
</screen>
2272
-
</example>
2273
-
</para>
2274
-
<para>
2275
-
To check if an object is <emphasis>not</emphasis> an instanceof a class, the
2276
-
<link linkend="language.operators.logical">logical <literal>not</literal>
2277
-
operator</link> can be used.
2278
-
<example>
2279
-
<title>Using <literal>instanceof</literal> to check if object is <emphasis>not</emphasis> an
2280
-
instanceof a class</title>
2281
-
<programlisting role="php">
2282
-
<![CDATA[
2283
-
<?php
2284
-
class MyClass
2285
-
{
2286
-
}
2287
-

2288
-
$a = new MyClass;
2289
-
var_dump(!($a instanceof stdClass));
2290
-
?>
2291
-
]]>
2292
-
</programlisting>
2293
-
&example.outputs;
2294
-
<screen>
2295
-
<![CDATA[
2296
-
bool(true)
2297
-
]]>
2298
-
</screen>
2299
-
</example>
2300
-
</para>
2301
-
<para>
2302
-
Lastly, <literal>instanceof</literal> can also be used to determine whether
2303
-
a variable is an instantiated object of a class that implements an
2304
-
<link linkend="language.oop5.interfaces">interface</link>:
2305
-
<example>
2306
-
<title>Using <literal>instanceof</literal> for class</title>
2307
-
<programlisting role="php">
2308
-
<![CDATA[
2309
-
<?php
2310
-
interface MyInterface
2311
-
{
2312
-
}
2313
-

2314
-
class MyClass implements MyInterface
2315
-
{
2316
-
}
2317
-

2318
-
$a = new MyClass;
2319
-

2320
-
var_dump($a instanceof MyClass);
2321
-
var_dump($a instanceof MyInterface);
2322
-
?>
2323
-
]]>
2324
-
</programlisting>
2325
-
&example.outputs;
2326
-
<screen>
2327
-
<![CDATA[
2328
-
bool(true)
2329
-
bool(true)
2330
-
]]>
2331
-
</screen>
2332
-
</example>
2333
-
</para>
2334
-
<para>
2335
-
Although <literal>instanceof</literal> is usually used with a literal classname,
2336
-
it can also be used with another object or a string variable:
2337
-
<example>
2338
-
<title>Using <literal>instanceof</literal> with other variables</title>
2339
-
<programlisting role="php">
2340
-
<![CDATA[
2341
-
<?php
2342
-
interface MyInterface
2343
-
{
2344
-
}
2345
-

2346
-
class MyClass implements MyInterface
2347
-
{
2348
-
}
2349
-

2350
-
$a = new MyClass;
2351
-
$b = new MyClass;
2352
-
$c = 'MyClass';
2353
-
$d = 'NotMyClass';
2354
-

2355
-
var_dump($a instanceof $b); // $b is an object of class MyClass
2356
-
var_dump($a instanceof $c); // $c is a string 'MyClass'
2357
-
var_dump($a instanceof $d); // $d is a string 'NotMyClass'
2358
-
?>
2359
-
]]>
2360
-
</programlisting>
2361
-
&example.outputs;
2362
-
<screen>
2363
-
<![CDATA[
2364
-
bool(true)
2365
-
bool(true)
2366
-
bool(false)
2367
-
]]>
2368
-
</screen>
2369
-
</example>
2370
-
</para>
2371
-
<para>
2372
-
instanceof does not throw any error if the variable being tested is not
2373
-
an object, it simply returns &false;. Constants, however, are not allowed.
2374
-
<example>
2375
-
<title>Using <literal>instanceof</literal> to test other variables</title>
2376
-
<programlisting role="php">
2377
-
<![CDATA[
2378
-
<?php
2379
-
$a = 1;
2380
-
$b = NULL;
2381
-
$c = imagecreate(5, 5);
2382
-
var_dump($a instanceof stdClass); // $a is an integer
2383
-
var_dump($b instanceof stdClass); // $b is NULL
2384
-
var_dump($c instanceof stdClass); // $c is a resource
2385
-
var_dump(FALSE instanceof stdClass);
2386
-
?>
2387
-
]]>
2388
-
</programlisting>
2389
-
&example.outputs;
2390
-
<screen>
2391
-
<![CDATA[
2392
-
bool(false)
2393
-
bool(false)
2394
-
bool(false)
2395
-
PHP Fatal error: instanceof expects an object instance, constant given
2396
-
]]>
2397
-
</screen>
2398
-
</example>
2399
-
</para>
2400
-
<para>
2401
-
There are a few pitfalls to be aware of. Before PHP version 5.1.0,
2402
-
<literal>instanceof</literal> would call <function>__autoload</function>
2403
-
if the class name did not exist. In addition, if the class was not loaded,
2404
-
a fatal error would occur. This can be worked around by using a dynamic
2405
-
class reference, or a string variable containing the class name:
2406
-
<example>
2407
-
<title>Avoiding classname lookups and fatal errors with <literal>instanceof</literal> in PHP 5.0</title>
2408
-
<programlisting role="php">
2409
-
<![CDATA[
2410
-
<?php
2411
-
$d = 'NotMyClass';
2412
-
var_dump($a instanceof $d); // no fatal error here
2413
-
?>
2414
-
]]>
2415
-
</programlisting>
2416
-
&example.outputs;
2417
-
<screen>
2418
-
<![CDATA[
2419
-
bool(false)
2420
-
]]>
2421
-
</screen>
2422
-
</example>
2423
-
</para>
2424
-
<simpara>
2425
-
The <literal>instanceof</literal> operator was introduced in PHP 5.
2426
-
Before this time <function>is_a</function> was used but
2427
-
<function>is_a</function> has since been deprecated in favor of
2428
-
<literal>instanceof</literal>. Note that as of PHP 5.3.0,
2429
-
<function>is_a</function> is no longer deprecated.
2430
-
</simpara>
2431
-
<para>
2432
-
See also <function>get_class</function> and
2433
-
<function>is_a</function>.
2434
-
</para>
2435
-
</sect1>
2436
-
</chapter>
2437
-

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>
2438
47
<!-- Keep this comment at the end of the file
2439
48
Local variables:
2440
49
mode: sgml
2441
50