Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1a9f55f

Browse files
committed
Numeric string comparison
1 parent 2ce9f7a commit 1a9f55f

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This is a list of some unextecped behaviours on developing in PHP.
55

66
It doesn't mean that's bugs. It only means you should be aware of it.
77

8+
* [Numeric string comparison](numeric_string_comparison.md)
89
* [0 to string comparison](0_to_string_comparison.md)
910
* [Change boolean constants TRUE/FALSE](change_boolean_constants.md)
1011
* [Overwriting $this when using references](overwriting_this.md)

numeric_string_comparison.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Numeric string comparison
2+
=========================
3+
4+
TODO: description
5+
6+
Example:
7+
--------
8+
(http://3v4l.org/6OPhQ)
9+
10+
```php
11+
echo "Compare non numeric string to string 0: ('str' == '0') = " . ('str' == '0' ? 'true' : 'false') . "\n";
12+
echo "Compare non numeric string to integer 0: ('str' == 0) = " . ('str' == 0 ? 'true' : 'false') . "\n";
13+
14+
echo "Compare evtl. numeric string to string: ('1string' == '1') = " . ('1string' == '1' ? 'true' : 'false') . "\n";
15+
echo "Compare evtl. numeric string to number: ('1string' == 1) = " . ('1string' == 1 ? 'true' : 'false') . "\n";
16+
17+
echo "Compare two numeric strings (same semantic 1): ('1e1' == '10') = " . ('1e1' == '10' ? 'true' : 'false') . "\n";
18+
echo "Compare two numeric strings (same semantic 2): ('1E1' == '10') = " . ('1E1' == '10' ? 'true' : 'false') . "\n";
19+
echo "Compare two numeric strings (same semantic 3): ('1e-1' == '0.1') = " . ('1e-1' == '0.1' ? 'true' : 'false') . "\n";
20+
echo "Compare two numeric strings (same semantic 4): ('1E-1' == '0.1') = " . ('1E-1' == '0.1' ? 'true' : 'false') . "\n";
21+
echo "Compare two numeric strings (same semantic 5): ('+1' == '1') = " . ('+1' == '1' ? 'true' : 'false') . "\n";
22+
echo "Compare two numeric strings (same semantic 6): ('+0' == '-0') = " . ('+0' == '-0' ? 'true' : 'false') . "\n";
23+
24+
echo "Compare two numeric strings (precision 1): ('0.99999999999999994' == '1') = " . ('0.99999999999999994' == '1' ? 'true' : 'false') . "\n";
25+
echo "Compare two numeric strings (precision 2): ('0.99999999999999995' == '1') = " . ('0.99999999999999995' == '1' ? 'true' : 'false') . "\n";
26+
```
27+
28+
```
29+
Compare non numeric string to string 0: ('str' == '0') = false
30+
Compare non numeric string to integer 0: ('str' == 0) = true
31+
Compare evtl. numeric string to string: ('1string' == '1') = false
32+
Compare evtl. numeric string to number: ('1string' == 1) = true
33+
Compare two numeric strings (same semantic 1): ('1e1' == '10') = true
34+
Compare two numeric strings (same semantic 2): ('1E1' == '10') = true
35+
Compare two numeric strings (same semantic 3): ('1e-1' == '0.1') = true
36+
Compare two numeric strings (same semantic 4): ('1E-1' == '0.1') = true
37+
Compare two numeric strings (same semantic 5): ('+1' == '1') = true
38+
Compare two numeric strings (same semantic 6): ('+0' == '-0') = true
39+
Compare two numeric strings (precision 1): ('0.99999999999999994' == '1') = false
40+
Compare two numeric strings (precision 2): ('0.99999999999999995' == '1') = true
41+
```

0 commit comments

Comments
 (0)