1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Variable Arithmetic tests. |
4
|
|
|
* |
5
|
|
|
* @package PHPCheatsheets |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
// Prevent direct calls to this file. |
9
|
|
|
if ( ! defined( 'APP_DIR' ) ) { |
10
|
|
|
header( 'Status: 403 Forbidden' ); |
11
|
|
|
header( 'HTTP/1.1 403 Forbidden' ); |
12
|
|
|
exit(); |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
require_once APP_DIR . '/class.vartype-compare.php'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Variable Arithmetic tests. |
20
|
|
|
*/ |
21
|
|
|
class VartypeArithmetic extends VartypeCompare { |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The tests to run. |
25
|
|
|
* |
26
|
|
|
* @var array $tests Multi-dimensional array. |
27
|
|
|
* Possible lower level array keys: |
28
|
|
|
* - title Used as tab title |
29
|
|
|
* - tooltip Additional code sample for tooltip on tab |
30
|
|
|
* - url Relevant PHP Manual page |
31
|
|
|
* - arg Function arguments |
32
|
|
|
* - function Function to run |
33
|
|
|
* - Notes Array of notes on this test |
34
|
|
|
*/ |
35
|
|
|
var $tests = array( |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Operator based calculations. |
39
|
|
|
*/ |
40
|
|
|
'negate' => array( |
41
|
|
|
'title' => 'negate…', |
42
|
|
|
'tooltip' => '$a == -$b', |
43
|
|
|
'url' => 'https://php.net/language.operators.arithmetic', |
44
|
|
|
'arg' => '$a, $b', |
45
|
|
|
'function' => 'if ( ( ! is_array( $a ) && ! is_array( $b ) ) && ( ! is_object( $a ) && ! is_object( $b ) ) ) { pr_bool( $a == -$b ); } else if ( PHP_VERSION_ID >= 50006 && ( ! is_array( $a ) && ! is_array( $b ) ) && ( is_object( $a ) || is_object( $b ) ) ) { pr_bool( $a == -$b ); } else { trigger_error( \'Unsupported operand types\', E_USER_ERROR ); }', |
46
|
|
|
), |
47
|
|
|
'negate_strict' => array( |
48
|
|
|
'title' => 'negate strict…', |
49
|
|
|
'tooltip' => '$a === -$b', |
50
|
|
|
'url' => 'https://php.net/language.operators.arithmetic', |
51
|
|
|
'arg' => '$a, $b', |
52
|
|
|
'function' => 'if ( ( ! is_array( $a ) && ! is_array( $b ) ) && ( ! is_object( $a ) && ! is_object( $b ) ) ) { pr_bool( $a === -$b ); } else if ( PHP_VERSION_ID >= 50006 && ( ! is_array( $a ) && ! is_array( $b ) ) && ( is_object( $a ) || is_object( $b ) ) ) { pr_bool( $a === -$b ); } else { trigger_error( \'Unsupported operand types\', E_USER_ERROR ); }', |
53
|
|
|
), |
54
|
|
|
'sum' => array( |
55
|
|
|
'title' => '+', |
56
|
|
|
'tooltip' => '$a + $b', |
57
|
|
|
'url' => 'https://php.net/language.operators.arithmetic', |
58
|
|
|
'arg' => '$a, $b', |
59
|
|
|
'function' => 'if ( ( ( ! is_array( $a ) && ! is_array( $b ) || ( is_array( $a ) && is_array( $b ) ) ) ) && ( ! is_object( $a ) && ! is_object( $b ) ) ) { pr_var( $a + $b, \'\', true, true ); } else if ( PHP_VERSION_ID >= 50006 && ( ! is_array( $a ) && ! is_array( $b ) ) && ( is_object( $a ) || is_object( $b ) ) ) { pr_var( $a + $b, \'\', true, true ); } else { trigger_error( \'Unsupported operand types\', E_USER_ERROR ); }', |
60
|
|
|
'notes' => array( |
61
|
|
|
'<p>Take note of the fact that <code> + </code> is a valid <a href="https://php.net/language.operators.array" target="_blank">array operator</a>.</p>', |
62
|
|
|
), |
63
|
|
|
), |
64
|
|
|
'subtract' => array( |
65
|
|
|
'title' => '-', |
66
|
|
|
'tooltip' => '$a - $b', |
67
|
|
|
'url' => 'https://php.net/language.operators.arithmetic', |
68
|
|
|
'arg' => '$a, $b', |
69
|
|
|
'function' => 'if ( ( ! is_array( $a ) && ! is_array( $b ) ) && ( ! is_object( $a ) && ! is_object( $b ) ) ) { pr_var( $a - $b, \'\', true, true ); } else if ( PHP_VERSION_ID >= 50006 && ( ! is_array( $a ) && ! is_array( $b ) ) && ( is_object( $a ) || is_object( $b ) ) ) { pr_var( $a - $b, \'\', true, true ); } else { trigger_error( \'Unsupported operand types\', E_USER_ERROR ); }', |
70
|
|
|
), |
71
|
|
|
'multiply' => array( |
72
|
|
|
'title' => '*', |
73
|
|
|
'tooltip' => '$a * $b', |
74
|
|
|
'url' => 'https://php.net/language.operators.arithmetic', |
75
|
|
|
'arg' => '$a, $b', |
76
|
|
|
'function' => 'if ( ( ! is_array( $a ) && ! is_array( $b ) ) && ( ! is_object( $a ) && ! is_object( $b ) ) ) { pr_var( $a * $b, \'\', true, true ); } else if ( PHP_VERSION_ID >= 50006 && ( ! is_array( $a ) && ! is_array( $b ) ) && ( is_object( $a ) || is_object( $b ) ) ) { pr_var( $a * $b, \'\', true, true ); } else { trigger_error( \'Unsupported operand types\', E_USER_ERROR ); }', |
77
|
|
|
), |
78
|
|
|
'divide' => array( |
79
|
|
|
'title' => '/', |
80
|
|
|
'tooltip' => '$a / $b', |
81
|
|
|
'url' => 'https://php.net/language.operators.arithmetic', |
82
|
|
|
'arg' => '$a, $b', |
83
|
|
|
'function' => 'if ( ( ! is_array( $a ) && ! is_array( $b ) ) && ( ! is_object( $a ) && ! is_object( $b ) ) ) { $r = $a / $b; if ( is_bool( $r ) ) { pr_bool( $r ); } else { pr_var( $r, \'\', true, true ); } } else if ( PHP_VERSION_ID >= 50006 && ( ! is_array( $a ) && ! is_array( $b ) ) && ( is_object( $a ) || is_object( $b ) ) ) { $r = $a / $b; if ( is_bool( $r ) ) { pr_bool( $r ); } else { pr_var( $r, \'\', true, true ); } } else { trigger_error( \'Unsupported operand types\', E_USER_ERROR ); }', |
84
|
|
|
), |
85
|
|
|
'modulus' => array( |
86
|
|
|
'title' => '%', |
87
|
|
|
'tooltip' => '$a % $b', |
88
|
|
|
'url' => 'https://php.net/language.operators.arithmetic', |
89
|
|
|
'arg' => '$a, $b', |
90
|
|
|
'function' => '$r = $a % $b; if ( is_bool( $r ) ) { pr_bool( $r ); } else { pr_var( $r, \'\', true, true ); }', |
91
|
|
|
'notes' => array( |
92
|
|
|
'<p>Pre-PHP 7, a "<em>Division by zero</em>" error would be emitted as a warning. From PHP 7 onwards, this has been changed to a catchable fatal error.</p>', |
93
|
|
|
), |
94
|
|
|
), |
95
|
|
|
|
96
|
|
|
// Will be removed from $tests property from constructor if not on PHP 5.6+ to prevent parse errors. |
97
|
|
|
'pow_operator' => array( |
98
|
|
|
'title' => '**', |
99
|
|
|
'url' => 'https://php.net/language.operators.arithmetic', |
100
|
|
|
'arg' => '$a, $b', |
101
|
|
|
'function' => 'if ( PHP_VERSION_ID >= 50600 ) { pr_var( $a ** $b, \'\', true, true ); } else { print \'E: \'**\' operator not available (PHP 5.6+)\'; }', |
102
|
|
|
'notes' => array( |
103
|
|
|
'<p>This operator is only available in PHP 5.6.0+.</p>', |
104
|
|
|
), |
105
|
|
|
), |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
// Doesn't really belong in arithmetic, but for now it's the most logical place anyhow. |
109
|
|
|
'concatenate' => array( |
110
|
|
|
'title' => '.', |
111
|
|
|
'tooltip' => '$a . $b', |
112
|
|
|
'url' => 'https://php.net/language.operators.string.php', |
113
|
|
|
'arg' => '$a, $b', |
114
|
|
|
'function' => 'pr_var( $a . $b, \'\', true, true );', |
115
|
|
|
), |
116
|
|
|
|
117
|
|
|
'fmod' => array( |
118
|
|
|
'title' => 'fmod()', |
119
|
|
|
'url' => 'https://php.net/fmod', |
120
|
|
|
'arg' => '$a, $b', |
121
|
|
|
'function' => 'if ( function_exists( \'fmod\' ) ) { pr_var( fmod( $a, $b ), \'\', true, true ); } else { print \'E: not available (PHP 4.2.0+)\'; }', |
122
|
|
|
), |
123
|
|
|
|
124
|
|
|
'pow' => array( |
125
|
|
|
'title' => 'pow()', |
126
|
|
|
'url' => 'https://php.net/pow', |
127
|
|
|
'arg' => '$a, $b', |
128
|
|
|
'function' => 'pr_var( pow( $a, $b ), \'\', true, true );', |
129
|
|
|
), |
130
|
|
|
|
131
|
|
|
'intdiv' => array( |
132
|
|
|
'title' => 'intdiv()', |
133
|
|
|
'url' => 'https://php.net/intdiv', |
134
|
|
|
'arg' => '$a, $b', |
135
|
|
|
'function' => 'if ( function_exists( \'intdiv\' ) ) { pr_var( intdiv( $a, $b ), \'\', true, true ); } else { print \'E: not available (PHP 7.0.0+)\'; }', |
136
|
|
|
'notes' => array( |
137
|
|
|
'<p>The <em>intdiv()</em> function is only available in PHP 7.0.0+.</p>', |
138
|
|
|
), |
139
|
|
|
), |
140
|
|
|
); |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Calculations with BCMath. |
145
|
|
|
* |
146
|
|
|
* Will be added to $tests property from constructor if BCMath is available. |
147
|
|
|
* |
148
|
|
|
* @var array $bcmath_tests Multi-dimensional array of BC Math related tests. |
149
|
|
|
*/ |
150
|
|
|
var $bcmath_tests = array( |
151
|
|
|
|
152
|
|
|
'bcadd' => array( |
153
|
|
|
'title' => 'bcadd()', |
154
|
|
|
'url' => 'https://php.net/bcadd', |
155
|
|
|
'arg' => '$a, $b', |
156
|
|
|
'function' => '$r = bcadd( $a, $b ); if ( is_string( $r ) ) { pr_str( $r ); } else { pr_var ( $r, \'\', true, true ); }', |
157
|
|
|
'notes' => array( |
158
|
|
|
'<p>For this cheat sheet <code>bcscale()</code> has been set to 3. Remember that the default is 0.</p>', |
159
|
|
|
'<p>For a reliable implementation of all the BCMath functions which avoids a number of the common pitfalls, see <a href="https://gist.github.com/jrfnl/8449978" target="_blank">this example function</a> (gist).</p>', |
160
|
|
|
), |
161
|
|
|
), |
162
|
|
|
'bcsub' => array( |
163
|
|
|
'title' => 'bcsub()', |
164
|
|
|
'url' => 'https://php.net/bcsub', |
165
|
|
|
'arg' => '$a, $b', |
166
|
|
|
'function' => '$r = bcsub( $a, $b ); if ( is_string( $r ) ) { pr_str( $r ); } else { pr_var ( $r, \'\', true, true ); }', |
167
|
|
|
'notes' => array( |
168
|
|
|
'<p>For this cheat sheet <code>bcscale()</code> has been set to 3. Remember that the default is 0.</p>', |
169
|
|
|
'<p>For a reliable implementation of all the BCMath functions which avoids a number of the common pitfalls, see <a href="https://gist.github.com/jrfnl/8449978" target="_blank">this example function</a> (gist).</p>', |
170
|
|
|
), |
171
|
|
|
), |
172
|
|
|
'bcmul' => array( |
173
|
|
|
'title' => 'bcmul()', |
174
|
|
|
'url' => 'https://php.net/bcmul', |
175
|
|
|
'arg' => '$a, $b', |
176
|
|
|
'function' => '$r = bcmul( $a, $b ); if ( is_string( $r ) ) { pr_str( $r ); } else { pr_var ( $r, \'\', true, true ); }', |
177
|
|
|
'notes' => array( |
178
|
|
|
'<p>For this cheat sheet <code>bcscale()</code> has been set to 3. Remember that the default is 0.</p>', |
179
|
|
|
'<p>For a reliable implementation of all the BCMath functions which avoids a number of the common pitfalls, see <a href="https://gist.github.com/jrfnl/8449978" target="_blank">this example function</a> (gist).</p>', |
180
|
|
|
), |
181
|
|
|
), |
182
|
|
|
'bcdiv' => array( |
183
|
|
|
'title' => 'bcdiv()', |
184
|
|
|
'url' => 'https://php.net/bcdiv', |
185
|
|
|
'arg' => '$a, $b', |
186
|
|
|
'function' => '$r = bcdiv( $a, $b ); if ( is_string( $r ) ) { pr_str( $r ); } else { pr_var ( $r, \'\', true, true ); }', |
187
|
|
|
'notes' => array( |
188
|
|
|
'<p>For this cheat sheet <code>bcscale()</code> has been set to 3. Remember that the default is 0.</p>', |
189
|
|
|
'<p>For a reliable implementation of all the BCMath functions which avoids a number of the common pitfalls, see <a href="https://gist.github.com/jrfnl/8449978" target="_blank">this example function</a> (gist).</p>', |
190
|
|
|
), |
191
|
|
|
), |
192
|
|
|
'bcmod' => array( |
193
|
|
|
'title' => 'bcmod()', |
194
|
|
|
'url' => 'https://php.net/bcmod', |
195
|
|
|
'arg' => '$a, $b', |
196
|
|
|
'function' => '$r = bcmod( $a, $b ); if ( is_string( $r ) ) { pr_str( $r ); } else { pr_var ( $r, \'\', true, true ); }', |
197
|
|
|
'notes' => array( |
198
|
|
|
'<p>For this cheat sheet <code>bcscale()</code> has been set to 3. Remember that the default is 0.</p>', |
199
|
|
|
'<p>For a reliable implementation of all the BCMath functions which avoids a number of the common pitfalls, see <a href="https://gist.github.com/jrfnl/8449978" target="_blank">this example function</a> (gist).</p>', |
200
|
|
|
), |
201
|
|
|
), |
202
|
|
|
|
203
|
|
|
/* |
204
|
|
|
Issue #5: https://github.com/jrfnl/PHP-cheat-sheet-extended/issues/5 |
205
|
|
|
|
206
|
|
|
'bcpow' => array( |
207
|
|
|
'title' => 'bcpow()', |
208
|
|
|
'url' => 'https://php.net/bcpow', |
209
|
|
|
'arg' => '$a, $b', |
210
|
|
|
'function' => 'if ( $a != 0 && is_infinite( pow( $a, $b ) ) === false ) { $r = bcpow( $a, $b ); if ( is_string( $r ) ) { pr_str( $r ); } else { pr_var ( $r, \'\', true, true ); } } else { trigger_error( \'would result in INF and will normally exhaust memory\', E_USER_ERROR ); }', |
211
|
|
|
'notes' => array( |
212
|
|
|
'<p>For this cheat sheet <code>bcscale()</code> has been set to 3. Remember that the default is 0.</p>', |
213
|
|
|
'<p>For a reliable implementation of all the BCMath functions which avoids a number of the common pitfalls, see <a href="https://gist.github.com/jrfnl/8449978" target="_blank">this example function</a> (gist).</p>', |
214
|
|
|
), |
215
|
|
|
), |
216
|
|
|
*/ |
217
|
|
|
); |
218
|
|
|
|
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Constructor. |
222
|
|
|
*/ |
223
|
|
|
function __construct() { |
224
|
|
|
if ( PHP_VERSION_ID < 50600 ) { |
225
|
|
|
unset( $this->tests['pow_operator'] ); |
226
|
|
|
} |
227
|
|
|
if ( PHP_VERSION_ID < 70000 ) { |
228
|
|
|
unset( $this->tests['intdiv'] ); |
229
|
|
|
} |
230
|
|
|
if ( extension_loaded( 'bcmath' ) ) { |
231
|
|
|
$this->tests = array_merge( $this->tests, $this->bcmath_tests ); |
232
|
|
|
bcscale( 3 ); |
233
|
|
|
} |
234
|
|
|
parent::__construct(); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* PHP4 Constructor. |
240
|
|
|
*/ |
241
|
|
|
function VartypeArithmetic() { |
242
|
|
|
$this->__construct(); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|