forked from bucksphp/bucksphp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.php
188 lines (159 loc) · 3.32 KB
/
test.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
<?php
/**
Just a bunch of junk to demonstrate PHP syntax.
*/
// this is a constant
define('VERBOSE_MODE', true);
// string variable
$animal = "dog";
// float variable
$animal_count = 2.234;
// cast float to integer
$animal_count = (int)$animal_count;
// boolean variables
$animals_might_exist = true;
$animals_might_not_exist = false;
// simple array
$other_animals = array(
"cat",
"mouse",
"walrus",
"moose",
);
// keyed array (a hash in most other languages)
$animal_colors = array(
'cat' => 'brown',
'walrus' => 'black',
'mouse' => 'pink',
'moose' => 'orange'
);
// multi-dimensional array
$multidimensional_animals = array(
'animals' => array(
'dog',
'cat',
'goat'
),
'colors' => array(
'red',
'green',
'blue'
),
'price' => 14.50
);
// an example function
// this one prints debug information inside a <pre> tag
function pre($value) {
echo '<pre>';
print_r($value);
echo '</pre><hr>';
}
// another example function
// this one takes two arguments. the second is optional
function test_truthiness($value, $name='value') {
if ( $value ) {
echo "$name is true<br>";
}
else {
echo "$name is false<br>";
}
}
// print debug info for multi-dimensional array
pre($multidimensional_animals);
// print a single item from the multi-dimensional array
echo $multidimensional_animals['animals'][2];
echo '<hr>';
// debugging functions
echo '<pre>';
print_r($other_animals);
var_dump($other_animals);
echo '</pre>';
echo '<hr>';
// print a single item from the simple array
echo $other_animals[1] . "<hr>";
// foreach loop example
foreach ( $other_animals as $i => $animal ) {
echo "$i: $animal $animal_colors[$animal]<br>";
}
echo "<hr>";
// for loop example
for ( $i = 0; $i < count($other_animals); $i += 2 ) {
echo $other_animals[$i] . "<br>";
}
echo "<hr>";
// while loop example
$i = 0;
while ( $i < count($other_animals) ) {
echo $other_animals[$i] . "<br>";
$i++;
}
echo "<hr>";
// do-while loop example
$i = 0;
do {
echo $other_animals[$i] . "<br>";
$i += 1;
}
while ( $i < 2 );
echo "<hr>";
if ( $animal_count > 0 ) {
$animals_exist = true;
}
else {
$animals_exist = false;
}
if ( $animal_count !== 13 ) {
$animal_count = $animal_count + 1;
}
// testing truthiness
$space = " ";
$null = null;
$false = false;
$empty_string = "";
test_truthiness($space, 'space');
test_truthiness($null, 'null');
test_truthiness($false, 'false');
test_truthiness($empty_string, 'empty_string');
echo "<hr>";
// if/else conditionals
if ( $animals_exist ) {
echo "hello, there are $animal_count $animal";
if ( ($animal_count > 10 || $animal_count < 4) && VERBOSE_MODE ) {
echo "<br>That's a lot (or a little)!";
}
}
else if ( $animals_might_exist || $animals_might_not_exist ) {
echo "hard to say";
}
// this will never be reached because of the previous condition
else if ( $animals_might_not_exist ) {
echo "possibly not";
}
else {
echo "animals do not exist";
}
// switches
echo "<hr>";
foreach ( $other_animals as $animal ) {
echo "<b>$animal</b>: ";
switch ( $animal ) {
case "dog":
echo "it's a dog<br>";
break;
case "cat":
echo "it's a cat<br>";
break;
case "moose":
echo "it's a moose<br>";
break;
case "walrus":
echo "it's a walrus<br>";
break;
default:
echo "i don't know this animal<br>";
}
}
echo "<hr>";
?>
<!-- HTML tag with PHP echo/print shorthard -->
<p>some words <?= 'again' ?></p>