You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
PHP is a general-purpose dynamically-typed programming language.
32
+
PHP is a dynamically-typed programming language.
6
33
Value types are only checked while the program is running.
7
34
Using the assignment `=` operator, any value type may be assigned to any variable name.
8
35
Variable names must start with a dollar `$` sign.
36
+
To dig deeper into variables, see [PHP variable docs][php-variables].
9
37
10
38
```php
11
-
<?php
12
-
$count = 1 // Assigned an integer value of 1
13
-
$count = 2 // Re-assigned a new value to the variable
39
+
$count = 1; // Assign an integer value of 1
40
+
$count = 2; // Re-assign a new value to the variable
14
41
15
-
$count = false // You may assign any value type
42
+
$count = false; // You may assign any value type
16
43
17
44
// Strings can be created by enclosing the characters
18
45
// within single `'` quotes or double `"` quotes.
19
-
$message = "Success!"
46
+
$message = "Success!";
20
47
```
21
48
49
+
## Functions and Methods
50
+
51
+
Literal values and values stored in variables can be passed to functions.
52
+
New variables are created when defining function arguments to hold values passed in.
53
+
54
+
```php
55
+
// Declare the window_width function
56
+
function window_width($width)
57
+
{
58
+
echo $width; // Access the value passed into the function as $width
59
+
// ...
60
+
}
61
+
62
+
window_width(100); // Call the function with the literal value 100
63
+
64
+
$newWidth = 100;
65
+
window_width($newWidth); // Call the function with the value stored in $newWidth
66
+
```
67
+
68
+
Functions may return values using the keyword `return`.
69
+
Values to return can be literals, stored in variables, or the result of an instruction.
70
+
71
+
```php
72
+
function window_height()
73
+
{
74
+
$heightOffset = 10;
75
+
76
+
return 100 + $heightOffset;
77
+
}
78
+
```
79
+
80
+
Functions inside classes and their instances are called methods.
81
+
To call a method, the name is preceeded by the instance and `->`.
82
+
A class instance is created by the `new` operation.
83
+
Methods have access to the special variable `$this`, which refers to the current instance.
84
+
85
+
```php
86
+
<?php
87
+
// Calculator.php
88
+
89
+
class Calculator {
90
+
// ...
91
+
92
+
public function sub($x, $y)
93
+
{
94
+
return $this->add($x, -$y); // Calls the method add() of the current instance
95
+
}
96
+
97
+
public function add($x, $y)
98
+
{
99
+
return $x + $y;
100
+
}
101
+
}
102
+
103
+
$calculator = new Calculator(); // Creates a new instance of Calculator class
104
+
$calculator->sub(3, 1); // Calls the method sub() of the instance stored in $calculator
105
+
// => 2
106
+
```
107
+
108
+
We will dig deeper into functions and classes in later exercises.
109
+
110
+
## Simple debugging
111
+
112
+
As a basic form of debugging, printing values and contents of variables helps with understanding what happens.
113
+
PHP can print values using:
114
+
115
+
-`echo $variable;` to output a string representation of the value
116
+
-`print $variable;`, which is the same as `echo $variable;`
117
+
-`var_dump($variable);` to get more insight into the value, e.g. the type PHP sees
118
+
119
+
When using `echo` or `print`: Empty strings, `false` and `null` will not show up.
120
+
And classes and instances will make them error out.
121
+
Use `var_dump()` to see these.
122
+
123
+
In the online editor, the output you produce is shown with the test results.
124
+
When you are using the CLI, the output appears mixed with the progress indicator before test summary.
125
+
22
126
## Code organization
23
127
24
-
PHP code files are text files that start with `<?php`.
25
-
If the file contains only php code, there is no closing `?>` tag.
26
-
Code, functions, and classes may all be present in a single file, but usually classes are in their own file with a filename which matches the class.
128
+
Files are basic units of code organisation in PHP and many other languages.
129
+
For showing the name of the file to look at in a code block, we use a comment.
130
+
You never have to add such comments to your files:
27
131
28
132
```php
29
133
<?php
30
-
// index.php
134
+
// NameOfPhpFile.php
31
135
32
-
$sum = add(1, 2);
136
+
[...]
33
137
```
34
138
139
+
Code, functions, and classes may all be present in a single file.
140
+
But usually these kinds of code units are in separate files.
141
+
142
+
It is common practice to have one class per file with a filename which matches the class.
143
+
35
144
```php
36
145
<?php
37
146
// Calculator.php
@@ -46,23 +155,56 @@ class Calculator {
46
155
}
47
156
```
48
157
158
+
Functions usually are grouped together in files describing their common theme.
159
+
160
+
```php
161
+
<?php
162
+
// window_handling.php
163
+
164
+
function window_paint()
165
+
{
166
+
// ...
167
+
}
168
+
169
+
function window_size($width, $height)
170
+
{
171
+
// ...
172
+
}
173
+
174
+
function window_move($x, $y)
175
+
{
176
+
// ...
177
+
}
178
+
```
179
+
180
+
And often there are files containing statements for direct execution, like an `index.php` that is commonly used as the entrypoint of a web application.
181
+
182
+
```php
183
+
<?php
184
+
// index.php
185
+
186
+
$sum = add(1, 2);
187
+
// ...
188
+
```
189
+
49
190
## Naming conventions
50
191
51
192
Classnames should all be `PascalCase`.
52
-
Depending on the style standard; variables, functions, method names may be either `camelCase` or `snake_case`.
193
+
Depending on the style standard; variables, functions, and method names may be either `camelCase` or `snake_case`.
53
194
Names may contain letters `a-zA-Z`, numbers `0-9`, and underscores `_` but they cannot start with a number.
54
195
55
196
## Comments
56
197
57
-
Singleline comments start with `//`, and a block of text can be wrapped with `/*` and `*/` to become a comment.
198
+
Single-line comments start with `//`, and a block of text can be wrapped with `/*` and `*/` to become a multi-line comment.
0 commit comments