-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathch2.php
148 lines (119 loc) · 2.61 KB
/
ch2.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
<?php
/**
*
* Created by PhpStorm.
* User: Tim
* Date: 6-6-2015
* Time: 12:32
*/
include_once('generalIncludes.php');
echo '<input id="chapter" type="hidden" value="2">';
echo '<h2>Chapter 2 Functions</h2>';
echo '<h3 id="syntax">Basic Syntax</h3>';
echo 'Nothing to fiddle with in this paragraph...';
echo '<h3 id="returningvalues">Returning values</h3>';
echo 'Listing 2.1: Returning a value';
showcode(<<<'CODE'
function helloworld() {
return "Hello World"; // No output is shown
}
// Assigns the return value "Hello World" to $txt
$txt = helloworld();
echo helloworld(); // Displays "Hello World"
CODE
);
echo 'Listing 2.2: Returning and exiting early';
showcode(<<<'CODE'
function hello($who) {
echo "Hello $who";
if ($who == "World") {
return; // Nothing else in the function will be processed
}
echo ", how are you";
}
hello("World"); // Displays "Hello World"
hello("Reader"); // Displays "Hello Reader, how are you?"
CODE
);
echo 'Listing 2.3: Returning by reference';
showcode(<<<'CODE'
function &query($sql) {
$result = mysql_query($sql);
return $result;
}
// The following is incorrect and will cause PHP
// to emit a notice when called.
function &getHello() {
return "Hello World";
}
// This will also cause the warning to be
// issued when called
function &test() {
echo 'This is a test';
}
CODE
);
echo "<p>Sandbox</p>";
showcode(<<<'CODE'
// Try it yourself...
CODE
);
echo '<h3 id="variablescope">Variable scope</h3>';
echo 'Listing 2.4: Variable scope';
showcode(<<<'CODE'
$a = "Hello World";
function hi() {
$a = "Hello Reader";
$b = "How are you";
}
hi();
echo $a; // Will output Hello World
echo $b; // Will emit a notice
CODE
);
echo 'Listing 2.5: Accessing with the global statement';
showcode(<<<'CODE'
$a = "Hello";
$b = "World";
function hiworld() {
global $a, $b;
echo "$a $b";
}
hiworld(); // Displays "Hello World"
CODE
);
echo 'Listing 2.6: Accessing $GLOBALS array';
showcode(<<<'CODE'
$a = "Hello";
$b = "World";
function greetings() {
echo $GLOBALS['a'] .' '. $GLOBALS['b'];
}
greetings(); // Displays "Hello World"
CODE
);
echo 'Listing 2.7: Passing arguments';
showcode(<<<'CODE'
function hithere($who) {
echo "Hello $who";
}
hithere("World");
CODE
);
echo 'Listing 2.8: Setting argument defaults';
showcode(<<<'CODE'
function heee($who = "World") {
echo "Hello $who";
}
heee();
/* This time we pass in no argument and $who is assigned "World" by default. */
heee("Reader");
/* This time we override the default argument */
echo '';
CODE
);
echo "<p>Sandbox</p>";
showcode(<<<'CODE'
// Try it yourself...
CODE
);