-
Notifications
You must be signed in to change notification settings - Fork 883
Expand file tree
/
Copy pathoperators.xml
More file actions
2482 lines (2314 loc) · 68.9 KB
/
Copy pathoperators.xml
File metadata and controls
2482 lines (2314 loc) · 68.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<chapter xml:id="language.operators" xmlns="http://docbook.org/ns/docbook">
<title>Operators</title>
<simpara>
An operator is something that takes one or more values (or
expressions, in programming jargon) and yields another value (so that the
construction itself becomes an expression).
</simpara>
<para>
Operators can be grouped according to the number of values they take. Unary
operators take only one value, for example <literal>!</literal> (the
<link linkend="language.operators.logical">logical not operator</link>) or
<literal>++</literal> (the
<link linkend="language.operators.increment">increment operator</link>).
Binary operators take two values, such as the familiar
<link linkend="language.operators.arithmetic">arithmetical operators</link>
<literal>+</literal> (plus) and <literal>-</literal> (minus), and the
majority of PHP operators fall into this category. Finally, there is a
single <link linkend="language.operators.comparison.ternary">ternary
operator</link>, <literal>? :</literal>, which takes three values; this is
usually referred to simply as "the ternary operator" (although it could
perhaps more properly be called the conditional operator).
</para>
<para>
A full list of PHP operators follows in the section
<link linkend="language.operators.precedence">Operator Precedence</link>.
The section also explains operator precedence and associativity, which govern
exactly how expressions containing several different operators are
evaluated.
</para>
<sect1 xml:id="language.operators.precedence">
<title>Operator Precedence</title>
<para>
The precedence of an operator specifies how "tightly" it binds two
expressions together. For example, in the expression <literal>1 +
5 * 3</literal>, the answer is <literal>16</literal> and not
<literal>18</literal> because the multiplication ("*") operator
has a higher precedence than the addition ("+") operator.
Parentheses may be used to force precedence, if necessary. For
instance: <literal>(1 + 5) * 3</literal> evaluates to
<literal>18</literal>.
</para>
<para>
When operators have equal precedence their associativity decides
how the operators are grouped. For example "-" is left-associative, so
<literal>1 - 2 - 3</literal> is grouped as <literal>(1 - 2) - 3</literal>
and evaluates to <literal>-4</literal>. "=" on the other hand is
right-associative, so <literal>$a = $b = $c</literal> is grouped as
<literal>$a = ($b = $c)</literal>.
</para>
<para>
Operators of equal precedence that are non-associative cannot be used
next to each other, for example <literal>1 < 2 > 1</literal> is
illegal in PHP. The expression <literal>1 <= 1 == 1</literal> on the
other hand is legal, because the <literal>==</literal> operator has lesser
precedence than the <literal><=</literal> operator.
</para>
<para>
Use of parentheses, even when not strictly necessary, can often increase
readability of the code by making grouping explicit rather than relying
on the implicit operator precedence and associativity.
</para>
<para>
The following table lists the operators in order of precedence, with
the highest-precedence ones at the top. Operators on the same line
have equal precedence, in which case associativity decides grouping.
<table>
<title>Operator Precedence</title>
<tgroup cols="2">
<thead>
<row>
<entry>Associativity</entry>
<entry>Operators</entry>
<entry>Additional Information</entry>
</row>
</thead>
<tbody>
<row>
<entry>non-associative</entry>
<entry>
<literal>clone</literal>
<literal>new</literal>
</entry>
<entry><link linkend="language.oop5.cloning">clone</link> and <link linkend="language.oop5.basic.new">new</link></entry>
</row>
<row>
<entry>right</entry>
<entry><literal>**</literal></entry>
<entry><link linkend="language.operators.arithmetic">arithmetic</link></entry>
</row>
<row>
<entry>right</entry>
<entry>
<literal>++</literal>
<literal>--</literal>
<literal>~</literal>
<literal>(int)</literal>
<literal>(float)</literal>
<literal>(string)</literal>
<literal>(array)</literal>
<literal>(object)</literal>
<literal>(bool)</literal>
<literal>@</literal>
</entry>
<entry>
<link linkend="language.types">types</link> and <link linkend="language.operators.increment">increment/decrement</link>
</entry>
</row>
<row>
<entry>non-associative</entry>
<entry><literal>instanceof</literal></entry>
<entry>
<link linkend="language.types">types</link>
</entry>
</row>
<row>
<entry>right</entry>
<entry><literal>!</literal></entry>
<entry>
<link linkend="language.operators.logical">logical</link>
</entry>
</row>
<row>
<entry>left</entry>
<entry>
<literal>*</literal>
<literal>/</literal>
<literal>%</literal>
</entry>
<entry>
<link linkend="language.operators.arithmetic">arithmetic</link>
</entry>
</row>
<row>
<entry>left</entry>
<entry>
<literal>+</literal>
<literal>-</literal>
<literal>.</literal>
</entry>
<entry>
<link linkend="language.operators.arithmetic">arithmetic</link>&listendand;
<link linkend="language.operators.string">string</link></entry>
</row>
<row>
<entry>left</entry>
<entry>
<literal><<</literal>
<literal>>></literal>
</entry>
<entry>
<link linkend="language.operators.bitwise">bitwise</link>
</entry>
</row>
<row>
<entry>non-associative</entry>
<entry>
<literal><</literal>
<literal><=</literal>
<literal>></literal>
<literal>>=</literal>
</entry>
<entry>
<link linkend="language.operators.comparison">comparison</link>
</entry>
</row>
<row>
<entry>non-associative</entry>
<entry>
<literal>==</literal>
<literal>!=</literal>
<literal>===</literal>
<literal>!==</literal>
<literal><></literal>
<literal><=></literal>
</entry>
<entry>
<link linkend="language.operators.comparison">comparison</link>
</entry>
</row>
<row>
<entry>left</entry>
<entry><literal>&</literal></entry>
<entry>
<link linkend="language.operators.bitwise">bitwise</link>&listendand;
<link linkend="language.references">references</link></entry>
</row>
<row>
<entry>left</entry>
<entry><literal>^</literal></entry>
<entry>
<link linkend="language.operators.bitwise">bitwise</link>
</entry>
</row>
<row>
<entry>left</entry>
<entry><literal>|</literal></entry>
<entry>
<link linkend="language.operators.bitwise">bitwise</link>
</entry>
</row>
<row>
<entry>left</entry>
<entry><literal>&&</literal></entry>
<entry>
<link linkend="language.operators.logical">logical</link>
</entry>
</row>
<row>
<entry>left</entry>
<entry><literal>||</literal></entry>
<entry>
<link linkend="language.operators.logical">logical</link>
</entry>
</row>
<row>
<entry>right</entry>
<entry><literal>??</literal></entry>
<entry>
<link linkend="language.operators.comparison.coalesce">null coalescing</link>
</entry>
</row>
<row>
<entry>left</entry>
<entry><literal>? :</literal></entry>
<entry>
<link linkend="language.operators.comparison.ternary">ternary</link>
</entry>
</row>
<row>
<entry>right</entry>
<entry>
<literal>=</literal>
<literal>+=</literal>
<literal>-=</literal>
<literal>*=</literal>
<literal>**=</literal>
<literal>/=</literal>
<literal>.=</literal>
<literal>%=</literal>
<literal>&=</literal>
<literal>|=</literal>
<literal>^=</literal>
<literal><<=</literal>
<literal>>>=</literal>
</entry>
<entry>
<link linkend="language.operators.assignment">assignment</link>
</entry>
</row>
<row>
<entry>right</entry>
<entry><literal>yield from</literal></entry>
<entry>
<link linkend="control-structures.yield.from">yield from</link>
</entry>
</row>
<row>
<entry>right</entry>
<entry><literal>yield</literal></entry>
<entry>
<link linkend="control-structures.yield">yield</link>
</entry>
</row>
<row>
<entry>left</entry>
<entry><literal>and</literal></entry>
<entry>
<link linkend="language.operators.logical">logical</link>
</entry>
</row>
<row>
<entry>left</entry>
<entry><literal>xor</literal></entry>
<entry>
<link linkend="language.operators.logical">logical</link>
</entry>
</row>
<row>
<entry>left</entry>
<entry><literal>or</literal></entry>
<entry>
<link linkend="language.operators.logical">logical</link>
</entry>
</row>
</tbody>
</tgroup>
</table>
</para>
<para>
<example>
<title>Associativity</title>
<programlisting role="php">
<![CDATA[
<?php
$a = 3 * 3 % 5; // (3 * 3) % 5 = 4
// ternary operator associativity differs from C/C++
$a = true ? 0 : true ? 1 : 2; // (true ? 0 : true) ? 1 : 2 = 2
$a = 1;
$b = 2;
$a = $b += 3; // $a = ($b += 3) -> $a = 5, $b = 5
?>
]]>
</programlisting>
</example>
</para>
<para>
Operator precedence and associativity only determine how expressions
are grouped, they do not specify an order of evaluation. PHP does not
(in the general case) specify in which order an expression is evaluated
and code that assumes a specific order of evaluation should be avoided,
because the behavior can change between versions of PHP or depending on
the surrounding code.
<example>
<title>Undefined order of evaluation</title>
<programlisting role="php">
<![CDATA[
<?php
$a = 1;
echo $a + $a++; // may print either 2 or 3
$i = 1;
$array[$i] = $i++; // may set either index 1 or 2
?>
]]>
</programlisting>
</example>
<example>
<title><literal>+</literal>, <literal>-</literal> and <literal>.</literal> have the same precedence</title>
<programlisting role="php">
<![CDATA[
<?php
$x = 4;
// this line might result in unexpected output:
echo "x minus one equals " . $x-1 . ", or so I hope\n";
// because it is evaluated like this line:
echo (("x minus one equals " . $x) - 1) . ", or so I hope\n";
// the desired precedence can be enforced by using parentheses:
echo "x minus one equals " . ($x-1) . ", or so I hope\n";
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
-1, or so I hope
-1, or so I hope
x minus one equals 3, or so I hope
]]>
</screen>
</example>
</para>
<note>
<para>
Although <literal>=</literal> has a lower precedence than
most other operators, PHP will still allow expressions
similar to the following: <literal>if (!$a = foo())</literal>,
in which case the return value of <literal>foo()</literal> is
put into <varname>$a</varname>.
</para>
</note>
</sect1>
<sect1 xml:id="language.operators.arithmetic">
<title>Arithmetic Operators</title>
<simpara>
Remember basic arithmetic from school? These work just
like those.
</simpara>
<table>
<title>Arithmetic Operators</title>
<tgroup cols="3">
<thead>
<row>
<entry>Example</entry>
<entry>Name</entry>
<entry>Result</entry>
</row>
</thead>
<tbody>
<row>
<entry>+$a</entry>
<entry>Identity</entry>
<entry>
Conversion of <varname>$a</varname> to <type>int</type> or
<type>float</type> as appropriate.
</entry>
</row>
<row>
<entry>-$a</entry>
<entry>Negation</entry>
<entry>Opposite of <varname>$a</varname>.</entry>
</row>
<row>
<entry>$a + $b</entry>
<entry>Addition</entry>
<entry>Sum of <varname>$a</varname> and <varname>$b</varname>.</entry>
</row>
<row>
<entry>$a - $b</entry>
<entry>Subtraction</entry>
<entry>Difference of <varname>$a</varname> and <varname>$b</varname>.</entry>
</row>
<row>
<entry>$a * $b</entry>
<entry>Multiplication</entry>
<entry>Product of <varname>$a</varname> and <varname>$b</varname>.</entry>
</row>
<row>
<entry>$a / $b</entry>
<entry>Division</entry>
<entry>Quotient of <varname>$a</varname> and <varname>$b</varname>.</entry>
</row>
<row>
<entry>$a % $b</entry>
<entry>Modulo</entry>
<entry>Remainder of <varname>$a</varname> divided by <varname>$b</varname>.</entry>
</row>
<row>
<entry>$a ** $b</entry>
<entry>Exponentiation</entry>
<entry>Result of raising <varname>$a</varname> to the <varname>$b</varname>'th power. Introduced in PHP 5.6.</entry>
</row>
</tbody>
</tgroup>
</table>
<simpara>
The division operator ("/") returns a float value unless the two operands
are integers (or strings that get converted to integers) and the numbers
are evenly divisible, in which case an integer value will be returned. For
integer division, see <function>intdiv</function>.
</simpara>
<simpara>
Operands of modulo are converted to integers (by stripping the decimal
part) before processing. For floating-point modulo, see
<function>fmod</function>.
</simpara>
<para>
The result of the modulo operator <literal>%</literal> has the same sign
as the dividend — that is, the result of <literal>$a % $b</literal>
will have the same sign as <varname>$a</varname>. For example:
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
echo (5 % 3)."\n"; // prints 2
echo (5 % -3)."\n"; // prints 2
echo (-5 % 3)."\n"; // prints -2
echo (-5 % -3)."\n"; // prints -2
?>
]]>
</programlisting>
</informalexample>
</para>
<simpara>
See also the manual page on
<link linkend="ref.math">Math functions</link>.
</simpara>
</sect1>
<sect1 xml:id="language.operators.assignment">
<title>Assignment Operators</title>
<simpara>
The basic assignment operator is "=". Your first inclination might
be to think of this as "equal to". Don't. It really means that the
left operand gets set to the value of the expression on the
right (that is, "gets set to").
</simpara>
<para>
The value of an assignment expression is the value assigned. That
is, the value of "<literal>$a = 3</literal>" is 3. This allows you to do some tricky
things:
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$a = ($b = 4) + 5; // $a is equal to 9 now, and $b has been set to 4.
?>
]]>
</programlisting>
</informalexample>
</para>
<para>
In addition to the basic assignment operator, there are "combined
operators" for all of the <link linkend="language.operators">binary
arithmetic</link>, array union and string operators that allow you to use a value in an
expression and then set its value to the result of that expression. For
example:
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$a = 3;
$a += 5; // sets $a to 8, as if we had said: $a = $a + 5;
$b = "Hello ";
$b .= "There!"; // sets $b to "Hello There!", just like $b = $b . "There!";
?>
]]>
</programlisting>
</informalexample>
</para>
<para>
Note that the assignment copies the original variable to the new
one (assignment by value), so changes to one will not affect the
other. This may also have relevance if you need to copy something
like a large array inside a tight loop.
</para>
<para>
An exception to the usual assignment by value behaviour within PHP occurs
with <type>object</type>s, which are assigned by reference.
Objects may be explicitly copied via the <link
linkend="language.oop5.cloning">clone</link> keyword.
</para>
<sect2 xml:id="language.operators.assignment.reference">
<title>Assignment by Reference</title>
<para>
Assignment by reference is also supported, using the
"<computeroutput>$var = &$othervar;</computeroutput>" syntax.
Assignment by reference means that both variables end up pointing at the
same data, and nothing is copied anywhere.
</para>
<para>
<example>
<title>Assigning by reference</title>
<programlisting role="php">
<![CDATA[
<?php
$a = 3;
$b = &$a; // $b is a reference to $a
print "$a\n"; // prints 3
print "$b\n"; // prints 3
$a = 4; // change $a
print "$a\n"; // prints 4
print "$b\n"; // prints 4 as well, since $b is a reference to $a, which has
// been changed
?>
]]>
</programlisting>
</example>
</para>
<para>
The <link linkend="language.oop5.basic.new">new</link>
operator returns a reference automatically, so assigning the result of
<link linkend="language.oop5.basic.new">new</link> by reference
is not allowed as of PHP 7.0.0,
results in an <constant>E_DEPRECATED</constant> message as of PHP 5.3.0, and
an <constant>E_STRICT</constant> message in earlier versions.
</para>
<para>
For example, this code will result in an error or warning:
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
class C {}
$o = &new C;
?>
]]>
</programlisting>
&example.outputs.7;
<screen>
<![CDATA[
Parse error: syntax error, unexpected 'new' (T_NEW) in …
]]>
</screen>
&example.outputs.53;
<screen>
<![CDATA[
Deprecated: Assigning the return value of new by reference is deprecated in …
]]>
</screen>
&example.outputs.5;
<screen>
<![CDATA[
Strict Standards: Assigning the return value of new by reference is deprecated in …
]]>
</screen>
</informalexample>
</para>
<para>
More information on references and their potential uses can be found in
the <link linkend="language.references">References Explained</link>
section of the manual.
</para>
</sect2>
</sect1>
<sect1 xml:id="language.operators.bitwise">
<title>Bitwise Operators</title>
<simpara>
Bitwise operators allow evaluation and manipulation of specific
bits within an integer.
</simpara>
<table>
<title>Bitwise Operators</title>
<tgroup cols="3">
<thead>
<row>
<entry>Example</entry>
<entry>Name</entry>
<entry>Result</entry>
</row>
</thead>
<tbody>
<row>
<entry><userinput>$a & $b</userinput></entry>
<entry>And</entry>
<entry>Bits that are set in both <varname>$a</varname> and <varname>$b</varname> are set.</entry>
</row>
<row>
<entry><userinput>$a | $b</userinput></entry>
<entry>Or (inclusive or)</entry>
<entry>Bits that are set in either <varname>$a</varname> or <varname>$b</varname> are set.</entry>
</row>
<row>
<entry><userinput>$a ^ $b</userinput></entry>
<entry>Xor (exclusive or)</entry>
<entry>
Bits that are set in <varname>$a</varname> or <varname>$b</varname> but not both are set.
</entry>
</row>
<row>
<entry><userinput>~ $a</userinput></entry>
<entry>Not</entry>
<entry>
Bits that are set in <varname>$a</varname> are not set, and vice versa.
</entry>
</row>
<row>
<entry><userinput>$a << $b</userinput></entry>
<entry>Shift left</entry>
<entry>
Shift the bits of <varname>$a</varname> <varname>$b</varname> steps to the left (each step means
"multiply by two")
</entry>
</row>
<row>
<entry><userinput>$a >> $b</userinput></entry>
<entry>Shift right</entry>
<entry>
Shift the bits of <varname>$a</varname> <varname>$b</varname> steps to the right (each step means
"divide by two")
</entry>
</row>
</tbody>
</tgroup>
</table>
<para>
Bit shifting in PHP is arithmetic.
Bits shifted off either end are discarded.
Left shifts have zeros shifted in on the right while the sign
bit is shifted out on the left, meaning the sign of an operand
is not preserved.
Right shifts have copies of the sign bit shifted in on the left,
meaning the sign of an operand is preserved.
</para>
<para>
Use parentheses to ensure the desired
<link linkend="language.operators.precedence">precedence</link>.
For example, <literal>$a & $b == true</literal> evaluates
the equivalency then the bitwise and; while
<literal>($a & $b) == true</literal> evaluates the bitwise and
then the equivalency.
</para>
<para>
If both operands for the <literal>&</literal>, <literal>|</literal> and
<literal>^</literal> operators are strings, then the operation will be
performed on the ASCII values of the characters that make up the strings and
the result will be a string. In all other cases, both operands will be
<link linkend="language.types.integer.casting">converted to integers</link>
and the result will be an integer.
</para>
<para>
If the operand for the <literal>~</literal> operator is a string, the
operation will be performed on the ASCII values of the characters that make
up the string and the result will be a string, otherwise the operand and the
result will be treated as integers.
</para>
<para>
Both operands and the result for the <literal><<</literal> and
<literal>>></literal> operators are always treated as integers.
</para>
<para>
<informalexample>
<para>
<literallayout>
PHP's error_reporting ini setting uses bitwise values,
providing a real-world demonstration of turning
bits off. To show all errors, except for notices,
the php.ini file instructions say to use:
<userinput>E_ALL & ~E_NOTICE</userinput>
</literallayout>
</para>
<para>
<literallayout>
This works by starting with E_ALL:
<computeroutput>00000000000000000111011111111111</computeroutput>
Then taking the value of E_NOTICE...
<computeroutput>00000000000000000000000000001000</computeroutput>
... and inverting it via <literal>~</literal>:
<computeroutput>11111111111111111111111111110111</computeroutput>
Finally, it uses AND (&) to find the bits turned
on in both values:
<computeroutput>00000000000000000111011111110111</computeroutput>
</literallayout>
</para>
<para>
<literallayout>
Another way to accomplish that is using XOR (<literal>^</literal>)
to find bits that are on in only one value or the other:
<userinput>E_ALL ^ E_NOTICE</userinput>
</literallayout>
</para>
</informalexample>
</para>
<para>
<informalexample>
<para>
<literallayout>
error_reporting can also be used to demonstrate turning bits on.
The way to show just errors and recoverable errors is:
<userinput>E_ERROR | E_RECOVERABLE_ERROR</userinput>
</literallayout>
</para>
<para>
<literallayout>
This process combines E_ERROR
<computeroutput>00000000000000000000000000000001</computeroutput>
and
<computeroutput>00000000000000000001000000000000</computeroutput>
using the OR (<literal>|</literal>) operator
to get the bits turned on in either value:
<computeroutput>00000000000000000001000000000001</computeroutput>
</literallayout>
</para>
</informalexample>
</para>
<para>
<example>
<title>Bitwise AND, OR and XOR operations on integers</title>
<programlisting role="php">
<![CDATA[
<?php
/*
* Ignore the top section,
* it is just formatting to make output clearer.
*/
$format = '(%1$2d = %1$04b) = (%2$2d = %2$04b)'
. ' %3$s (%4$2d = %4$04b)' . "\n";
echo <<<EOH
--------- --------- -- ---------
result value op test
--------- --------- -- ---------
EOH;
/*
* Here are the examples.
*/
$values = array(0, 1, 2, 4, 8);
$test = 1 + 4;
echo "\n Bitwise AND \n";
foreach ($values as $value) {
$result = $value & $test;
printf($format, $result, $value, '&', $test);
}
echo "\n Bitwise Inclusive OR \n";
foreach ($values as $value) {
$result = $value | $test;
printf($format, $result, $value, '|', $test);
}
echo "\n Bitwise Exclusive OR (XOR) \n";
foreach ($values as $value) {
$result = $value ^ $test;
printf($format, $result, $value, '^', $test);
}
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
--------- --------- -- ---------
result value op test
--------- --------- -- ---------
Bitwise AND
( 0 = 0000) = ( 0 = 0000) & ( 5 = 0101)
( 1 = 0001) = ( 1 = 0001) & ( 5 = 0101)
( 0 = 0000) = ( 2 = 0010) & ( 5 = 0101)
( 4 = 0100) = ( 4 = 0100) & ( 5 = 0101)
( 0 = 0000) = ( 8 = 1000) & ( 5 = 0101)
Bitwise Inclusive OR
( 5 = 0101) = ( 0 = 0000) | ( 5 = 0101)
( 5 = 0101) = ( 1 = 0001) | ( 5 = 0101)
( 7 = 0111) = ( 2 = 0010) | ( 5 = 0101)
( 5 = 0101) = ( 4 = 0100) | ( 5 = 0101)
(13 = 1101) = ( 8 = 1000) | ( 5 = 0101)
Bitwise Exclusive OR (XOR)
( 5 = 0101) = ( 0 = 0000) ^ ( 5 = 0101)
( 4 = 0100) = ( 1 = 0001) ^ ( 5 = 0101)
( 7 = 0111) = ( 2 = 0010) ^ ( 5 = 0101)
( 1 = 0001) = ( 4 = 0100) ^ ( 5 = 0101)
(13 = 1101) = ( 8 = 1000) ^ ( 5 = 0101)
]]>
</screen>
</example>
</para>
<para>
<example>
<title>Bitwise XOR operations on strings</title>
<programlisting role="php">
<![CDATA[
<?php
echo 12 ^ 9; // Outputs '5'
echo "12" ^ "9"; // Outputs the Backspace character (ascii 8)
// ('1' (ascii 49)) ^ ('9' (ascii 57)) = #8
echo "hallo" ^ "hello"; // Outputs the ascii values #0 #4 #0 #0 #0
// 'a' ^ 'e' = #4
echo 2 ^ "3"; // Outputs 1
// 2 ^ ((int)"3") == 1
echo "2" ^ 3; // Outputs 1
// ((int)"2") ^ 3 == 1
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>Bit shifting on integers</title>
<programlisting role="php">
<![CDATA[
<?php
/*
* Here are the examples.
*/
echo "\n--- BIT SHIFT RIGHT ON POSITIVE INTEGERS ---\n";
$val = 4;
$places = 1;
$res = $val >> $places;
p($res, $val, '>>', $places, 'copy of sign bit shifted into left side');
$val = 4;
$places = 2;
$res = $val >> $places;
p($res, $val, '>>', $places);
$val = 4;
$places = 3;
$res = $val >> $places;
p($res, $val, '>>', $places, 'bits shift out right side');
$val = 4;
$places = 4;
$res = $val >> $places;
p($res, $val, '>>', $places, 'same result as above; can not shift beyond 0');
echo "\n--- BIT SHIFT RIGHT ON NEGATIVE INTEGERS ---\n";
$val = -4;
$places = 1;
$res = $val >> $places;
p($res, $val, '>>', $places, 'copy of sign bit shifted into left side');
$val = -4;
$places = 2;
$res = $val >> $places;
p($res, $val, '>>', $places, 'bits shift out right side');
$val = -4;
$places = 3;
$res = $val >> $places;
p($res, $val, '>>', $places, 'same result as above; can not shift beyond -1');
echo "\n--- BIT SHIFT LEFT ON POSITIVE INTEGERS ---\n";
$val = 4;
$places = 1;
$res = $val << $places;
p($res, $val, '<<', $places, 'zeros fill in right side');
$val = 4;
$places = (PHP_INT_SIZE * 8) - 4;
$res = $val << $places;
p($res, $val, '<<', $places);
$val = 4;
$places = (PHP_INT_SIZE * 8) - 3;
$res = $val << $places;
p($res, $val, '<<', $places, 'sign bits get shifted out');
$val = 4;
$places = (PHP_INT_SIZE * 8) - 2;
$res = $val << $places;
p($res, $val, '<<', $places, 'bits shift out left side');
echo "\n--- BIT SHIFT LEFT ON NEGATIVE INTEGERS ---\n";
$val = -4;
$places = 1;
$res = $val << $places;
p($res, $val, '<<', $places, 'zeros fill in right side');
$val = -4;
$places = (PHP_INT_SIZE * 8) - 3;
$res = $val << $places;
p($res, $val, '<<', $places);
$val = -4;
$places = (PHP_INT_SIZE * 8) - 2;
$res = $val << $places;
p($res, $val, '<<', $places, 'bits shift out left side, including sign bit');
/*
* Ignore this bottom section,
* it is just formatting to make output clearer.
*/
function p($res, $val, $op, $places, $note = '') {
$format = '%0' . (PHP_INT_SIZE * 8) . "b\n";
printf("Expression: %d = %d %s %d\n", $res, $val, $op, $places);
echo " Decimal:\n";
printf(" val=%d\n", $val);
printf(" res=%d\n", $res);
echo " Binary:\n";
printf(' val=' . $format, $val);
printf(' res=' . $format, $res);
if ($note) {
echo " NOTE: $note\n";
}
echo "\n";
}
?>
]]>
</programlisting>
&example.outputs.32bit;
<screen>
<![CDATA[
--- BIT SHIFT RIGHT ON POSITIVE INTEGERS ---
Expression: 2 = 4 >> 1
Decimal:
val=4
res=2
Binary:
val=00000000000000000000000000000100
res=00000000000000000000000000000010
NOTE: copy of sign bit shifted into left side
Expression: 1 = 4 >> 2
Decimal:
val=4
res=1
Binary:
val=00000000000000000000000000000100
res=00000000000000000000000000000001
Expression: 0 = 4 >> 3
Decimal:
val=4
res=0
Binary: