Skip to content

Commit 7d40f00

Browse files
committed
Renamed files in 1_Basic folder
1 parent 1d73efb commit 7d40f00

File tree

7 files changed

+347
-0
lines changed

7 files changed

+347
-0
lines changed

1_Basics/1_basics.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
echo "Hello World!";
3+
echo "<br>";
4+
$a = 5;
5+
$b = 10;
6+
$c = $a + $b;
7+
$d = $a * $b;
8+
echo "Addition of a and b is: $c<br>";
9+
echo "Multiplication of a and b is: $d<br>";
10+
?>
11+
12+
<!-- Variable naming conventions in PHP -->
13+
14+
<!DOCTYPE html>
15+
<html lang="en">
16+
17+
<head>
18+
<meta charset="UTF-8">
19+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
20+
<title>Basics</title>
21+
</head>
22+
23+
<body>
24+
<?php
25+
echo "HELLO<br>";
26+
const WISHES = "Good Day!<br>"; // method 1
27+
echo WISHES;
28+
echo constant("WISHES"); // method 2
29+
define("WISHESS", "Good Day!"); // method 3
30+
echo WISHESS;
31+
echo "<br>";
32+
$a = "Welcome to LPU, Punjab";
33+
echo gettype($a); // string
34+
echo "<br>";
35+
echo strlen($a); // length of string $a = 22
36+
echo "<br>";
37+
echo str_word_count($a); // word count of string $a = 4
38+
echo "<br>";
39+
$t = date("H"); // current hour
40+
if ($t < "20") {
41+
echo "Have a good day!";
42+
} else {
43+
echo "Have a good night!";
44+
}
45+
?>
46+
</body>
47+
48+
</html>

1_Basics/2_loops.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Loops</title>
8+
</head>
9+
10+
<body>
11+
<?php
12+
$i = 1;
13+
while($i <= 10) {
14+
echo "The number is: $i<br>";
15+
$i++;
16+
}
17+
18+
echo "<br>";
19+
$i = 1;
20+
do {
21+
echo "The number is: $i<br>";
22+
$i++;
23+
} while($i <= 10);
24+
25+
echo "<br>";
26+
for($i = 1; $i <= 10; $i++) {
27+
echo "The number is: $i<br>";
28+
}
29+
30+
echo "<br>";
31+
$colors = array("Red", "Green", "Blue", "Yellow");
32+
foreach($colors as $value) {
33+
echo "$value<br>";
34+
}
35+
echo "<br>";
36+
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
37+
foreach($age as $x => $val) {
38+
echo "$x = $val<br>";
39+
}
40+
echo "<br>";
41+
$day = "Monday";
42+
43+
switch ($day) {
44+
case "Monday":
45+
// continue;
46+
echo "Today is Monday";
47+
break;
48+
case "Tuesday":
49+
echo "Today is Tuesday";
50+
break;
51+
case "Wednesday":
52+
echo "Today is Wednesday";
53+
break;
54+
case "Thursday":
55+
echo "Today is Thursday";
56+
break;
57+
case "Friday":
58+
echo "Today is Friday";
59+
break;
60+
case "Saturday":
61+
echo "Today is Saturday";
62+
break;
63+
case "Sunday":
64+
echo "Today is Sunday";
65+
break;
66+
default:
67+
echo "Invalid day";
68+
}
69+
70+
?>
71+
</body>
72+
73+
</html>

1_Basics/3_arrays.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Document</title>
8+
</head>
9+
10+
<body>
11+
<?php
12+
$colors = array("Red", "Green", "Blue", "Yellow");
13+
foreach ($colors as $value) {
14+
echo "$value<br>";
15+
}
16+
echo "<br>";
17+
$age = array("Peter" => "35", "Ben" => "37", "Joe" => "43");
18+
foreach ($age as $x => $val) {
19+
echo "$x = $val<br>";
20+
}
21+
echo "<br>";
22+
$day = "Monday";
23+
$month = "January";
24+
$year = 2020;
25+
$date = array($day, $month, $year);
26+
foreach ($date as $value) {
27+
echo "$value<br>";
28+
}
29+
echo "<br>";
30+
$date = array("day" => $day, "month" => $month, "year" => $year);
31+
foreach ($date as $key => $value) {
32+
echo "$key = $value<br>";
33+
}
34+
echo "<br>Using for loop";
35+
$cars = array(
36+
array("Volvo", 22, 18),
37+
array("BMW", 15, 13),
38+
array("Saab", 5, 2),
39+
array("Land Rover", 17, 15)
40+
);
41+
for ($row = 0; $row < 4; $row++) {
42+
echo "<p><b>Row number $row</b></p>";
43+
echo "<ul>";
44+
for ($col = 0; $col < 3; $col++) {
45+
echo "<li>" . $cars[$row][$col] . "</li>";
46+
}
47+
echo "</ul>";
48+
}
49+
echo "<br>Using For each loop";
50+
foreach ($cars as $row) {
51+
echo "<p><b>Row number</b></p>";
52+
echo "<ul>";
53+
foreach ($row as $col) {
54+
echo "<li>" . $col . "</li>";
55+
}
56+
echo "</ul>";
57+
}
58+
echo "<br>";
59+
$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++){
67+
echo "<p><b>Row number $row</b></p>";
68+
echo "<ul>";
69+
foreach($res[$row] as $ress){
70+
echo "<li>" . $ress . "</li>";
71+
}
72+
echo "</ul>";
73+
}
74+
echo "<br>";
75+
$emp = array((101)=>"Rahul", (102)=>"Rohit", (103)=>"Aditya", (104)=>"Manoj");
76+
echo "<pre>";
77+
print_r($emp); // print_r() is used to print the human-readable information about a variable
78+
echo "</pre>";
79+
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+
);
84+
$keys = array_keys($books);
85+
for($i = 0;$i<count($books);$i++){
86+
echo "<h4>" . $keys[$i] . "</h4>";
87+
foreach($books[$keys[$i]] as $key => $value){
88+
echo $key . " : " . $value . "<br>";
89+
}
90+
// echo "<br>";
91+
}
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>";
96+
}
97+
}
98+
99+
100+
?>
101+
</body>
102+
103+
</html>

1_Basics/4_functions.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Document</title>
8+
</head>
9+
10+
<body>
11+
<?php function helloWorld()
12+
{
13+
echo "Hello, world!";
14+
}
15+
16+
helloWorld();
17+
echo "<br>";
18+
$users = ["Manoj", "Rahul", "Raj"];
19+
echo (is_array($users) ? "Yes, it is an array" : "No, it is not an array");
20+
echo "<br>";
21+
22+
unset($users[2]);
23+
print_r($users);
24+
echo "<br>";
25+
// To remove last element
26+
array_pop($users);
27+
print_r($users);
28+
echo "<br>";
29+
30+
$emp = ["name" => "abc", "age" => 25, "salary" => 25000];
31+
print_r(array_keys($emp));
32+
echo "<br>";
33+
34+
// Using implode function to cpnvert array to string
35+
$colors = ["red", "green", "blue"];
36+
$colorsString = implode(", ", $colors);
37+
echo $colorsString;
38+
echo "<br>";
39+
40+
// Using explode function to convert string to array
41+
$colorsString = "red, green, blue";
42+
$colorsArray = explode(", ", $colorsString);
43+
print_r($colorsArray);
44+
echo "<br>";
45+
// Removing duplicate values from an array
46+
$numbers = [1, 2, 3, 4, 4, 5, 6, 6];
47+
$uniqueNumbers = array_unique($numbers);
48+
print_r($uniqueNumbers);
49+
echo "<br>";
50+
// Merging two arrays
51+
$array1 = [1, 2, 3];
52+
$array2 = [4, 5, 6];
53+
$mergedArray = array_merge($array1, $array2);
54+
print_r($mergedArray);
55+
echo "<br>";
56+
// Combining two arrays into an associative array
57+
$keys = ["name", "age", "salary"];
58+
$values = ["John Doe", 30, 50000];
59+
$combinedArray = array_combine($keys, $values);
60+
print_r($combinedArray);
61+
echo "<br>";
62+
// Splitting an array into chunks
63+
$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
64+
$chunks = array_chunk($numbers, 3); //preserve key
65+
print_r($chunks);
66+
echo "<br>";
67+
$courses = array("PHP", "Laravel", "Node.js", "HTML", "CSS", "ASP.NET");
68+
$slicedArray = array_slice($courses, 2, 3, true);
69+
print_r($slicedArray);
70+
echo "<br>";
71+
// Finding the difference between two arrays
72+
$array1 = ["apple", "banana", "orange"];
73+
$array2 = ["banana", "orange", "grapefruit"];
74+
$difference = array_diff($array1, $array2); //returns entry which are present in array 1 but not array 2
75+
print_r($difference);
76+
echo "<br>";
77+
?>
78+
</body>
79+
80+
</html>

1_Basics/5_data_type.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
$x = "Hello world!";
3+
$y = 'Hello world!';
4+
$x = 5985;
5+
$z = 10.365;
6+
$a = true;
7+
$b=null;
8+
$cars = array("Volvo","BMW","Toyota");
9+
echo($a);
10+
echo "<br>";
11+
echo $x;
12+
echo "<br>";
13+
echo($y);
14+
echo "<br>";
15+
echo($z);
16+
echo "<br>";
17+
echo ($b);
18+
echo "<br>";
19+
echo gettype($cars);
20+
echo "<br>";
21+
var_dump($z == $y);
22+
?>

1_Basics/6_array.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
$a=array("rahul","mohan");
3+
echo gettype($a);
4+
echo "<br>";
5+
$cars = array("Volvo","BMW","Toyota");
6+
echo gettype($cars);
7+
echo "<br>";
8+
$b = ["INT219" => "Front End Web", "INT220" => "PHP", "INT221" => "Laravel"];
9+
$c = ["INT222" => "Node js", "CSE316" => "Operating System"];
10+
$d = ($b + $c); // Union of $a and $b
11+
print_r($d);
12+
echo "<br>";
13+
var_dump($d);
14+
?>

1_Basics/7_var1.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
$str = "raining";
3+
echo "It is $str!";
4+
echo "\n";
5+
echo 'It is $str!'; // this shows a different result; single inverted code don't show value of variable
6+
echo "\n";
7+
echo "It is ".$str."!";

0 commit comments

Comments
 (0)