Skip to content

Commit 0f2d493

Browse files
committed
Enhance PHP scripts with improved cookie handling, session management, and global variable examples; update HTML titles and add new global variables documentation.
1 parent ef4ed99 commit 0f2d493

File tree

13 files changed

+192
-48
lines changed

13 files changed

+192
-48
lines changed

1_Basics/1_basics.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77
$d = $a * $b;
88
echo "Addition of a and b is: $c<br>";
99
echo "Multiplication of a and b is: $d<br>";
10+
11+
$globalVar = "I am a global variable";
12+
13+
function accessGlobalVar() {
14+
global $globalVar; // GLOBAL also works
15+
echo "Accessing global variable inside function: $globalVar<br>";
16+
}
17+
18+
accessGlobalVar();
1019
?>
1120

1221
<!-- Variable naming conventions in PHP -->

1_Basics/3_arrays.php

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,47 +57,64 @@
5757
}
5858
echo "<br>";
5959
$res = array(
60-
array("Manoj", 7.8, "Pass"),
61-
array("Aditya", 3.4, "Fail"),
62-
array("Rahul", 8.9, "Pass"),
63-
array("Rohit", 9.2, "Pass")
64-
);
65-
66-
for($row = 0; $row < 4; $row++){
60+
array("Manoj", 7.8, "Pass"),
61+
array("Aditya", 3.4, "Fail"),
62+
array("Rahul", 8.9, "Pass"),
63+
array("Rohit", 9.2, "Pass")
64+
);
65+
66+
for ($row = 0; $row < 4; $row++) {
6767
echo "<p><b>Row number $row</b></p>";
6868
echo "<ul>";
69-
foreach($res[$row] as $ress){
69+
foreach ($res[$row] as $ress) {
7070
echo "<li>" . $ress . "</li>";
7171
}
7272
echo "</ul>";
7373
}
7474
echo "<br>";
75-
$emp = array((101)=>"Rahul", (102)=>"Rohit", (103)=>"Aditya", (104)=>"Manoj");
75+
$emp = array((101) => "Rahul", (102) => "Rohit", (103) => "Aditya", (104) => "Manoj");
7676
echo "<pre>";
7777
print_r($emp); // print_r() is used to print the human-readable information about a variable
7878
echo "</pre>";
7979
echo "<br>2D traversal using foreach loop";
80-
$books = array("C++"=>array("name" => "Beginning with C++", "author" => "E.Balagurusamy", "price" => 450),
81-
"Java"=>array("name" => "Java Programming", "author" => "Herbert Schildt", "price" => 500),
82-
"Python"=>array("name" => "Python Programming", "author" => "Guido van Rossum", "price" => 600)
83-
);
80+
$books = array(
81+
"C++" => array("name" => "Beginning with C++", "author" => "E.Balagurusamy", "price" => 450),
82+
"Java" => array("name" => "Java Programming", "author" => "Herbert Schildt", "price" => 500),
83+
"Python" => array("name" => "Python Programming", "author" => "Guido van Rossum", "price" => 600)
84+
);
8485
$keys = array_keys($books);
85-
for($i = 0;$i<count($books);$i++){
86+
for ($i = 0; $i < count($books); $i++) {
8687
echo "<h4>" . $keys[$i] . "</h4>";
87-
foreach($books[$keys[$i]] as $key => $value){
88+
foreach ($books[$keys[$i]] as $key => $value) {
8889
echo $key . " : " . $value . "<br>";
8990
}
9091
// echo "<br>";
9192
}
92-
echo "<br>2D traversal using for loop(ERROR needs to be fixed)";
93-
for($i=0; $i <count($books); $i++){
94-
for($j=0; $j<count($books[$keys[$i]]); $j++){
95-
echo $books[$keys[$i]][$keys[$j]] . "<br>";
93+
echo "<br>2D traversal using for loop<br>";
94+
$data = [
95+
"first" => ["name" => "John", "age" => 25, "city" => "New York"],
96+
"second" => ["name" => "Alice", "age" => 30, "city" => "Los Angeles"],
97+
"third" => ["name" => "Bob", "age" => 35, "city" => "Chicago"]
98+
];
99+
100+
$outerKeys = array_keys($data);
101+
102+
for ($i = 0; $i < count($data); $i++) {
103+
$outerKey = $outerKeys[$i];
104+
echo "Key: " . $outerKey . "<br>";
105+
$innerKeys = array_keys($data[$outerKey]);
106+
107+
for ($j = 0; $j < count($data[$outerKey]); $j++) {
108+
$innerKey = $innerKeys[$j];
109+
$value = $data[$outerKey][$innerKey];
110+
echo $innerKey . ": " . $value . "<br>";
96111
}
112+
echo "<hr>";
97113
}
98114

115+
var_dump($books); // var_dump() is used to display structured information (type and value) about one or more variables
99116

100-
?>
117+
?>
101118
</body>
102119

103120
</html>

1_Basics/4_functions.php

Lines changed: 99 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@
2626
array_pop($users);
2727
print_r($users);
2828
echo "<br>";
29+
// To push elements to last
30+
array_push($users, "Raj", "Yash");
31+
print_r($users);
32+
echo "<br>";
33+
unset($users);
34+
print_r($users ?? "not exists");
35+
echo "<br>";
2936

3037
$emp = ["name" => "abc", "age" => 25, "salary" => 25000];
3138
print_r(array_keys($emp));
@@ -36,9 +43,11 @@
3643
$colorsString = implode(", ", $colors);
3744
echo $colorsString;
3845
echo "<br>";
46+
echo gettype($colorsString);
47+
echo "<br>";
3948

4049
// Using explode function to convert string to array
41-
$colorsString = "red, green, blue";
50+
$colorsString = "red, green, blue, orange";
4251
$colorsArray = explode(", ", $colorsString);
4352
print_r($colorsArray);
4453
echo "<br>";
@@ -48,8 +57,8 @@
4857
print_r($uniqueNumbers);
4958
echo "<br>";
5059
// Merging two arrays
51-
$array1 = [1, 2, 3];
52-
$array2 = [4, 5, 6];
60+
$array1 = ["0" => 1, 2, 3, 4];
61+
$array2 = [4, 5, "b" => 6, 7];
5362
$mergedArray = array_merge($array1, $array2);
5463
print_r($mergedArray);
5564
echo "<br>";
@@ -69,11 +78,96 @@
6978
print_r($slicedArray);
7079
echo "<br>";
7180
// Finding the difference between two arrays
72-
$array1 = ["apple", "banana", "orange"];
81+
$array1 = ["apple", "banana", "orange", "pineapple"];
7382
$array2 = ["banana", "orange", "grapefruit"];
74-
$difference = array_diff($array1, $array2); //returns entry which are present in array 1 but not array 2
83+
$array3 = ["tomato", "orange", "grapefruit"];
84+
$difference = array_diff($array1, $array2, $array3); //returns entry which are present in array 1 but not array 2
7585
print_r($difference);
7686
echo "<br>";
87+
// reverse an array
88+
$numbers = [1, 2, 3, 4, 5];
89+
$reversedArray = array_reverse($numbers, true);
90+
print_r($reversedArray);
91+
echo "<br>";
92+
$a = array("a" => "Volvo", "b" => "BMW", "c" => "Toyota");
93+
print_r(array_reverse($a));
94+
echo "<br>Sort functions<br>";
95+
// Sorting an array in ascending order
96+
$numbers = [4, 2, 5, 1, 3];
97+
sort($numbers);
98+
print_r($numbers);
99+
echo "<br>";
100+
// Sorting an array in descending order
101+
$numbers = [4, 2, 5, 1, 3];
102+
rsort($numbers);
103+
print_r($numbers);
104+
echo "<br>";
105+
// Sorting an associative array by values
106+
$salaries = ["John" => 40000, "Jane" => 50000, "Jim" => 30000];
107+
asort($salaries);
108+
print_r($salaries);
109+
echo "<br>";
110+
// Sorting an associative array by keys
111+
$salaries = ["John" => 40000, "Jane" => 50000, "Jim" => 30000];
112+
ksort($salaries);
113+
print_r($salaries);
114+
echo "<br>";
115+
// Sorting an associative array by values in descending order
116+
$salaries = ["John" => 40000, "Jane" => 50000, "Jim" => 30000];
117+
arsort($salaries);
118+
print_r($salaries);
119+
echo "<br>";
120+
// Sorting an associative array by keys in descending order
121+
$salaries = ["John" => 40000, "Jane" => 50000, "Jim" => 30000];
122+
krsort($salaries);
123+
print_r($salaries);
124+
echo "<br>";
125+
// Sorting a multidimensional array
126+
$users = [
127+
["name" => "John", "age" => 30],
128+
["name" => "Jane", "age" => 25],
129+
["name" => "Jim", "age" => 35]
130+
];
131+
array_multisort(array_column($users, "age"), SORT_ASC, $users);
132+
print_r($users);
133+
echo "<br>";
134+
$result = array(
135+
array('name' => 'Manoj', 'cgpa' => 6.7, 'status' => 'pass'),
136+
array('name' => "Shalini", 'cgpa' => 9.8, 'status' => 'pass'),
137+
array('name' => 'Mani', 'cgpa' => 3.2, 'status' => 'fail')
138+
);
139+
$names = array_column($result, 'status', 'name');
140+
print_r($names);
141+
echo "<br>";
142+
echo "<br>Searching functions<br>";
143+
// Searching for a value in an array
144+
$numbers = [1, 2, 3, 4, 5];
145+
$key = array_search(3, $numbers);
146+
echo $key;
147+
echo "<br>";
148+
// Checking if a value exists in an array
149+
$numbers = [1, 2, 3, 4, 5];
150+
$exists = in_array(3, $numbers);
151+
echo $exists ? "Yes" : "No";
152+
echo "<br>";
153+
// Checking if a key exists in an array
154+
$emp = ["name" => "John", "age" => 30];
155+
$exists = array_key_exists("age", $emp);
156+
echo $exists ? "Yes" : "No";
157+
echo "<br>";
158+
// Counting the number of elements in an array
159+
$numbers = [1, 2, 3, 4, 5];
160+
$count = count($numbers);
161+
echo $count;
162+
echo "<br>";
163+
// Finding the maximum value in an array
164+
$max = max($numbers);
165+
echo $max;
166+
echo "<br>";
167+
// Finding the minimum value in an array
168+
$min = min($numbers);
169+
echo $min;
170+
echo "<br>";
77171
?>
78172
</body>
79173

2_Intermediate/10_checkcookie.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@
55
} else {
66
echo "Cookies are disabled.";
77
}
8+
echo "<br>";
9+
print_r($_COOKIE);

2_Intermediate/1_getMethod.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@
4141
<input type="submit" value="Submit">
4242
</form>
4343
<?php
44-
if (isset($_GET['name'])) {
44+
if (isset($_GET['name']) && isset($_GET['age']) && !empty($_GET['name']) && !empty($_GET['age'])) {
4545
echo "Hello, " . $_GET['name'];
46-
} else if (isset($_GET['age'])) {
46+
echo "<br>";
4747
echo "Your age is " . $_GET['age'];
48+
} else {
49+
echo "Fill the name field";
4850
}
4951
?>
5052
</body>

2_Intermediate/2_postMethod.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@
4444
<?php
4545
if (isset($_POST['name'])) {
4646
echo "Hello, " . $_POST['name'];
47-
} else if (isset($_POST['age'])) {
47+
echo "<br>";
4848
echo "Your age is " . $_POST['age'];
49+
} else {
50+
echo "Fill the name field";
4951
}
5052
?>
5153
</body>

2_Intermediate/4.1_globals.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
echo $_SERVER['HTTP_USER_AGENT']; // Shows browser information
3+
echo "<br>";
4+
echo $_SERVER['REQUEST_METHOD']; // Shows the request method (GET, POST, etc.)
5+
echo "<br>";
6+
echo $_SERVER['PHP_SELF'];
7+
echo "<br>";
8+
echo $_SERVER['SERVER_NAME'];
9+
echo "<br>";
10+
echo $_SERVER['REMOTE_PORT'];
11+
?>

2_Intermediate/4_GlobalVariables.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Here are the key PHP superglobals:
2929
```php
3030
echo $_SERVER['HTTP_USER_AGENT']; // Shows browser information
3131
echo $_SERVER['REQUEST_METHOD']; // Shows the request method (GET, POST, etc.)
32+
echo $_SERVER['PHP_SELF'] // Shows filename of the currently executing script
3233
```
3334

3435

2_Intermediate/7_cookiesForm.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
echo "Cookie '" . $cookie . "' is set!<br>";
3232
}
3333

34-
if(isset($_POST['del'])){
34+
if(isset($_POST['del']) && !empty($_POST['del'])){
3535
$cookie1 = $_POST['user'];
3636
setcookie($cookie1, "", time() - 3600, "/");
3737
echo "Cookie '" . $cookie1 . "' is deleted!<br>";

2_Intermediate/filters/filter.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@
1818
echo "Validate an integer:<br>";
1919
$int = 100;
2020

21-
if (!filter_var($int, FILTER_VALIDATE_INT) === false) {
22-
echo ("Integer is valid");
21+
if (filter_var($int, FILTER_VALIDATE_INT)) {
22+
echo "Integer is valid";
2323
} else {
24-
echo ("Integer is not valid");
24+
echo "Integer is not valid";
2525
}
2626

2727
echo "<br>";
2828
echo "filter_var() and Problem With 0<br>";
2929
$int = 0;
3030

3131
if (filter_var($int, FILTER_VALIDATE_INT) === 0 || !filter_var($int, FILTER_VALIDATE_INT) === false) {
32-
echo ("Integer is valid");
32+
echo "Integer is valid";
3333
} else {
34-
echo ("Integer is not valid");
34+
echo "Integer is not valid";
3535
}
3636

3737
echo "<br>";
@@ -40,9 +40,9 @@
4040
$ip = "127.0.0.1";
4141

4242
if (!filter_var($ip, FILTER_VALIDATE_IP) === false) {
43-
echo ("$ip is a valid IP address");
43+
echo "$ip is a valid IP address";
4444
} else {
45-
echo ("$ip is not a valid IP address");
45+
echo "$ip is not a valid IP address";
4646
}
4747
echo "<br>";
4848
echo "Validate an Email Address<br>";
@@ -54,9 +54,9 @@
5454

5555
// Validate e-mail
5656
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
57-
echo ("$email is a valid email address");
57+
echo "$email is a valid email address";
5858
} else {
59-
echo ("$email is not a valid email address");
59+
echo "$email is not a valid email address";
6060
}
6161

6262
echo "<br>";
@@ -69,9 +69,9 @@
6969

7070
// Validate url
7171
if (!filter_var($url, FILTER_VALIDATE_URL) === false) {
72-
echo ("$url is a valid URL");
72+
echo "$url is a valid URL";
7373
} else {
74-
echo ("$url is not a valid URL");
74+
echo "$url is not a valid URL";
7575
}
7676

7777
echo "<br>";

0 commit comments

Comments
 (0)