-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-variables & datatypes.php
More file actions
134 lines (132 loc) · 4.53 KB
/
2-variables & datatypes.php
File metadata and controls
134 lines (132 loc) · 4.53 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP - Tutorial</title>
</head>
<body>
<?php
/*
-> Variables are "containers" for storing information.
- Creating (Declaring) PHP Variables
- In PHP, a variable starts with the $ sign
- Variable names are case-sensitive ($age and $AGE are two different variables)
- A variable name cannot start with a number
*/
$name = "Umer Farooq Jillani"; // $name is an string
$name_2 = 'Abu Bakar Jillani'; // $name_2 is an string
$Age = 20; // $Age is an integer
$x = 5; // $x is an integer
$y = 4; // $y is an integer
$a = $b = $c = "Fruit"; //Assign the multiple values
echo $x + $y;
echo "<br>";
echo "a = $a, b = $b, c = $c";
echo "<br>";
echo "I Love $name.<br>";
echo "My name is $name_2.<br>";
echo "My age is $Age.<br>";
// --------------------------------------------------------------------------------------------
/*
-> PHP supports the following data types:
- String
- Integer
- Float (floating point numbers - also called double)
- Boolean
- Array
- Object
- NULL
- Resource
*/
$cars = array("Volvo","BMW","Toyota");
echo "$cars[0]";
echo "<br>";
var_dump($cars);
echo "<br>";
var_dump($x); //Get the Type
echo "<br>";
var_dump($name);
echo "<br>";
var_dump(5);
echo "<br>";
var_dump("John");
echo "<br>";
var_dump(3.14); // Float
echo "<br>";
var_dump(true); // Boolean
echo "<br>";
var_dump([2, 3, 56]); //Array
echo "<br>";
var_dump(NULL); //NULL
echo "<br>";
// --------------------------------------------------------------------------------------------
/*
-> PHP Variables Scope
The scope of a variable is the part of the script where the variable can be referenced/used.
PHP has three different variable scopes:
- global
- local
- static
*/
// --------------------------------------------------------------------------------------------
// Global Scope
// A variable declared outside a function has a GLOBAL SCOPE and can only be
// accessed outside a function
$x = 5; // global scope
function myTest() {
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
myTest(); //fnc call
echo "<p>Variable x outside function is: $x</p>";
// --------------------------------------------------------------------------------------------
// Local Scope
// A variable declared within a function has a LOCAL SCOPE and can only be accessed
// within that function
function myTest_2() {
$w = 6; // local scope
echo "<p>Variable x inside function is: $w</p>";
}
myTest_2();
// using x outside the function will generate an error
echo "<p>Variable x outside function is: $w</p>";
// --------------------------------------------------------------------------------------------
// PHP The global Keyword
// The global keyword is used to access a global variable from within a function.
$A = 50;
$B = 100;
function myTest_3() {
global $A, $B;
$B = $A + $B;
}
myTest_3();
echo $B; // outputs 15
// --------------------------------------------------------------------------------------------
// PHP also stores all global variables in an array called $GLOBALS[index].
// The index holds the name of the variable. This array is also accessible from
// within functions and can be used to update global variables directly.
$x = 5;
$y = 10;
function myTest_4() {
$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
}
myTest_4();
echo "<br>$y"; // outputs 15
// --------------------------------------------------------------------------------------------
// static
// Normally, when a function is completed/executed, all of its variables are deleted.
// However, sometimes we want a local variable NOT to be deleted. We need it for a further job.
// Then, each time the function is called, that variable will still have the information
// it contained from the last time the function was called.
function myTest_5() {
static $x = 0;
echo "<p>$x</p>";
$x++;
}
myTest_5();
myTest_5();
myTest_5();
?>
</body>
</html>