-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08_type_casting.php
107 lines (85 loc) · 1.93 KB
/
08_type_casting.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
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
<?php
/*
* Casting in PHP is done with these statements:
* (string) - Converts to data type String
* (int) - Converts to data type Integer
* (float) - Converts to data type Float
* (bool) - Converts to data type Boolean
* (array) - Converts to data type Array
* (object) - Converts to data type Object
* (unset) - Converts to data type NULL
* */
// Cast to Integer
$a = 5.90;
$b = "kilometers 25";
$c = true;
$d = NULL;
$a = (int)$a;
$b = (int)$b;
$c = (int)$c;
$d = (int)$d;
echo "<pre/>======== Cast to Integer ======== <pre/>";
var_dump($a, $b, $c, $d);
// Cast to Float
$a = 5;
$b = "kilometers 25";
$c = true;
$d = NULL;
$a = (float)$a;
$b = (float)$b;
$c = (float)$c;
$d = (float)$d;
echo "<pre/>======== Cast to Float ======== <pre/>";
var_dump($a, $b, $c, $d);
// Cast to Boolean
$a = 5;
$b = "kilometers 25";
$c = 5.90;
$d = NULL;
$a = (bool)$a;
$b = (bool)$b;
$c = (bool)$c;
$d = (bool)$d;
echo "<pre/>======== Cast to Boolean ======== <pre/>";
var_dump($a, $b, $c, $d);
// Cast to Array
$a = 5;
$b = "kilometers 25";
$c = 5.90;
$d = NULL;
$a = (array)$a;
$b = (array)$b;
$c = (array)$c;
$d = (array)$d;
echo "<pre/>======== Cast to Array ======== <pre/>";
var_dump($a, $b, $c, $d);
// Cast to Object
$a = 5;
$b = "kilometers 25";
$c = 5.90;
$d = NULL;
$a = (object)$a;
$b = (object)$b;
$c = (object)$c;
$d = (object)$d;
echo "<pre/>======== Cast to Object ======== <pre/>";
var_dump($a, $b, $c, $d);
// Cast to NULL
$a = 5;
$b = "kilometers 25";
$c = 5.90;
$d = NULL;
// $a = (unset)$a; // (Deprecated since version 7.2)
$a = NULL;
$b = null;
$c = null;
$d = null;
echo "<pre/>======== Cast to NULL ======== <pre/>";
var_dump($a, $b, $c, $d);
// Converting Arrays into Objects:
$a = array("Volvo", "BMW", "Toyota"); // indexed array
$b = array("Peter" => "35", "Ben" => "37", "Joe" => "43"); // associative array
$a = (object)$a;
$b = (object)$b;
echo "<pre/>======== Converting Arrays into Objects ======== <pre/>";
var_dump($a, $b);