-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathoperators.php
More file actions
executable file
·154 lines (150 loc) · 3.8 KB
/
Copy pathoperators.php
File metadata and controls
executable file
·154 lines (150 loc) · 3.8 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
<?php
//Assignement
$a = 'by value';
$b = &$a;
echo $a;
echo PHP_EOL; //php end of line constant
echo $b;
echo PHP_EOL;
$b = 'Changed value in the reference variable';
echo $a;
echo PHP_EOL;
echo $b;
//Arithmetic
$plus = 2 + 2;
$minus = 2 - 2;
$multiplication = 2 * 2;
$division = 2 / 2;
$mod = 2 % 2;
//Bitwise
$a = 9;
$b = 10;
echo $a & $b;
// place value 128 64 32 16 8 4 2 1
// $a 0 0 0 0 1 0 0 1 =9
// $b 0 0 0 0 1 0 1 0 =10
// result 8
// only bit they share together is the 8 bit. So 8 gets returned.
$a = 36;
$b = 103;
echo $a & $b;
// place value 128 64 32 16 8 4 2 1
// $a 0 0 1 0 0 1 0 0 =36
// $b 0 1 1 0 0 1 1 1 =103
// result 32+4 = 36
// the only bits these two share together are the bits 32 and 4 which when added together return 36.
$a = 9;
$b = 10;
echo $a | $b;
// place value 128 64 32 16 8 4 2 1
// $a 0 0 0 0 1 0 0 1 =9
// $b 0 0 0 0 1 0 1 0 =10
// result 8+2+1 = 11
// 3 bits set, in the 8, 2, and 1 column.add those up 8+2+1 and you get 11
$a = 9;
$b = 10;
echo $a ^ $b;
// place value 128 64 32 16 8 4 2 1
// $a 0 0 0 0 1 0 0 1 =9
// $b 0 0 0 0 1 0 1 0 =10
// result 2+1 = 3
// the 2 bit and the 1 bit that they each have set but don't share. Soooo 2+1 = 3
// echo 5 & 3;
//Bitwise multiplication and division by the power of 2
//Multiplication by the power of 2
$x = 2;
echo $x << 1; // Outputs 4
echo $x << 2; // Outputs 8
//Division by the power of 2
$x = 4;
echo $x >> 1; // Outputs 2
echo PHP_EOL;
echo $x >> 2; // Outputs 1
//Comparison operators
//Equivalence:
'a' == 'b' //false
'1' == '1' //true
//Identity:
'a' === 'b' //false
1 === '1' //false
1 === 1 //true
//Not equivalent:
'a' != 'b' //true
'1' != '1' //false
//Not identical:
'a' !== 'b' //true
'a' !== 'a' //false
1 !== '1' //true
1 !== 1 //false
//Less than and less & equals to:
15 <= 15 //true
15 < 15 //false
15 < 16 //true
//Greater than and greater & equals:
15 >= 15 //true
15 > 15 //false
16 > 15 //true
//Spaceship operator:
1 <=> 1 //0
1 <=> 2 //-1
2 <=> 1 //1
//Null Coalescing operator
$a = ['a' => 1, 'b' => 3];
$two = $a['2'] ?? '2';
//Binary operators
//And:
$a = 1;
$b = 2;
$a === 1 && $b === 2 //true if both are true
$a === 1 and $b === 3 //false if both are true, the change here is operator precedence
//Not
var_dump(!a) //false
//Or
$a === 1 || $b === 3 //true if any of expressions evaluate to true
$a === 2 or $b === 2 //true if any of expressions evaluate to true, the change here is operator precedence
//Xor:
$a === 1 xor $b === 3 //true if only one of expressions evaluate to true
$a === 1 xor $b === 2 //false if only one of expressions evaluate to true, the change here is operator
//Execution operators
//Backticks:
$output = `ls -al`;
echo "<pre>$output</pre>";
//Error control operators
x = @fopen("/tmp/foo"); //Ignore default error messages
//String operators
//Concatenation:
$a = 'Studing';
$b = 'ZCE';
echo $a .' '. $b;
//Concatenating assignment:
$a = 'Studing';
$a .= ' ZCE';
echo $a;
//Array operators
//Union:
$a = ['one' => 1, 'two' => 2];
$b = ['one' => 2, 'two' => 4, 'three' => 3];
print_r($a + $b);
print_r($a += $b);
//Equality:
$a = ['one' => '1', 'two' => 2];
$b = ['one' => 1, 'two' => 2];
print_($a == $b); //true
print_($a === $b); //false
//Inequality:
$a = ['one' => '1', 'two' => 2];
$b = ['one' => 1, 'two' => 2];
print_($a != $b); //false
print_($a <> $b); //false
//Non-indentity:
$a = ['one' => '1', 'two' => 2];
$b = ['one' => 1, 'two' => 2];
print_($a !== $b); //true
//Type operators:
class A {}
class B extends A {}
class C {}
$b = new B;
$c = new C;
var_dump($b instanceof B); //Instance of A either B
var_dump($c instanceof A); //false