File: operators.xml

package info (click to toggle)
php-doc 20100521-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 59,992 kB
  • ctags: 4,085
  • sloc: xml: 796,833; php: 21,338; cpp: 500; sh: 117; makefile: 58; awk: 28
file content (2015 lines) | stat: -rw-r--r-- 56,752 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 297272 $ -->
 <chapter xml:id="language.operators" xmlns="http://docbook.org/ns/docbook">
  <title>Operators</title>
  <simpara>
   An operator is something that you feed with one or more values (or
   expressions, in programming jargon) which yields another value (so that the
   construction itself becomes an expression). So you can think of functions
   or constructions that return a value (like print) as operators and those
   that return nothing (like echo) as any other thing.
  </simpara>
  <para>
   There are three types of operators.  Firstly there is the unary operator which
   operates on only one value, for example ! (the negation operator) or ++
   (the increment operator). The second group are termed binary operators; this
   group contains most of the operators that PHP supports, and a list follows
   below in the section <link linkend="language.operators.precedence">Operator
   Precedence</link>.
  </para>
  <para>
   The third group is the ternary operator: ?:.  It should be used to select
   between two expressions depending on a third one, rather than to select two
   sentences or paths of execution. Surrounding ternary expressions with
   parentheses is a very good idea.
  </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>. If operator precedence is equal, left to right
    associativity is used.
   </para>
   <para>
    The following table lists the precedence of operators with the
    highest-precedence operators listed at the top of the table. Operators
    on the same line have equal precedence, in which case their
    associativity decides which order to evaluate them in.
    <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>clone new</entry>
        <entry><link linkend="language.oop5.cloning">clone</link> and <link linkend="language.oop5.basic.new">new</link></entry>
       </row>
       <row>
        <entry>left</entry>
        <entry>[</entry>
        <entry><function>array</function></entry>
       </row>
       <row>
        <entry>non-associative</entry>
        <entry>++ --</entry>
        <entry>
         <link linkend="language.operators.increment">increment/decrement</link>
        </entry>
       </row>
       <row>
        <entry>right</entry>
        <entry>~ - (int) (float) (string) (array) (object) (bool) @</entry>
        <entry>
         <link linkend="language.types">types</link>
        </entry>
       </row>
       <row>
        <entry>non-associative</entry>
        <entry>instanceof</entry>
        <entry>
         <link linkend="language.types">types</link>
        </entry>
       </row>
       <row>
        <entry>right</entry>
        <entry>!</entry>
        <entry>
         <link linkend="language.operators.logical">logical</link>
        </entry>
       </row>
       <row>
        <entry>left</entry>
        <entry>* / %</entry>
        <entry>
         <link linkend="language.operators.arithmetic">arithmetic</link>
        </entry>
       </row>
       <row>
        <entry>left</entry>
        <entry>+ - .</entry>
        <entry>
         <link linkend="language.operators.arithmetic">arithmetic</link>&listendand;
         <link linkend="language.operators.string">string</link></entry>
       </row>
       <row>
        <entry>left</entry>
        <entry>&lt;&lt; &gt;&gt;</entry>
        <entry>
         <link linkend="language.operators.bitwise">bitwise</link>
        </entry>
       </row>
       <row>
        <entry>non-associative</entry>
        <entry>&lt; &lt;= &gt; &gt;= &lt;&gt;</entry>
        <entry>
         <link linkend="language.operators.comparison">comparison</link>
        </entry>
       </row>
       <row>
        <entry>non-associative</entry>
        <entry>== != === !==</entry>
        <entry>
         <link linkend="language.operators.comparison">comparison</link>
        </entry>
       </row>
       <row>
        <entry>left</entry>
        <entry>&amp;</entry>
        <entry>
         <link linkend="language.operators.bitwise">bitwise</link>&listendand;
         <link linkend="language.references">references</link></entry>
       </row>
       <row>
        <entry>left</entry>
        <entry>^</entry>
        <entry>
         <link linkend="language.operators.bitwise">bitwise</link>
        </entry>
       </row>
       <row>
        <entry>left</entry>
        <entry>|</entry>
        <entry>
         <link linkend="language.operators.bitwise">bitwise</link>
        </entry>
       </row>
       <row>
        <entry>left</entry>
        <entry>&amp;&amp;</entry>
        <entry>
         <link linkend="language.operators.logical">logical</link>
        </entry>
       </row>
       <row>
        <entry>left</entry>
        <entry>||</entry>
        <entry>
         <link linkend="language.operators.logical">logical</link>
        </entry>
       </row>
       <row>
        <entry>left</entry>
        <entry>? :</entry>
        <entry>
         <link linkend="language.operators.comparison.ternary">ternary</link>
        </entry>
       </row>
       <row>
        <entry>right</entry>
        <entry>
         = += -= *= /= .= %= &amp;= |= ^= &lt;&lt;= &gt;&gt;= =&gt;
        </entry>
        <entry>
         <link linkend="language.operators.assignment">assignment</link>
        </entry>
       </row>
       <row>
        <entry>left</entry>
        <entry>and</entry>
        <entry>
         <link linkend="language.operators.logical">logical</link>
        </entry>
       </row>
       <row>
        <entry>left</entry>
        <entry>xor</entry>
        <entry>
         <link linkend="language.operators.logical">logical</link>
        </entry>
       </row>
       <row>
        <entry>left</entry>
        <entry>or</entry>
        <entry>
         <link linkend="language.operators.logical">logical</link>
        </entry>
       </row>
       <row>
        <entry>left</entry>
        <entry>,</entry>
        <entry>many uses</entry>
       </row>
      </tbody>
     </tgroup>
    </table>
   </para>
   <para>
    Left associativity means that the expression is evaluated from left to right,
    right associativity means the opposite.
    <example>
     <title>Associativity</title>
     <programlisting role="php">
<![CDATA[
<?php
$a = 3 * 3 % 5; // (3 * 3) % 5 = 4
$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>
    Use parentheses to increase readability of the code.
   </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>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>Modulus</entry>
       <entry>Remainder of <varname>$a</varname> divided by <varname>$b</varname>.</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.
   </simpara>
   <simpara>
    Operands of modulus are converted to integers (by stripping the decimal
    part) before processing.
   </simpara>
   <note>
    <simpara>
     Remainder <literal>$a % $b</literal> is negative for negative
     <literal>$a</literal>.
    </simpara>
   </note>
   <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
    rights (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>
    For <type>arrays</type>, assigning a value to a named key is performed using
    the "=&gt;" operator. The <link linkend="language.operators.precedence">precedence</link>
    of this operator is the same as other assignment operators.
   </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. Assignment
    by reference is also supported, using the <computeroutput>$var =
    &amp;$othervar;</computeroutput> syntax.
    'Assignment by reference' means that both variables end
    up pointing at the same data, and nothing is copied anywhere.
    To learn more about references, please read <link
    linkend="language.references">References explained</link>. As of
    PHP 5, objects are assigned by reference unless explicitly told
    otherwise with the new <link linkend="language.oop5.cloning">clone</link>
    keyword.
   </para>
  </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 &amp; $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 &lt;&lt; $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 &gt;&gt; $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 &amp; $b == true</literal> evaluates
    the equivalency then the bitwise and; while
    <literal>($a &amp; $b) == true</literal> evaluates the bitwise and
    then the equivalency.
   </para>
   <para>
    Be aware of data type conversions. If both the left-hand and
    right-hand parameters are strings, the bitwise operator will
    operate on the characters' ASCII values.
   </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 &amp; ~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 (&amp;) 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:
  val=00000000000000000000000000000100
  res=00000000000000000000000000000000
 NOTE: bits shift out right side

Expression: 0 = 4 >> 4
 Decimal:
  val=4
  res=0
 Binary:
  val=00000000000000000000000000000100
  res=00000000000000000000000000000000
 NOTE: same result as above; can not shift beyond 0


--- BIT SHIFT RIGHT ON NEGATIVE INTEGERS ---
Expression: -2 = -4 >> 1
 Decimal:
  val=-4
  res=-2
 Binary:
  val=11111111111111111111111111111100
  res=11111111111111111111111111111110
 NOTE: copy of sign bit shifted into left side

Expression: -1 = -4 >> 2
 Decimal:
  val=-4
  res=-1
 Binary:
  val=11111111111111111111111111111100
  res=11111111111111111111111111111111
 NOTE: bits shift out right side

Expression: -1 = -4 >> 3
 Decimal:
  val=-4
  res=-1
 Binary:
  val=11111111111111111111111111111100
  res=11111111111111111111111111111111
 NOTE: same result as above; can not shift beyond -1


--- BIT SHIFT LEFT ON POSITIVE INTEGERS ---
Expression: 8 = 4 << 1
 Decimal:
  val=4
  res=8
 Binary:
  val=00000000000000000000000000000100
  res=00000000000000000000000000001000
 NOTE: zeros fill in right side

Expression: 1073741824 = 4 << 28
 Decimal:
  val=4
  res=1073741824
 Binary:
  val=00000000000000000000000000000100
  res=01000000000000000000000000000000

Expression: -2147483648 = 4 << 29
 Decimal:
  val=4
  res=-2147483648
 Binary:
  val=00000000000000000000000000000100
  res=10000000000000000000000000000000
 NOTE: sign bits get shifted out

Expression: 0 = 4 << 30
 Decimal:
  val=4
  res=0
 Binary:
  val=00000000000000000000000000000100
  res=00000000000000000000000000000000
 NOTE: bits shift out left side


--- BIT SHIFT LEFT ON NEGATIVE INTEGERS ---
Expression: -8 = -4 << 1
 Decimal:
  val=-4
  res=-8
 Binary:
  val=11111111111111111111111111111100
  res=11111111111111111111111111111000
 NOTE: zeros fill in right side

Expression: -2147483648 = -4 << 29
 Decimal:
  val=-4
  res=-2147483648
 Binary:
  val=11111111111111111111111111111100
  res=10000000000000000000000000000000

Expression: 0 = -4 << 30
 Decimal:
  val=-4
  res=0
 Binary:
  val=11111111111111111111111111111100
  res=00000000000000000000000000000000
 NOTE: bits shift out left side, including sign bit
]]>
     </screen>
     &example.outputs.64bit;
     <screen>
<![CDATA[

--- BIT SHIFT RIGHT ON POSITIVE INTEGERS ---
Expression: 2 = 4 >> 1
 Decimal:
  val=4
  res=2
 Binary:
  val=0000000000000000000000000000000000000000000000000000000000000100
  res=0000000000000000000000000000000000000000000000000000000000000010
 NOTE: copy of sign bit shifted into left side

Expression: 1 = 4 >> 2
 Decimal:
  val=4
  res=1
 Binary:
  val=0000000000000000000000000000000000000000000000000000000000000100
  res=0000000000000000000000000000000000000000000000000000000000000001

Expression: 0 = 4 >> 3
 Decimal:
  val=4
  res=0
 Binary:
  val=0000000000000000000000000000000000000000000000000000000000000100
  res=0000000000000000000000000000000000000000000000000000000000000000
 NOTE: bits shift out right side

Expression: 0 = 4 >> 4
 Decimal:
  val=4
  res=0
 Binary:
  val=0000000000000000000000000000000000000000000000000000000000000100
  res=0000000000000000000000000000000000000000000000000000000000000000
 NOTE: same result as above; can not shift beyond 0


--- BIT SHIFT RIGHT ON NEGATIVE INTEGERS ---
Expression: -2 = -4 >> 1
 Decimal:
  val=-4
  res=-2
 Binary:
  val=1111111111111111111111111111111111111111111111111111111111111100
  res=1111111111111111111111111111111111111111111111111111111111111110
 NOTE: copy of sign bit shifted into left side

Expression: -1 = -4 >> 2
 Decimal:
  val=-4
  res=-1
 Binary:
  val=1111111111111111111111111111111111111111111111111111111111111100
  res=1111111111111111111111111111111111111111111111111111111111111111
 NOTE: bits shift out right side

Expression: -1 = -4 >> 3
 Decimal:
  val=-4
  res=-1
 Binary:
  val=1111111111111111111111111111111111111111111111111111111111111100
  res=1111111111111111111111111111111111111111111111111111111111111111
 NOTE: same result as above; can not shift beyond -1


--- BIT SHIFT LEFT ON POSITIVE INTEGERS ---
Expression: 8 = 4 << 1
 Decimal:
  val=4
  res=8
 Binary:
  val=0000000000000000000000000000000000000000000000000000000000000100
  res=0000000000000000000000000000000000000000000000000000000000001000
 NOTE: zeros fill in right side

Expression: 4611686018427387904 = 4 << 60
 Decimal:
  val=4
  res=4611686018427387904
 Binary:
  val=0000000000000000000000000000000000000000000000000000000000000100
  res=0100000000000000000000000000000000000000000000000000000000000000

Expression: -9223372036854775808 = 4 << 61
 Decimal:
  val=4
  res=-9223372036854775808
 Binary:
  val=0000000000000000000000000000000000000000000000000000000000000100
  res=1000000000000000000000000000000000000000000000000000000000000000
 NOTE: sign bits get shifted out

Expression: 0 = 4 << 62
 Decimal:
  val=4
  res=0
 Binary:
  val=0000000000000000000000000000000000000000000000000000000000000100
  res=0000000000000000000000000000000000000000000000000000000000000000
 NOTE: bits shift out left side


--- BIT SHIFT LEFT ON NEGATIVE INTEGERS ---
Expression: -8 = -4 << 1
 Decimal:
  val=-4
  res=-8
 Binary:
  val=1111111111111111111111111111111111111111111111111111111111111100
  res=1111111111111111111111111111111111111111111111111111111111111000
 NOTE: zeros fill in right side

Expression: -9223372036854775808 = -4 << 61
 Decimal:
  val=-4
  res=-9223372036854775808
 Binary:
  val=1111111111111111111111111111111111111111111111111111111111111100
  res=1000000000000000000000000000000000000000000000000000000000000000

Expression: 0 = -4 << 62
 Decimal:
  val=-4
  res=0
 Binary:
  val=1111111111111111111111111111111111111111111111111111111111111100
  res=0000000000000000000000000000000000000000000000000000000000000000
 NOTE: bits shift out left side, including sign bit
]]>
     </screen>
    </example>
   </para>
   <warning>
    <para>
     Don't right shift for more than 32 bits on 32 bits systems.
     Don't left shift in case it results to number longer than 32 bits.
     Use functions from the gmp extension for bitwise manipulation on
     numbers beyond PHP_INT_MAX.
    </para>
   </warning>
   <para>
    See also
    <function>pack</function>,
    <function>unpack</function>,
    <function>gmp_and</function>,
    <function>gmp_or</function>,
    <function>gmp_xor</function>,
    <function>gmp_testbit</function>,
    <function>gmp_clrbit</function>
   </para>
  </sect1>

  <sect1 xml:id="language.operators.comparison">
   <title>Comparison Operators</title>
   <simpara>
    Comparison operators, as their name implies, allow you to compare
    two values.  You may also be interested in viewing
    <link linkend="types.comparisons">the type comparison tables</link>,
    as they show examples of various type related comparisons.
   </simpara>
   <table>
    <title>Comparison Operators</title>
    <tgroup cols="3">
     <thead>
      <row>
       <entry>Example</entry>
       <entry>Name</entry>
       <entry>Result</entry>
      </row>
     </thead>
     <tbody>
      <row>
       <entry>$a == $b</entry>
       <entry>Equal</entry>
       <entry>&true; if <varname>$a</varname> is equal to <varname>$b</varname>.</entry>
      </row>
      <row>
       <entry>$a === $b</entry>
       <entry>Identical</entry>
       <entry>
        &true; if <varname>$a</varname> is equal to <varname>$b</varname>, and they are of the same
        type. (introduced in PHP 4)
       </entry>
      </row>
      <row>
       <entry>$a != $b</entry>
       <entry>Not equal</entry>
       <entry>&true; if <varname>$a</varname> is not equal to <varname>$b</varname>.</entry>
      </row>
      <row>
       <entry>$a &lt;&gt; $b</entry>
       <entry>Not equal</entry>
       <entry>&true; if <varname>$a</varname> is not equal to <varname>$b</varname>.</entry>
      </row>
      <row>
       <entry>$a !== $b</entry>
       <entry>Not identical</entry>
       <entry>
        &true; if <varname>$a</varname> is not equal to <varname>$b</varname>, or they are not of the same
        type. (introduced in PHP 4)
       </entry>
      </row>
      <row>
       <entry>$a &lt; $b</entry>
       <entry>Less than</entry>
       <entry>&true; if <varname>$a</varname> is strictly less than <varname>$b</varname>.</entry>
      </row>
      <row>
       <entry>$a &gt; $b</entry>
       <entry>Greater than</entry>
       <entry>&true; if <varname>$a</varname> is strictly greater than <varname>$b</varname>.</entry>
      </row>
      <row>
       <entry>$a &lt;= $b</entry>
       <entry>Less than or equal to </entry>
       <entry>&true; if <varname>$a</varname> is less than or equal to <varname>$b</varname>.</entry>
      </row>
      <row>
       <entry>$a &gt;= $b</entry>
       <entry>Greater than or equal to </entry>
       <entry>&true; if <varname>$a</varname> is greater than or equal to <varname>$b</varname>.</entry>
      </row>
     </tbody>
    </tgroup>
   </table>
   <para>
    If you compare a number with a string or the comparison involves numerical
    strings, then each string is
    <link linkend="language.types.string.conversion">converted to a number</link>
    and the comparison performed numerically. These rules also apply to the
    <link linkend="control-structures.switch">switch</link> statement. The
    type conversion does not take place when the comparison is === or !== as
    this involves comparing the type as well as the value.
    <informalexample>
     <programlisting role="php">
<![CDATA[
<?php
var_dump(0 == "a"); // 0 == 0 -> true
var_dump("1" == "01"); // 1 == 1 -> true
var_dump("10" == "1e1"); // 10 == 10 -> true
var_dump(100 == "1e2"); // 100 == 100 -> true

switch ("a") {
case 0:
    echo "0";
    break;
case "a": // never reached because "a" is already matched with 0
    echo "a";
    break;
}
?>
]]>
     </programlisting>
    </informalexample>
   </para>

   <para>
    For various types, comparison is done according to the following
    table (in order).
   </para>
   <table xml:id="language.operators.comparison.types">
    <title>Comparison with Various Types</title>
    <tgroup cols="3">
     <thead>
      <row>
       <entry>Type of Operand 1</entry>
       <entry>Type of Operand 2</entry>
       <entry>Result</entry>
      </row>
     </thead>
     <tbody>
      <row>
       <entry><type>null</type> or <type>string</type></entry>
       <entry><type>string</type></entry>
       <entry>Convert &null; to "", numerical or lexical comparison</entry>
      </row>
      <row>
       <entry><type>bool</type> or <type>null</type></entry>
       <entry>anything</entry>
       <entry>Convert to <type>bool</type>, &false; &lt; &true;</entry>
      </row>
      <row>
       <entry><type>object</type></entry>
       <entry><type>object</type></entry>
       <entry>Built-in classes can define its own comparison, different classes
        are uncomparable, same class - compare properties the same way as
        arrays (PHP 4), PHP 5 has its own <link
        linkend="language.oop5.object-comparison">explanation</link>
       </entry>
      </row>
      <row>
       <entry><type>string</type>, <type>resource</type> or <type>number</type></entry>
       <entry><type>string</type>, <type>resource</type> or <type>number</type></entry>
       <entry>Translate strings and resources to numbers, usual math</entry>
      </row>
      <row>
       <entry><type>array</type></entry>
       <entry><type>array</type></entry>
       <entry>Array with fewer members is smaller, if key from operand 1 is not
        found in operand 2 then arrays are uncomparable, otherwise - compare
        value by value (see following example)</entry>
      </row>
      <row>
       <entry><type>array</type></entry>
       <entry>anything</entry>
       <entry><type>array</type> is always greater</entry>
      </row>
      <row>
       <entry><type>object</type></entry>
       <entry>anything</entry>
       <entry><type>object</type> is always greater</entry>
      </row>
     </tbody>
    </tgroup>
   </table>
   <para>
    <example>
     <title>Transcription of standard array comparison</title>
     <programlisting role="php">
<![CDATA[
<?php
// Arrays are compared like this with standard comparison operators
function standard_array_compare($op1, $op2)
{
    if (count($op1) < count($op2)) {
        return -1; // $op1 < $op2
    } elseif (count($op1) > count($op2)) {
        return 1; // $op1 > $op2
    }
    foreach ($op1 as $key => $val) {
        if (!array_key_exists($key, $op2)) {
            return null; // uncomparable
        } elseif ($val < $op2[$key]) {
            return -1;
        } elseif ($val > $op2[$key]) {
            return 1;
        }
    }
    return 0; // $op1 == $op2
}
?>
]]>
     </programlisting>
    </example>
   </para>

   <para>
    See also <function>strcasecmp</function>,
    <function>strcmp</function>,
    <link linkend="language.operators.array">Array operators</link>,
    and the manual section on
    <link linkend="language.types">Types</link>.
   </para>

   <warning>
    <title>Comparison of floating point numbers</title>

    <para>
     Because of the way <type>float</type>s are represented internally, you
     should not test two <type>float</type>s for equality.
    </para>

    <para>
     See the documentation for <type>float</type> for more information.
    </para>
   </warning>

   <sect2 xml:id="language.operators.comparison.ternary">
    <title>Ternary Operator</title>
    <para>
     Another conditional operator is the "?:" (or ternary) operator.
     <example>
      <title>Assigning a default value</title>
      <programlisting role="php">
<![CDATA[
<?php
// Example usage for: Ternary Operator
$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];

// The above is identical to this if/else statement
if (empty($_POST['action'])) {
    $action = 'default';
} else {
    $action = $_POST['action'];
}

?>
]]>
      </programlisting>
     </example>
     The expression <literal>(expr1) ? (expr2) : (expr3)</literal>
     evaluates to <replaceable>expr2</replaceable> if
     <replaceable>expr1</replaceable> evaluates to &true;, and
     <replaceable>expr3</replaceable> if
     <replaceable>expr1</replaceable> evaluates to &false;.
    </para>
    <para>
     Since PHP 5.3, it is possible to leave out the middle part of the ternary
     operator. Expression <literal>expr1 ?: expr3</literal> returns
     <replaceable>expr1</replaceable> if <replaceable>expr1</replaceable>
     evaluates to &true;, and <replaceable>expr3</replaceable> otherwise.
    </para>
    <note>
     <simpara>
      Please note that the ternary operator is a statement, and that it
      doesn't evaluate to a variable, but to the result of a statement. This
      is important to know if you want to return a variable by reference.
      The statement <literal>return $var == 42 ? $a : $b;</literal> in a
      return-by-reference function will therefore not work and a warning is
      issued in later PHP versions.
     </simpara>
    </note>
    <note>
     <para>
      It is recommended that you avoid "stacking" ternary expressions. PHP's
      behaviour when using more than one ternary operator within a single
      statement is non-obvious:
      <example>
       <title>Non-obvious Ternary Behaviour</title>
       <programlisting role="php">
<![CDATA[
<?php
// on first glance, the following appears to output 'true'
echo (true?'true':false?'t':'f');

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

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

// here, you can see that the first expression is evaluated to 'true', which
// in turn evaluates to (bool)true, thus returning the true branch of the
// second ternary expression.
?>
]]>
       </programlisting>
      </example>
     </para>
    </note>
   </sect2>

  </sect1>

  <sect1 xml:id="language.operators.errorcontrol">
   <title>Error Control Operators</title>
   <simpara>
    PHP supports one error control operator: the at sign (@). When
    prepended to an expression in PHP, any error messages that might
    be generated by that expression will be ignored.
   </simpara>
   <simpara>
    If the <link linkend="ini.track-errors"><option>track_errors</option></link>
    feature is enabled, any error message generated by the expression
    will be saved in the variable
    <varname>$php_errormsg</varname>.
    This variable will be overwritten on each error, so check early if you
    want to use it.
   </simpara>
   <para>
    <informalexample>
     <programlisting role="php">
<![CDATA[
<?php
/* Intentional file error */
$my_file = @file ('non_existent_file') or
    die ("Failed opening file: error was '$php_errormsg'");

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

?>
]]>
     </programlisting>
    </informalexample>
   </para>
   <note>
    <simpara>
     The @-operator works only on
     <link linkend="language.expressions">expressions</link>. A simple rule
     of thumb is: if you can take the value of something, you can prepend
     the @ operator to it. For instance, you can prepend it to variables,
     function and <function>include</function> calls, constants, and
     so forth. You cannot prepend it to function or class definitions,
     or conditional structures such as <literal>if</literal> and
     &foreach;, and so forth.
    </simpara>
   </note>
   <simpara>
    See also <function>error_reporting</function> and the manual section for
    <link linkend="ref.errorfunc">Error Handling and Logging functions</link>.
   </simpara>
   <warning>
    <para>
     Currently the "@" error-control operator prefix will even disable
     error reporting for critical errors that will terminate script
     execution. Among other things, this means that if you use "@" to
     suppress errors from a certain function and either it isn't
     available or has been mistyped, the script will die right there
     with no indication as to why.
    </para>
   </warning>
  </sect1>

  <sect1 xml:id="language.operators.execution">
   <title>Execution Operators</title>
   <para>
    PHP supports one execution operator: backticks (``). Note that
    these are not single-quotes! PHP will attempt to execute the
    contents of the backticks as a shell command; the output will be
    returned (i.e., it won't simply be dumped to output; it can be
    assigned to a variable).  Use of the backtick operator is identical
    to <function>shell_exec</function>.
    <informalexample>
     <programlisting role="php">
<![CDATA[
<?php
$output = `ls -al`;
echo "<pre>$output</pre>";
?>
]]>
     </programlisting>
    </informalexample>
   </para>
   <note>
    <para>
     The backtick operator is disabled when &safemode; is enabled
     or <function>shell_exec</function> is disabled.
    </para>
   </note>
   <para>
    See also the manual section on <link linkend="ref.exec">Program
    Execution functions</link>, <function>popen</function>
    <function>proc_open</function>, and
    <link linkend="features.commandline">Using PHP from the
    commandline</link>.
   </para>
  </sect1>

  <sect1 xml:id="language.operators.increment">
   <title>Incrementing/Decrementing Operators</title>
   <para>
    PHP supports C-style pre- and post-increment and decrement
    operators.
   </para>
   <note>
    <simpara>
     The increment/decrement operators do not affect boolean values.
     Decrementing &null; values has no effect too, but incrementing them
     results in <literal>1</literal>.
    </simpara>
   </note>
   <table>
    <title>Increment/decrement Operators</title>
    <tgroup cols="3">
     <thead>
      <row>
       <entry>Example</entry>
       <entry>Name</entry>
       <entry>Effect</entry>
      </row>
     </thead>
     <tbody>
      <row>
       <entry>++$a</entry>
       <entry>Pre-increment</entry>
       <entry>Increments <varname>$a</varname> by one, then returns <varname>$a</varname>.</entry>
      </row>
      <row>
       <entry>$a++</entry>
       <entry>Post-increment</entry>
       <entry>Returns <varname>$a</varname>, then increments <varname>$a</varname> by one.</entry>
      </row>
      <row>
       <entry>--$a</entry>
       <entry>Pre-decrement</entry>
       <entry>Decrements <varname>$a</varname> by one, then returns <varname>$a</varname>.</entry>
      </row>
      <row>
       <entry>$a--</entry>
       <entry>Post-decrement</entry>
       <entry>Returns <varname>$a</varname>, then decrements <varname>$a</varname> by one.</entry>
      </row>
     </tbody>
    </tgroup>
   </table>
   <para>
    Here's a simple example script:
    <informalexample>
     <programlisting role="php">
<![CDATA[
<?php
echo "<h3>Postincrement</h3>";
$a = 5;
echo "Should be 5: " . $a++ . "<br />\n";
echo "Should be 6: " . $a . "<br />\n";

echo "<h3>Preincrement</h3>";
$a = 5;
echo "Should be 6: " . ++$a . "<br />\n";
echo "Should be 6: " . $a . "<br />\n";

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

echo "<h3>Predecrement</h3>";
$a = 5;
echo "Should be 4: " . --$a . "<br />\n";
echo "Should be 4: " . $a . "<br />\n";
?>
]]>
     </programlisting>
    </informalexample>
   </para>
   <para>
    PHP follows Perl's convention when dealing with arithmetic operations
    on character variables and not C's.  For example, in Perl
    <literal>'Z'+1</literal> turns into <literal>'AA'</literal>, while in C
    <literal>'Z'+1</literal> turns into <literal>'['</literal>
    ( <literal>ord('Z') == 90</literal>, <literal>ord('[') == 91</literal> ).
    Note that character variables can be incremented but not decremented and
    even so only plain ASCII characters (a-z and A-Z) are supported.
    <example>
     <title>Arithmetic Operations on Character Variables</title>
     <programlisting role="php">
<![CDATA[
<?php
$i = 'W';
for ($n=0; $n<6; $n++) {
    echo ++$i . "\n";
}
?>
]]>
     </programlisting>
     &example.outputs;
     <screen>
<![CDATA[
X
Y
Z
AA
AB
AC
]]>
     </screen>
    </example>
   </para>
   <para>
    Incrementing or decrementing booleans has no effect.
   </para>
  </sect1>

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

   <table>
    <title>Logical Operators</title>
    <tgroup cols="3">
     <thead>
      <row>
       <entry>Example</entry>
       <entry>Name</entry>
       <entry>Result</entry>
      </row>
     </thead>
     <tbody>
      <row>
       <entry>$a and $b</entry>
       <entry>And</entry>
       <entry>&true; if both <varname>$a</varname> and <varname>$b</varname> are &true;.</entry>
      </row>
      <row>
       <entry>$a or $b</entry>
       <entry>Or</entry>
       <entry>&true; if either <varname>$a</varname> or <varname>$b</varname> is &true;.</entry>
      </row>
      <row>
       <entry>$a xor $b</entry>
       <entry>Xor</entry>
       <entry>&true; if either <varname>$a</varname> or <varname>$b</varname> is &true;, but not both.</entry>
      </row>
      <row>
       <entry>! $a</entry>
       <entry>Not</entry>
       <entry>&true; if <varname>$a</varname> is not &true;.</entry>
      </row>
      <row>
       <entry>$a &amp;&amp; $b</entry>
       <entry>And</entry>
       <entry>&true; if both <varname>$a</varname> and <varname>$b</varname> are &true;.</entry>
      </row>
      <row>
       <entry>$a || $b</entry>
       <entry>Or</entry>
       <entry>&true; if either <varname>$a</varname> or <varname>$b</varname> is &true;.</entry>
      </row>
     </tbody>
    </tgroup>
   </table>
   <simpara>
    The reason for the two different variations of "and" and "or"
    operators is that they operate at different precedences. (See
    <link linkend="language.operators.precedence">Operator
    Precedence</link>.)
   </simpara>
   <example>
    <title>Logical operators illustrated</title>
    <programlisting role="php">
<![CDATA[
<?php

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

$a = (false && foo());
$b = (true  || foo());
$c = (false and foo());
$d = (true  or  foo());

// --------------------
// "||" has a greater precedence than "or"

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

// The constant false is assigned to $f and then true is ignored
// Acts like: (($e = false) or true)
$f = false or true;

var_dump($e, $f);

// --------------------
// "&&" has a greater precedence than "and"

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

// The constant true is assigned to $h and then false is ignored
// Acts like: (($h = true) and false)
$h = true and false;

var_dump($g, $h);
?>
]]>
    </programlisting>
    &example.outputs.similar;
    <screen>
<![CDATA[
bool(true)
bool(false)
bool(false)
bool(true)
]]>
    </screen>
   </example>
  </sect1>

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

   <para>
    <informalexample>
     <programlisting role="php">
<![CDATA[
<?php
$a = "Hello ";
$b = $a . "World!"; // now $b contains "Hello World!"

$a = "Hello ";
$a .= "World!";     // now $a contains "Hello World!"
?>
]]>
     </programlisting>
    </informalexample>
   </para>
   <para>
    See also the manual sections on the
    <link linkend="language.types.string">String type</link> and
    <link linkend="ref.strings">String functions</link>.
   </para>
  </sect1>

  <sect1 xml:id="language.operators.array">
   <title>Array Operators</title>
   <table>
    <title>Array Operators</title>
    <tgroup cols="3">
     <thead>
      <row>
       <entry>Example</entry>
       <entry>Name</entry>
       <entry>Result</entry>
      </row>
     </thead>
     <tbody>
      <row>
       <entry>$a + $b</entry>
       <entry>Union</entry>
       <entry>Union of <varname>$a</varname> and <varname>$b</varname>.</entry>
      </row>
      <row>
       <entry>$a == $b</entry>
       <entry>Equality</entry>
       <entry>&true; if <varname>$a</varname> and <varname>$b</varname> have the same key/value pairs.</entry>
      </row>
      <row>
       <entry>$a === $b</entry>
       <entry>Identity</entry>
       <entry>&true; if <varname>$a</varname> and <varname>$b</varname> have the same key/value pairs in the same
        order and of the same types.</entry>
      </row>
      <row>
       <entry>$a != $b</entry>
       <entry>Inequality</entry>
       <entry>&true; if <varname>$a</varname> is not equal to <varname>$b</varname>.</entry>
      </row>
      <row>
       <entry>$a &lt;&gt; $b</entry>
       <entry>Inequality</entry>
       <entry>&true; if <varname>$a</varname> is not equal to <varname>$b</varname>.</entry>
      </row>
      <row>
       <entry>$a !== $b</entry>
       <entry>Non-identity</entry>
       <entry>&true; if <varname>$a</varname> is not identical to <varname>$b</varname>.</entry>
      </row>
     </tbody>
    </tgroup>
   </table>
   <para>
    The <literal>+</literal> operator
    appends elements of remaining keys from the right handed array to the
    left handed, whereas duplicated keys are NOT overwritten.
   </para>
   <para>
    <informalexample>
     <programlisting role="php">
<![CDATA[
<?php
$a = array("a" => "apple", "b" => "banana");
$b = array("a" => "pear", "b" => "strawberry", "c" => "cherry");

$c = $a + $b; // Union of $a and $b
echo "Union of \$a and \$b: \n";
var_dump($c);

$c = $b + $a; // Union of $b and $a
echo "Union of \$b and \$a: \n";
var_dump($c);
?>
]]>
     </programlisting>
    </informalexample>
    When executed, this script will print the following:
    <screen role="php">
<![CDATA[
Union of $a and $b:
array(3) {
  ["a"]=>
  string(5) "apple"
  ["b"]=>
  string(6) "banana"
  ["c"]=>
  string(6) "cherry"
}
Union of $b and $a:
array(3) {
  ["a"]=>
  string(4) "pear"
  ["b"]=>
  string(10) "strawberry"
  ["c"]=>
  string(6) "cherry"
}
]]>
    </screen>
   </para>
   <para>
    Elements of arrays are equal for the comparison if they have the
    same key and value.
   </para>
   <para>
    <example>
     <title>Comparing arrays</title>
     <programlisting role="php">
<![CDATA[
<?php
$a = array("apple", "banana");
$b = array(1 => "banana", "0" => "apple");

var_dump($a == $b); // bool(true)
var_dump($a === $b); // bool(false)
?>
]]>
     </programlisting>
    </example>
   </para>
   <para>
    See also the manual sections on the
    <link linkend="language.types.array">Array type</link> and
    <link linkend="ref.array">Array functions</link>.
   </para>
  </sect1>
  <sect1 xml:id="language.operators.type">
   <title>Type Operators</title>
   <para>
    <literal>instanceof</literal> is used to determine whether a PHP variable
    is an instantiated object of a certain
    <link linkend="language.oop5.basic.class">class</link>:
    <example>
     <title>Using instanceof with classes</title>
     <programlisting role="php">
<![CDATA[
<?php
class MyClass
{
}

class NotMyClass
{
}
$a = new MyClass;

var_dump($a instanceof MyClass);
var_dump($a instanceof NotMyClass);
?>
]]>
     </programlisting>
     &example.outputs;
     <screen>
<![CDATA[
bool(true)
bool(false)
]]>
     </screen>
    </example>
   </para>
   <para>
    <literal>instanceof</literal> can also be used to determine whether a variable
    is an instantiated object of a class that inherits from a parent class:
    <example>
     <title>Using <literal>instanceof</literal> with inherited classes</title>
     <programlisting role="php">
<![CDATA[
<?php
class ParentClass
{
}

class MyClass extends ParentClass
{
}

$a = new MyClass;

var_dump($a instanceof MyClass);
var_dump($a instanceof ParentClass);
?>
]]>
     </programlisting>
     &example.outputs;
     <screen>
<![CDATA[
bool(true)
bool(true)
]]>
     </screen>
    </example>
   </para>
   <para>
    To check if an object is <emphasis>not</emphasis> an instanceof a class, the
    <link linkend="language.operators.logical">logical <literal>not</literal>
    operator</link> can be used.
    <example>
     <title>Using <literal>instanceof</literal> to check if object is <emphasis>not</emphasis> an
      instanceof a class</title>
     <programlisting role="php">
<![CDATA[
<?php
class MyClass
{
}

$a = new MyClass;
var_dump(!($a instanceof stdClass));
?>
]]>
     </programlisting>
    &example.outputs;
    <screen>
<![CDATA[
bool(true)
]]>
     </screen>
    </example>
   </para>
   <para>
    Lastly, <literal>instanceof</literal> can also be used to determine whether
    a variable is an instantiated object of a class that implements an
    <link linkend="language.oop5.interfaces">interface</link>:
    <example>
     <title>Using <literal>instanceof</literal> for class</title>
     <programlisting role="php">
<![CDATA[
<?php
interface MyInterface
{
}

class MyClass implements MyInterface
{
}

$a = new MyClass;

var_dump($a instanceof MyClass);
var_dump($a instanceof MyInterface);
?>
]]>
     </programlisting>
     &example.outputs;
     <screen>
<![CDATA[
bool(true)
bool(true)
]]>
     </screen>
    </example>
   </para>
   <para>
    Although <literal>instanceof</literal> is usually used with a literal classname,
    it can also be used with another object or a string variable:
    <example>
     <title>Using <literal>instanceof</literal> with other variables</title>
     <programlisting role="php">
<![CDATA[
<?php
interface MyInterface
{
}

class MyClass implements MyInterface
{
}

$a = new MyClass;
$b = new MyClass;
$c = 'MyClass';
$d = 'NotMyClass';

var_dump($a instanceof $b); // $b is an object of class MyClass
var_dump($a instanceof $c); // $c is a string 'MyClass'
var_dump($a instanceof $d); // $d is a string 'NotMyClass'
?>
]]>
    </programlisting>
    &example.outputs;
    <screen>
<![CDATA[
bool(true)
bool(true)
bool(false)
]]>
     </screen>
    </example>
   </para>
   <para>
    There are a few pitfalls to be aware of.  Before PHP version 5.1.0,
    <literal>instanceof</literal> would call <link linkend="language.oop5.autoload">__autoload()</link>
    if the class name did not exist.  In addition, if the class was not loaded,
    a fatal error would occur.  This can be worked around by using a dynamic
    class reference, or a string variable containing the class name:
    <example>
     <title>Avoiding classname lookups and fatal errors with <literal>instanceof</literal> in PHP 5.0</title>
     <programlisting role="php">
<![CDATA[
<?php
$d = 'NotMyClass';
var_dump($a instanceof $d); // no fatal error here
?>
     ]]>
    </programlisting>
    &example.outputs;
    <screen>
<![CDATA[
bool(false)
]]>
     </screen>
    </example>
   </para>
   <simpara>
    The <literal>instanceof</literal> operator was introduced in PHP 5.
    Before this time <function>is_a</function> was used but
    <function>is_a</function> has since been deprecated in favor of
    <literal>instanceof</literal>. Note that as of PHP 5.3.0,
    <function>is_a</function> is no longer deprecated.
   </simpara>
   <para>
    See also <function>get_class</function> and
    <function>is_a</function>.
   </para>
  </sect1>
 </chapter>

<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->