-
Notifications
You must be signed in to change notification settings - Fork 0
/
checking_if_is_numeric.php
executable file
·69 lines (63 loc) · 2.06 KB
/
checking_if_is_numeric.php
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
#!/usr/bin/php8.0
<?php
$n = null;
$b = true;
$i = 5;
$d = 2.4;
$s = "hello";
$a = [1, 2, 3];
$f = function(int $number) {
echo "number = $number\n";
};
$o = new class {
private int $number;
public function set_number(int $number): void {
$this->number = $number;
}
public function get_number(): int {
return $number;
}
};
# Determines if the given variable is a number or a numeric string.
# A PHP string is considered numeric if it can be interpreted as an int or a float.
echo "null: $n (" . is_numeric($n) . ")\n";
echo "boolean: $b (" . is_numeric($b) . ")\n";
echo "integer: $i (" . is_numeric($i) . ")\n";
echo "floating point: $d (" . is_numeric($d) . ")\n";
echo "string: $s (" . is_numeric($s) . ")\n";
echo "array (" . is_numeric($a) . ")\n";
echo "function (" . is_numeric($f) . ")\n";
echo "object (" . is_numeric($o) . ")\n";
echo "\n";
# Prior to PHP 8.0.0, a string was considered numeric only if it had leading whitespaces, if it had trailing whitespaces then the string was considered to be leading numeric.
# From PHP 7.0: The is_numeric() function will return FALSE for numeric strings in hexadecimal form (e.g. 0xf4c3b00c), as they are no longer considered as numeric strings.
$s = " hello";
echo "string: $s (" . is_numeric($s) . ")\n";
$s = "hello ";
echo "string: $s (" . is_numeric($s) . ")\n";
$s = " ";
echo "string: $s (" . is_numeric($s) . ")\n";
$s = "15";
echo "string: $s (" . is_numeric($s) . ")\n";
$s = " 15";
echo "string: $s (" . is_numeric($s) . ")\n";
$s = "15 ";
echo "string: $s (" . is_numeric($s) . ")\n";
$s = "-15";
echo "string: $s (" . is_numeric($s) . ")\n";
$s = "1.5";
echo "string: $s (" . is_numeric($s) . ")\n";
$s = "-1.5";
echo "string: $s (" . is_numeric($s) . ")\n";
$s = "2e3";
echo "string: $s (" . is_numeric($s) . ")\n";
$s = "017";
echo "string: $s (" . is_numeric($s) . ")\n";
$s = "0xf";
echo "string: $s (" . is_numeric($s) . ")\n";
$s = "0xF";
echo "string: $s (" . is_numeric($s) . ")\n";
$s = "0Xf";
echo "string: $s (" . is_numeric($s) . ")\n";
$s = "0XF";
echo "string: $s (" . is_numeric($s) . ")\n";