· 7 years ago · Jan 16, 2019, 09:54 PM
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
89Functions
90 var_dump(VARIABLE)
91 returned string of variable type and value.
92 Example:
93 $x = 10.365;
94 var_dump($x);
95
96 [Outputs: "float(10.365)"]
97
98 Example 2:
99 $cars = array("Volvo, "BMW", "Toyota");
100 var_dump($cars);
101
102 [Outputs: "array(3) { [0]=> string(5) "Volvo" [1]=> string(3) "BMW" [2]=> string(6) "Toyota" }"]
103
104 Null
105 For Null variables or nonexistent variables var_dump returns "NULL"
106
107 strlen(STRING)
108 returns length of string.
109
110 str_word_count(STRING)
111 returns number of text strings delimited by white space.
112
113 strrev(STRING)
114 returns word with character order reversed
115
116 strpos(STRING1, STRING2)
117 returns the character position of STRING2 inside STRING1.
118 This is the number of characters before the start of the string.
119 Example:
120 echo strpos("Hello world!", "world"); // outputs 6
121
122 [Outputs: 6, H-E-L-L-O- and 1 white space]
123
124 str_replace(STRING1, STRING2, STRING3)
125 returns STRING3 where all instances of STRING1 are replaced with STRING2.
126 Example:
127 echo str_replace("world", "Dolly", "Hello world!");
128
129 [Outputs: "Hello Dolly!""]