-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path06_variable_scope.php
More file actions
56 lines (39 loc) · 1.12 KB
/
06_variable_scope.php
File metadata and controls
56 lines (39 loc) · 1.12 KB
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
<html>
<head>
<title>Hello</title>
</head>
<body>
<h1>Variables Scopes in PHP !!!</h1>
<?php
/*
PHP has three different variable scopes:
Local
- A variable declared within a function
Global
- A variable declared outside a function
Static
- When a function is executed, all of its variables are deleted.
If you want a local variable NOT to be deleted, use the static keyword
*/
$name = "Shiham Samsudeen"; // This is a global variable defined outsite a function
function sayHello(){
global $name; // if we do not call this it will throw an exception saying undefined variable $name
echo "Hello " . $name;
}
sayHello();
function sayHi(){
$anotherName = "Micheal Lue"; // Local variable declared within a function
echo "Hi, ". $anotherName;
}
sayHi();
function incrementA(){
static $a=0; // static functionality
echo $a;
$a++;
}
for($i = 0; $i < 10; $i++){
incrementA();
}
?>
</body>
</html>