· 7 years ago · Jan 17, 2019, 07:02 AM
1Scope
2 Global
3 Declared in the HTML file under the PHP code block.
4 Terminated when page is closed.
5 global keyword
6 Not Accessable from within function without use of global keyword.
7 Example:
8
9 <?php
10 $x = 5;
11 $y = 4;
12 function myTest(){
13 global $x, $y;
14 $y = $x + $y;
15 }
16
17 myTest();
18 echo $y;
19 ?>
20
21 [Output is 9.]
22
23 Without global keyword the values $x = 5; and $y = 4; would not be accessed.
24
25 Alternative call for global variables
26 A table called $GLOBALS[] exists for all global variables.
27 The index for each variable is the variables written name.
28 Example:
29
30 function myTest() {
31 global $x, $y;
32 $x = $x + $y;
33 }
34
35 [This code is identical in function to the follow function.]
36
37 function myTest() {
38 $GLOBALS['x'] = $GLOBALS['x'] + $GLOBALS['y'];
39 }
40
41 Local
42 variables declared within functions are collected when the function terminates.
43 Static
44 Allows access to variable without instanciating the class for this variable.
45 Local variables can be scoped into global variables with the static keyword.
46 Example:
47 function myTest(){
48 static $x = 5;
49 echo $x;
50 $x++;
51 }
52
53 echo $x;
54
55 The echo will print the number 5 onto the page, but $x will not be collected and will have the value of 6 after the function is executed.
56 No Overwrite on Static Declartions
57 If the above function is ran again, the output will be 6.
58 Once a static variable is
59
60Object
61 Object or Class is created with the java moniker class
62 Example:
63 <?php
64 class car{
65 function Car(){
66 $this->model = "VW"
67 }
68 }
69
70 //create an object
71 $herbie = new Car();
72
73 //show object properties
74 echo $herbie->model;
75 ?>
76
77 [Outputs a list of variable values, but not the variable names]
78 [Outputs: "VW"]
79
80
81
82
83
84Arrays
85 Simple Array:
86 Example:
87 $cars = array("Volvo, "BMW", "Toyota");
88
89 Example 2:
90 $cars = array();
91 $cars[0] = "Volvo";
92 $cars[1] = "BMW";
93 $cars[2] = "Toyota";
94
95 Key Value pairs
96 Example:
97 $age = array("Peter"=>35, "Ben"=>37, "Joe"=>43);
98
99 Example 2:
100 $age = array();
101 $age["Peter"] = 35;
102 $age["Ben"] = 37;
103 $age["Joe"] = 43;
104
105 Array Functions
106 (Shortcut: r = decending instead of acending, k = sort based on key, a = sort based on value)
107 Simple Array Sorts:
108 sort() - sort arrays in ascending order
109 rsort() - sort arrays in descending order
110 Associate (Key-Value) Sorts:
111 asort() - sort associative arrays in ascending order, according to the value
112 ksort() - sort associative arrays in ascending order, according to the key
113 arsort() - sort associative arrays in descending order, according to the value
114 krsort() - sort associative arrays in descending order, according to the key
115
116 Array Operators
117 Union: $x + $y
118 Equality: $x == $y
119 Boolean: Checks if x and y have the same key/value pairs.
120 Identity: $x === $y
121 Boolean: Checks if x and y have the same key/value pairs in the same order
122 Inequality: $x != $y
123 Boolean: Reverse of Equality
124 Non-Identity: $x !== $y
125 Boolean: Reverse of Identity
126
127 Count function
128 count($array);
129 returns the number of elements in the array.
130Constants
131 created via define(STRING, VALUE) function.
132 Creates a global constant with the identifer STRING and the value of VALUE.
133 Example:
134 define("GREETING", "Welcome home!");
135
136 [Creates the global constant GREETING with the value "Welcome home!"]
137
138Function
139 User Defined Function
140 function fuctionName($x = 50, $y = 10){
141 return $x / $y;
142 }
143
144 [If you do not give this function an arguement for each parameter they will default to $x=50 and $y=10]
145 functionName();
146 [Returns 5]
147 functionName(10);
148 [Returns 1]
149 functionName(,25);
150 [Returns 2]
151 functionName(9,3);
152 [Returns 3]
153
154Default Functions
155 var_dump(VARIABLE)
156 returned string of variable type and value.
157 Example:
158 $x = 10.365;
159 var_dump($x);
160
161 [Outputs: "float(10.365)"]
162
163 Example 2:
164 $cars = array("Volvo, "BMW", "Toyota");
165 var_dump($cars);
166
167 [Outputs: "array(3) { [0]=> string(5) "Volvo" [1]=> string(3) "BMW" [2]=> string(6) "Toyota" }"]
168
169 Null
170 For Null variables or nonexistent variables var_dump returns "NULL"
171
172 empty(VARIABLE)
173 returns if variable is undefined (null).
174
175 strlen(STRING)
176 returns length of string.
177
178 str_word_count(STRING)
179 returns number of text strings delimited by white space.
180
181 strrev(STRING)
182 returns word with character order reversed
183
184 strpos(STRING1, STRING2)
185 returns the character position of STRING2 inside STRING1.
186 This is the number of characters before the start of the string.
187 Example:
188 echo strpos("Hello world!", "world"); // outputs 6
189
190 [Outputs: 6, H-E-L-L-O- and 1 white space]
191
192 str_replace(STRING1, STRING2, STRING3)
193 returns STRING3 where all instances of STRING1 are replaced with STRING2.
194 Example:
195 echo str_replace("world", "Dolly", "Hello world!");
196
197 [Outputs: "Hello Dolly!""]
198
199
200
201
202
203Operators
204 ** - Exponent
205 Example: $x**$y = x to the yth power.
206
207 comparasion operations (==)
208 $x === $y
209 if x has the same value as y and they are both the same type.
210 Different from == which examines only value but not type.
211
212 Logical Operators
213 and: And, &&
214 or: Or, ||
215 not: !
216 xor: Xor
217
218 Concatenate
219 simple concatentate done with period (.)
220 Append to existing string variable done with .=
221 Example:
222 $x = "Hi ";
223 $x .= "Mom";
224 echo $x;
225
226 [Outputs "Hi Mom"]
227
228 Loops
229 For, while, dowhile - All Java.
230 foreach
231 example
232 foreach ($array as $x){
233 echo $x;
234 }
235
236 [Outputs all values of the array]
237
238 foreach
239 example
240 foreach ($array as $x => $y){
241 echo $array . " " . $value;
242 }
243
244 [Outputs all key ($x) and value pairs ($y) in array]
245
246 Server Globals
247 Hold information about the server.
248 All self descriptive
249 List of Super Globals: https://www.w3schools.com/php/php_superglobals.asp
250
251 Forms
252 Form Action
253 The action directs the information passed in the form to a php file.
254 If that file is the same the page's php code blocks will rerun, however the page's elements will not be removed (only changed).
255 If it is an external file, the elements produced by that file will replace the <Form> code block.
256 POST
257 A quick summary of POST can be found here: https://imgur.com/a/GyOsrpa
258 GET
259 The URL of a GET request is modified.
260 a "?"" is appended to the page URL.
261 Following the "?" are key value pairs
262 For example on www.example.com/page with the two passed pairs:
263 name => "John", age => 20
264 The URL will look like: "www.example.com/page?name=John&age=20"
265 GET Requests can also be sent through direct access of PHP files
266 Example: <a href="example.php?name=John&age=20"> Link Text </a>
267
268 Just like POST, get retrieves it's key value pairs from the $_GET[] array.
269
270 Input Elements
271 type attribute determines the visual representation and interface of the element.
272 Textbox: type = "text"
273 Radio Buttons: type = "radio"
274 Radio buttons which have the same name are unchecked if any other radio button with the same name is checked.
275 Example:
276 <input type="radio" name="gender" value="female">Female
277 <input type="radio" name="gender" value="male">Male
278 <input type="radio" name="gender" value="other">Other
279
280 [Checking any one of these will uncheck the other two]
281
282 Security in forms
283 <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
284 will reject javascript injections and should be used instead of <?php echo $_SERVER["PHP_SELF"]); ?>
285
286 Protecting from injected valuesL
287 Example:
288
289 <?php
290 // define variables and set to empty values
291 $name = $email = $gender = $comment = $website = "";
292
293 if ($_SERVER["REQUEST_METHOD"] == "POST") {
294 $name = test_input($_POST["name"]);
295 $email = test_input($_POST["email"]);
296 $website = test_input($_POST["website"]);
297 $comment = test_input($_POST["comment"]);
298 $gender = test_input($_POST["gender"]);
299 }
300
301 function test_input($data) {
302 $data = trim($data);
303 $data = stripslashes($data);
304 $data = htmlspecialchars($data);
305 return $data;
306 }
307 ?>
308
309 [The text_input function will strip all data such that injections will become impossible.]