forked from 4iz278/cviceni
-
Notifications
You must be signed in to change notification settings - Fork 0
/
01-datove-typy.php
207 lines (126 loc) · 4.38 KB
/
01-datove-typy.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<h1>Booleans</h1>
<?php
// booleans
// true, TRUE, True, booleans jsou case insensitive
// http://php.net/manual/en/language.types.boolean.php
var_dump((bool) true); // bool(true)
var_dump((bool) TRue); // bool(true)
var_dump((bool) "TRUE"); // bool(true)
var_dump((bool) "1"); // bool(true)
var_dump((bool) "false"); // bool(true) // !!! pozor !!!
var_dump((bool) "FALSE"); // bool(true) // !!! pozor !!!
var_dump((bool) 1); // bool(true)
var_dump((bool) -2); // bool(true)
var_dump((bool) "foo"); // bool(true)
var_dump((bool) 2.3e5); // bool(true)
var_dump((bool) array(12)); // bool(true)
var_dump((bool) 0); // bool(false), integer
var_dump((bool) 0.0); // bool(false), float
var_dump((bool) false); // bool(false)
var_dump((bool) FALSE); // bool(false)
var_dump((bool) "0"); // bool(false) // !!! pozor, string, ale bere ho jako false, protoze je 0 !!!
var_dump((bool) "0.0"); // bool(true) // !!! pozor, string !!!
var_dump((bool) ""); // bool(false)
var_dump((bool) array()); // bool(false)
var_dump((bool) " "); // bool(true) !!! pozor, mezera je prazdny znak!
?>
<h1>Integers</h1>
<?php
//integers
$a = 1234; // decimal number
var_dump($a);
$a = -123; // a negative number
var_dump($a);
$a = 0123; // octal number (equivalent to 83 decimal)
var_dump($a);
$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
var_dump($a);
$a = 0b11111111; // binary number (equivalent to 255 decimal)
var_dump($a);
//integer overflow to float
//32-bit system
$large_number = 2147483647;
var_dump($large_number); // int(2147483647)
$large_number = 2147483648;
var_dump($large_number); // float(2147483648)
$million = 1000000;
$large_number = 50000 * $million;
var_dump($large_number); // float(50000000000)
//integer division
var_dump(21/7); // int(3)
var_dump(25/7); // float(3.5714285714286)
var_dump((int) (25/7)); // int(3)
var_dump(round(25/7)); // float(4)
?>
<h1>Floats aka doubles aka real numbers</h1>
<?php
//floats
$a = 1.234;
$b = 1.2e3;
$c = 7E-10;
// pozor na ztratu presnosti pri konverzi na binarni cisla !!!
// http://www.exploringbinary.com/why-0-point-1-does-not-exist-in-floating-point/
$d = (0.1+0.7)*10; //7.9999999999999991118
var_dump(floor($d));
// BC Math Functions
// http://php.net/manual/en/ref.bc.php
// pozor, cisla se zadavaji jako string!
bcscale(4); //pozor, musime nastavit presnost, default 0
$e = bcmul(bcadd('0.1', '0.7'), '10');
var_dump(floor($e));
// porovnavani cisel
var_dump((0.1+0.7)*10 == 8); // bool(false)
// http://php.net/manual/en/function.bccomp.php
// shoda, pokud vraci int 0
var_dump(bccomp($e, '8'));
?>
<h1>Strings</h1>
<?php
// jednoduche uvozovky ''
echo 'this is a simple string';
echo 'You can also have embedded newlines in
strings this way as it is
okay to do';
// backslash \'
// Outputs: Arnold once said: "I'll be back"
echo 'Arnold once said: "I\'ll be back"';
// backslash \\
// Outputs: You deleted C:\*.*?
echo 'You deleted C:\\*.*?';
// Outputs: You deleted C:\*.*?
echo 'You deleted C:\*.*?';
// Outputs: This will not expand: \n a newline
echo 'This will not expand: \n a newline';
// Outputs: Variables do not $expand $either
echo 'Variables do not $expand $either';
?>
<?php
//dvojite uvozovky
/* vyhodnocuji se promenne a tyto specialni znaky
\n linefeed (LF or 0x0A (10) in ASCII)
\r carriage return (CR or 0x0D (13) in ASCII)
\t horizontal tab (HT or 0x09 (9) in ASCII)
\v vertical tab (VT or 0x0B (11) in ASCII) (since PHP 5.2.5)
\e escape (ESC or 0x1B (27) in ASCII) (since PHP 5.4.0)
\f form feed (FF or 0x0C (12) in ASCII) (since PHP 5.2.5)
\\ backslash
\$ dollar sign
\"
*/
$a = "ahoj";
echo "Pozdrav: $a";
//newline
echo '<pre>Jednoduche uvozovky: Prvni radek\nDruhy radek</pre>'; //nevyhodnoti se \n
echo "<pre>Dvojite uvozovky: Prvni radek\nDruhy radek</pre>"; //vyhodnoti se \n
?>
<?php
// string conversion to numbers
$foo = 1 + "10.5"; // $foo is float (11.5)
$foo = 1 + "-1.3e3"; // $foo is float (-1299)
$foo = 1 + "bob-1.3e3"; // $foo is integer (1)
$foo = 1 + "bob3"; // $foo is integer (1)
$foo = 1 + "10 Small Pigs"; // $foo is integer (11)
$foo = 4 + "10.2 Little Piggies"; // $foo is float (14.2)
$foo = "10.0 pigs " + 1; // $foo is float (11)
$foo = "10.0 pigs " + 1.0; // $foo is float (11)
?>