-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathdata_types.php
More file actions
88 lines (63 loc) · 1.24 KB
/
data_types.php
File metadata and controls
88 lines (63 loc) · 1.24 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
<?php
$a = "99";
$b = 99;
$c = 1.99;
$d = "1909";
$e = 1;
//Convert string to Int
(int)$a;
var_dump($a + 0); //int(99)
//convert int to_float()
(float)$b;
var_dump($b + .7); // float(99.7)
//convert int_to_string() using Inline variable parsing
echo "I'd like {$b} waffles\n"; // I'd like 99 waffles
//or explicit
echo (string)$b; // 99
//convert int_to_bool()
var_dump((bool)$e); // bool(true)
echo ((bool)$c) ? 'true' : 'false'; // true
//convert int_to_array()
$array = array_map('intval', str_split($d));
var_dump($array);
//convert_to_null()
$newA = is_null($a);
var_dump($newA);
?>
<?php
//Convert to Int
function convert_to_int($input = "0"){
return $input;
}
echo convert_to_int();
?>
<?php
//Convert to float
$num = "7.5";
$rating = floatval($num);
function convert_to_float($rating){
return $rating;
}
echo convert_to_float($rating);
?>
<?php
//Convert to String
$input = 555;
function convert_to_string($input){
return "$input";
}
echo convert_to_string($input);
?>
<?php
//Convert to bool
$var = 0;
$input = boolval($var);
function convert_to_bool($input){
if(is_bool($input) === true){
echo "Booyah! A Boolean!";
} else {
echo "404 error";
}
}
echo convert_to_bool($input);
?>