├── .gitignore
├── images
└── oop1.png
├── module1
├── 10_conditionals.php
├── 11_if_else.php
├── 12_ternary_operator.php
├── 13_nested_ternary.php
├── 14_switch.php
├── 15_coding_challenge.js
├── 16_server.js
├── 1_agenda.js
├── 2_intro_php.js
├── 3_variables.php
├── 4_constants.php
├── 5_comments.php
├── 6_printing.php
├── 7_arithmetic_operation.php
├── 8_number_system.php
├── 9_printf.php
├── index.php
├── main.php
└── project
│ └── calculator
│ ├── index.php
│ └── styles.css
├── module2
├── 1_agenda.js
├── 2_loop.js
├── 3_forloop.js
├── 4_break_continue.js
├── 5_fibonacci.js
├── 6_function.js
├── 7_function_advance.js
├── 8_scope.js
└── 9_multiplication_table.js
├── module3
├── 1_array_basic.php
├── 2_associative_multidimensional_array.php
├── 3_array_function.php
├── 4_handle_string.php
├── main.php
├── project
│ └── tic_tac_toe.php
├── string.php
└── topic_module3.php
└── topics.js
/.gitignore:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/faisal2410/understand_php/980f5df580ae18928757b620348bf1ea0f895b76/.gitignore
--------------------------------------------------------------------------------
/images/oop1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/faisal2410/understand_php/980f5df580ae18928757b620348bf1ea0f895b76/images/oop1.png
--------------------------------------------------------------------------------
/module1/10_conditionals.php:
--------------------------------------------------------------------------------
1 |
5 |
6 |
14 |
15 |
16 |
17 |
18 |
28 |
29 |
57 |
58 |
68 |
69 |
78 |
79 |
83 |
84 |
93 |
94 |
95 |
97 |
98 |
106 |
110 |
111 |
112 |
113 |
114 |
115 |
117 |
118 |
119 |
128 |
129 |
130 |
132 |
133 |
141 |
142 |
145 |
146 |
154 |
155 |
156 |
158 |
159 |
171 |
172 |
173 |
175 |
176 |
186 |
187 |
188 |
190 |
191 |
199 |
200 |
202 |
203 |
211 |
212 |
213 |
214 |
216 |
217 |
230 |
231 |
232 |
234 |
235 |
243 |
244 |
245 |
247 |
248 |
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 |
266 |
267 |
268 |
269 |
270 |
271 |
--------------------------------------------------------------------------------
/module1/11_if_else.php:
--------------------------------------------------------------------------------
1 |
5 |
6 |
13 |
14 |
15 |
16 |
17 |
28 |
29 |
30 |
31 |
32 |
34 |
35 |
36 |
37 |
48 |
49 |
50 |
51 |
53 |
54 |
63 |
64 |
65 |
66 |
67 | = 13 && $age < 20) {
74 | echo "You are a teenager.";
75 | } else {
76 | echo "You are an adult.";
77 | }
78 |
79 | ?>
80 |
81 |
82 |
87 |
88 |
89 |
90 |
91 |
92 |
--------------------------------------------------------------------------------
/module1/12_ternary_operator.php:
--------------------------------------------------------------------------------
1 |
5 |
6 |
14 |
15 |
23 |
24 |
30 |
31 |
32 |
33 |
34 |
38 |
39 |
43 |
44 |
45 |
46 |
47 |
51 |
52 |
53 |
55 |
62 |
63 |
64 |
65 |
66 |
68 |
69 |
70 |
71 |
75 |
76 |
78 |
79 |
83 |
84 |
85 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
--------------------------------------------------------------------------------
/module1/13_nested_ternary.php:
--------------------------------------------------------------------------------
1 |
8 |
9 |
10 |
12 |
13 |
19 |
20 |
25 |
26 |
30 |
33 |
34 |
40 |
41 |
42 |
47 |
48 |
55 |
56 |
57 |
66 |
67 |
70 |
71 |
72 |
73 |
74 |
--------------------------------------------------------------------------------
/module1/14_switch.php:
--------------------------------------------------------------------------------
1 |
5 |
6 |
7 |
8 |
21 |
22 |
52 |
53 |
63 |
64 |
65 |
66 |
70 |
71 |
80 |
81 |
85 |
86 |
97 |
103 |
104 |
105 |
106 |
107 |
--------------------------------------------------------------------------------
/module1/15_coding_challenge.js:
--------------------------------------------------------------------------------
1 | // Here are 19 coding challenges based on the concepts of PHP you provided:
2 |
3 | // 1. Variables
4 |
5 | // Variable Declaration and Initialization: Create two variables - one storing your name (string) and the other storing your age (int). Print them.
6 | // Global vs. Local: Declare a global variable and a function that tries to print this global variable without using the global keyword. Then try with the global keyword.
7 | // 2. Constants
8 |
9 | // Constant Creation: Define a constant that stores the value of Pi (3.14159) and print it.
10 | // Variables vs Constants: Try to change the value of the constant defined in the previous challenge. Observe the result.
11 | // 3. Comments
12 |
13 | // Commenting Code: Write a function with both single-line and multi-line comments explaining what the function does.
14 | // 4. Printing Output
15 |
16 | // Echo vs Print: Use both echo and print statements to display your favorite quote.
17 | // String Concatenation: Concatenate two strings - "Hello" and "World", and display the result.
18 | // Escape Characters: Print a string that includes a new line and a tab using escape characters.
19 | // 5. Arithmetic Operations
20 |
21 | // Basic Calculations: Accept two numbers from a user and display their sum, difference, product, and quotient.
22 | // Modulus Operator: Input a number and display if it's even or odd using the modulus operator.
23 | // 6. Number Systems
24 |
25 | // Number Conversion: Convert a decimal number 255 to binary, octal, and hexadecimal.
26 | // 7. Printf Function
27 |
28 | // Formatted Output: Use the printf() function to print a float number with 2 decimal points.
29 | // 8. Conditional Statements
30 |
31 | // Simple Condition: Ask the user for their age and print whether they are a minor or an adult using the if statement.
32 | // Logical Operators: Write a program that checks if a number is both even and greater than 10.
33 | // 9. If-Else Statements
34 |
35 | // Temperature Feedback: Input the current temperature and provide feedback. If it's below 20°C, say it's cold; between 20°C and 30°C, say it's pleasant; above 30°C, say it's hot.
36 | // 10. Ternary Operator
37 |
38 | // Simple Ternary: Convert the temperature feedback logic from challenge 15 into a single line using the ternary operator.
39 | // 11. Nested Ternary Operators
40 |
41 | // Advanced Ternary: Implement a simple calculator that takes 3 inputs - two numbers and an operation (+, -, *, /) and displays the result using nested ternary operators.
42 | // 12. Switch Case Statements
43 |
44 | // Days of the Week: Accept a number between 1 and 7 and print the corresponding day of the week using a switch-case.
45 | // Strict vs Loose Comparison: Using a switch case, compare a string '10' with an integer 10 using both loose and strict comparisons.
46 | // Default Case: Enhance the "Days of the Week" challenge to include a default case which handles numbers outside the range of 1-7.
47 | // These challenges should provide a comprehensive practice over the mentioned PHP concepts. Happy coding!
48 |
49 |
50 |
51 | // 13. Dynamic Variable Names
52 |
53 | // Challenge: Create a string variable with a value of 'age'. Then, using this string, dynamically create another variable named 'age' that stores the value 25. Print the value of this new variable.
54 | // 14. Constants with Arrays
55 |
56 | // Challenge: Define a constant that holds an array of your favorite colors. Display the third color in the list.
57 | // 15. Advanced String Formatting with Printf
58 |
59 | // Challenge: Use the printf() function to display a formatted string containing a person's first name, last name, and age, ensuring that the age is right-aligned and occupies 3 spaces.
60 | // 16. Nested If-Else for Grades
61 |
62 | // Challenge: Accept a student's score (0 to 100) and provide a grade:
63 | // A (>=90)
64 | // B (>=75 and <90)
65 | // C (>=50 and <75)
66 | // D (<50)
67 | // 17. Ternary for Login Feedback
68 |
69 | // Challenge: Simulate a login mechanism. If a user's username is 'admin' and password is 'password123', display 'Welcome Admin'. For incorrect details, use a ternary operator to display 'Invalid Password' if the username is correct, otherwise display 'Invalid Username'.
70 | // 18. Complex Nested Ternary
71 |
72 | // Challenge: Given three integers, use a nested ternary operator to determine and display the largest one.
73 | // 19. Enhanced Switch Case with Functions
74 |
75 | // Challenge: Create a calculator function that accepts two numbers and an operation (add, subtract, multiply, divide) as arguments. Use a switch-case inside the function to perform the operation and return the result. The function should handle invalid operations gracefully.
76 | // I hope these challenges further enhance your understanding and mastery of PHP.
77 |
78 |
79 |
80 | // Solutions :
81 |
82 | // 1. Variables
83 |
84 | // Variable Declaration and Initialization
85 | // $name = "John";
86 | // $age = 30;
87 | // echo "Name: $name, Age: $age\n";
88 |
89 | // // Global vs. Local
90 | // $globalVar = "I am a global variable";
91 |
92 | // function displayVar() {
93 | // // This won't work as we are not using the 'global' keyword
94 | // // echo $globalVar;
95 |
96 | // global $globalVar;
97 | // echo $globalVar;
98 | // }
99 |
100 | // displayVar();
101 |
102 | // // 2. Constants
103 |
104 | // // Constant Creation
105 | // define("PI", 3.14159);
106 | // echo PI . "\n";
107 |
108 | // // Variables vs Constants
109 | // // This will result in an error since constants can't be redefined
110 | // // PI = 3.14;
111 |
112 | // // 3. Comments
113 |
114 | // // Commenting Code
115 | // function add($a, $b) {
116 | // // This function adds two numbers
117 | // /*
118 | // Multi-line comment:
119 | // It takes two parameters:
120 | // a - First number
121 | // b - Second number
122 | // */
123 | // return $a + $b;
124 | // }
125 |
126 | // // 4. Printing Output
127 |
128 | // // Echo vs Print
129 | // echo "Life is what happens when you're busy making other plans.\n";
130 | // print "Life is what happens when you're busy making other plans.\n";
131 |
132 | // // String Concatenation
133 | // echo "Hello " . "World" . "\n";
134 |
135 | // // Escape Characters
136 | // echo "This is a new line.\n\tAnd this is a tab.\n";
137 |
138 | // // 5. Arithmetic Operations
139 |
140 | // // Basic Calculations
141 | // $number1 = 10; // Sample input
142 | // $number2 = 20; // Sample input
143 | // echo "Sum: " . ($number1 + $number2) . "\n";
144 | // echo "Difference: " . ($number1 - $number2) . "\n";
145 | // echo "Product: " . ($number1 * $number2) . "\n";
146 | // echo "Quotient: " . ($number1 / $number2) . "\n";
147 |
148 | // // Modulus Operator
149 | // $number = 5; // Sample input
150 | // echo ($number % 2 == 0) ? "Even" : "Odd";
151 |
152 | // // 6. Number Systems
153 |
154 | // // Number Conversion
155 | // $decimal = 255;
156 | // echo "Binary: " . decbin($decimal) . "\n";
157 | // echo "Octal: " . decoct($decimal) . "\n";
158 | // echo "Hexadecimal: " . dechex($decimal) . "\n";
159 |
160 | // // 7. Printf Function
161 |
162 | // // Formatted Output
163 | // $floatNumber = 10.456789; // Sample input
164 | // printf("%.2f\n", $floatNumber);
165 |
166 | // // 8. Conditional Statements
167 |
168 | // // Simple Condition
169 | // $userAge = 18; // Sample input
170 | // if ($userAge < 18) {
171 | // echo "You are a minor.\n";
172 | // } else {
173 | // echo "You are an adult.\n";
174 | // }
175 |
176 | // // Logical Operators
177 | // $number = 15; // Sample input
178 | // if ($number % 2 == 0 && $number > 10) {
179 | // echo "The number is even and greater than 10.\n";
180 | // } else {
181 | // echo "The number does not meet the criteria.\n";
182 | // }
183 |
184 | // // 9. If-Else Statements
185 |
186 | // // Temperature Feedback
187 | // $temperature = 25; // Sample input
188 | // if ($temperature < 20) {
189 | // echo "It's cold.\n";
190 | // } elseif ($temperature >= 20 && $temperature <= 30) {
191 | // echo "It's pleasant.\n";
192 | // } else {
193 | // echo "It's hot.\n";
194 | // }
195 |
196 | // // 10. Ternary Operator
197 |
198 | // // Simple Ternary
199 | // echo ($temperature < 20) ? "It's cold." : (($temperature <= 30) ? "It's pleasant." : "It's hot.");
200 |
201 |
202 |
203 |
204 |
205 | // 11. Nested Ternary Operators
206 |
207 | // $num1 = 10;
208 | // $num2 = 20;
209 | // $operation = '+'; // Sample operation
210 |
211 | // $result = ($operation == '+') ? $num1 + $num2 :
212 | // (($operation == '-') ? $num1 - $num2 :
213 | // (($operation == '*') ? $num1 * $num2 :
214 | // (($operation == '/') ? $num1 / $num2 : "Invalid Operation")));
215 |
216 | // echo "Result: $result\n";
217 |
218 | // // 12. Switch Case Statements
219 |
220 | // // Days of the Week
221 | // $dayNumber = 5; // Sample number
222 |
223 | // switch($dayNumber) {
224 | // case 1:
225 | // echo "Monday\n";
226 | // break;
227 | // case 2:
228 | // echo "Tuesday\n";
229 | // break;
230 | // case 3:
231 | // echo "Wednesday\n";
232 | // break;
233 | // case 4:
234 | // echo "Thursday\n";
235 | // break;
236 | // case 5:
237 | // echo "Friday\n";
238 | // break;
239 | // case 6:
240 | // echo "Saturday\n";
241 | // break;
242 | // case 7:
243 | // echo "Sunday\n";
244 | // break;
245 | // default:
246 | // echo "Invalid number\n";
247 | // }
248 |
249 | // // Strict vs Loose Comparison
250 | // $value = '10';
251 |
252 | // switch(true) {
253 | // case ($value == 10):
254 | // echo "Loose match\n";
255 | // break;
256 | // case ($value === 10):
257 | // echo "Strict match\n";
258 | // break;
259 | // default:
260 | // echo "No match\n";
261 | // }
262 |
263 | // // 13. Dynamic Variable Names
264 |
265 | // $stringVariable = 'age';
266 | // $$stringVariable = 25; // Creates a variable named $age
267 | // echo $age . "\n";
268 |
269 | // // 14. Constants with Arrays
270 |
271 | // define("COLORS", ["Red", "Blue", "Green", "Yellow"]);
272 | // echo COLORS[2] . "\n";
273 |
274 | // // 15. Advanced String Formatting with Printf
275 |
276 | // $firstName = "John";
277 | // $lastName = "Doe";
278 | // $age = 27;
279 | // printf("Name: %s %s, Age: %3d\n", $firstName, $lastName, $age);
280 |
281 | // // 16. Nested If-Else for Grades
282 |
283 | // $score = 85;
284 |
285 | // if ($score >= 90) {
286 | // echo "Grade: A\n";
287 | // } elseif ($score >= 75) {
288 | // echo "Grade: B\n";
289 | // } elseif ($score >= 50) {
290 | // echo "Grade: C\n";
291 | // } else {
292 | // echo "Grade: D\n";
293 | // }
294 |
295 | // // 17. Ternary for Login Feedback
296 |
297 | // $username = "admin";
298 | // $password = "password123";
299 |
300 | // $message = ($username == "admin") ?
301 | // (($password == "password123") ? "Welcome Admin" : "Invalid Password")
302 | // : "Invalid Username";
303 | // echo $message . "\n";
304 |
305 | // // 18. Complex Nested Ternary
306 |
307 | // $a = 5;
308 | // $b = 15;
309 | // $c = 10;
310 |
311 | // $largest = ($a > $b) ? (($a > $c) ? $a : $c) : (($b > $c) ? $b : $c);
312 | // echo "Largest: $largest\n";
313 |
314 | // // 19. Enhanced Switch Case with Functions
315 |
316 | // function calculator($num1, $num2, $operation) {
317 | // switch($operation) {
318 | // case "add":
319 | // return $num1 + $num2;
320 | // case "subtract":
321 | // return $num1 - $num2;
322 | // case "multiply":
323 | // return $num1 * $num2;
324 | // case "divide":
325 | // if($num2 == 0) {
326 | // return "Division by zero is not allowed.";
327 | // }
328 | // return $num1 / $num2;
329 | // default:
330 | // return "Invalid operation.";
331 | // }
332 | // }
333 |
334 | // echo calculator(10, 5, "add");
335 |
--------------------------------------------------------------------------------
/module1/16_server.js:
--------------------------------------------------------------------------------
1 | // Understanding $_SERVER
2 | // In PHP, $_SERVER is a superglobal variable, which means that it is available in all scopes throughout a script. It is an array that holds information about headers, paths, and script locations. The entries in this array are created by the web server and contain data related to the current script and the HTTP request.
3 |
4 | // Here's an example of using $_SERVER to get the user's IP address and the user agent string (which identifies the user's browser):
5 |
6 |
7 | // ';
9 | // echo 'User Agent: ' . $_SERVER['HTTP_USER_AGENT'] . ' ';
10 | // ?>
11 |
12 |
13 | // Understanding $_SERVER["REQUEST_METHOD"]
14 | // The $_SERVER["REQUEST_METHOD"] entry in the $_SERVER superglobal array allows you to determine the HTTP method used to access the page. Typically, this will be one of the methods like 'GET', 'POST', 'PUT', 'DELETE', etc.
15 |
16 | // In many cases, you will use this entry to check if a form has been submitted using the "POST" method, like this:
17 |
18 |
19 | //
25 |
26 |
27 | // Combining $_SERVER and $_SERVER["REQUEST_METHOD"] to Handle Form Submissions
28 | // Let's build upon the previous example to demonstrate how you might use $_SERVER["REQUEST_METHOD"] to handle form submissions in a real PHP script. Below, we have a simple HTML form and a PHP script that processes the form data if the request method is "POST":
29 |
30 |
31 | //
38 |
39 | //
40 | //
41 | //
42 | // Form Handling in PHP
43 | //
44 | //
45 |
46 | //
50 |
51 | //
52 | //
53 |
54 | // In the above script:
55 |
56 | // We use $_SERVER["REQUEST_METHOD"] to check if the form was submitted using the POST method.
57 | // If the form was submitted using the POST method, we use the $_POST superglobal to get the value of the name input field.
58 | // We then use htmlspecialchars() to sanitize the input and prevent XSS attacks before echoing a greeting to the user.
59 | // I hope this gives you a clear understanding! If you have any further questions or topics you'd like to explore, feel free to ask.
60 |
61 |
62 | // ✅Understanding $_POST
63 | // In PHP, $_POST is a superglobal array that is used to collect form data sent in an HTTP POST request. It allows you to access the data sent in the request body, which is often used to collect data from HTML forms.
64 |
65 | // Usage of $_POST
66 | // 1. Basic Usage
67 | // You can use $_POST to get the value of individual form fields using syntax like $_POST['fieldname']. Here's a basic example:
68 |
69 |
70 | //
76 | //
80 | // In this script:
81 |
82 | // The HTML form uses the POST method to send data to the server.
83 | // We use $_POST['name'] to get the value of the name input field when the form is submitted.
84 | // 2. Sanitizing Input
85 | // To protect your application from XSS (Cross-Site Scripting) attacks and other potential vulnerabilities, it's essential to sanitize user input before using it. You can use PHP's htmlspecialchars function to do this:
86 |
87 |
88 | //
94 | // 3. Checking if a Form Field is Set
95 | // Before you access a form field with $_POST, it's a good practice to check if the field is set using isset() to avoid PHP undefined index notices:
96 |
97 |
98 | //
108 | // 4. Working with Different Input Types
109 | // You can work with various input types, such as text, radio buttons, checkboxes, and others using $_POST. Here is an example with a radio button:
110 |
111 |
112 | //
118 | //
123 | // I hope this helps you understand how to use $_POST in PHP! If you have any further questions, feel free to ask.
124 |
125 |
126 |
127 |
128 |
129 |
130 | //
133 |
134 | // ';
136 | // echo 'User Agent: ' . $_SERVER['HTTP_USER_AGENT'] . ' ';
137 | // ?>
138 |
139 |
140 |
--------------------------------------------------------------------------------
/module1/1_agenda.js:
--------------------------------------------------------------------------------
1 | // PHP Basics
2 | //👉 1. PHP Environment Setup
3 | // Installing a local server (XAMPP)
4 | // Setting up a code editor (Visual Studio Code)
5 | // 👉2. Variables
6 | // Introduction to variables and data types (int, float, string, bool)
7 | // Variable naming conventions and rules
8 | // Variable scope (local, global)
9 | // 👉3. Constants
10 | // Defining constants using define()
11 | // Naming conventions for constants
12 | // Difference between variables and constants
13 | // 👉4. Comments
14 | // Single-line comments
15 | // Multi-line comments
16 | // Comments for code documentation
17 | // 👉5. Printing Output
18 | // Using echo and print statements
19 | // Concatenating strings and variables
20 | // Formatting output using escape characters
21 | // 👉6. Arithmetic Operations
22 | // Addition, subtraction, multiplication, division
23 | // Modulus (remainder) operation
24 | // Operator precedence and associativity
25 | // 👉7. Number Systems
26 | // Binary, octal, decimal, hexadecimal
27 | // Converting between number systems
28 | // 👉8. Printf Function
29 | // Using printf() for formatted output
30 | // Specifiers for different data types (%d, %f, %s, %c)
31 | // Formatting options (width, precision)
32 | // 👉9. Conditional Statements
33 | // Introduction to if statements
34 | // Using comparison operators (==, !=, <, >, <=, >=)
35 | // Building conditions using logical operators (&&, ||, !)
36 | // 👉10. If-Else Statements
37 | // Adding an else block to handle alternate conditions
38 | // Nesting if-else statements for complex conditions
39 | // 👉11. Ternary Operator
40 | // Syntax and usage of the ternary operator (condition ? true : false)
41 | // Replacing simple if-else statements with ternary
42 | // 👉12. Nested Ternary Operators
43 | // Using multiple levels of ternary operators
44 | // Considerations for readability and maintainability
45 | // 👉13. Switch Case Statements
46 | // Syntax and purpose of switch-case statements
47 | // Handling different cases and using the default case
48 | // Comparison using strict (===) vs. loose (==) equality
49 |
50 |
51 | // 👉Teaching Approach:
52 | // Start with a brief introduction to PHP and its importance in web development.
53 | // Progress through the topics in a logical order, building on each concept.
54 | // Provide plenty of code examples and practical exercises for hands-on practice.
55 | // Encourage students to experiment with code variations to solidify their understanding.
56 | // Offer coding challenges that require combining multiple concepts.
57 | // Highlight best practices and coding standards as you teach each topic.
58 | // Include real-world examples to show how these concepts are used in actual PHP projects.
59 | // Make use of interactive tools, quizzes, and projects to keep students engaged.
60 | // Remember that practice is crucial in coding, so allocate plenty of time for coding exercises and projects. By following this curriculum, your students will have a solid foundation in PHP programming and be well-prepared to tackle more advanced topics in the future.
61 |
62 |
63 |
64 |
65 |
66 |
--------------------------------------------------------------------------------
/module1/2_intro_php.js:
--------------------------------------------------------------------------------
1 | // 👉Introduction to PHP and Its Importance in Web Development
2 | // PHP (Hypertext Preprocessor) is a widely-used open-source scripting language specifically designed for web development. It's embedded within HTML code and executed on the server-side, generating dynamic web content that is then sent to the user's browser.
3 |
4 | //👉 Importance in Web Development:
5 |
6 | // Server-Side Scripting: PHP is primarily used for server-side scripting, which means it runs on the web server before sending the output to the client's browser. This enables the creation of dynamic and interactive web pages.
7 |
8 | // Dynamic Web Content: Unlike static HTML, PHP allows developers to create websites that respond to user interactions, database queries, and various conditions. This dynamic nature enhances user experience and interactivity.
9 |
10 | // Database Integration: PHP seamlessly integrates with databases like MySQL, allowing web applications to store, retrieve, and manipulate data. This is crucial for creating content-rich websites and web applications.
11 |
12 | // Efficiency: PHP is highly efficient for web development tasks due to its lightweight nature and optimized execution. It's designed to handle the demands of web traffic without excessive resource consumption.
13 |
14 | // Versatility: PHP can be used for various web-related tasks, including form handling, file manipulation, authentication, sessions, and more. It's well-suited for creating e-commerce sites, blogs, content management systems, and social networks.
15 |
16 | // Open Source and Active Community: Being open-source, PHP has a vast community of developers who contribute to its continuous improvement. This results in frequent updates, bug fixes, and new features, ensuring the language remains relevant and secure.
17 |
18 | // Widely Used: PHP is one of the most popular server-side scripting languages, and as a result, a large number of websites and web applications, including some of the most prominent ones, are built using PHP.
19 |
20 | // Integration with HTML: PHP code is embedded within HTML, making it easy to mix dynamic content with static elements. This seamless integration simplifies the development process.
21 |
22 | // In summary, PHP is a versatile and powerful scripting language that plays a pivotal role in web development. It empowers developers to create dynamic, interactive, and data-driven websites, enhancing user experiences and enabling the creation of complex web applications. As you delve into learning PHP, you'll unlock the ability to create websites that go beyond static content and engage users with dynamic features.
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/module1/3_variables.php:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
6 |
19 |
20 |
21 |
23 |
24 |
25 |
26 |
40 |
41 |
42 |
43 |
46 |
47 |
48 |
49 |
64 |
65 |
66 |
67 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
93 |
94 |
95 |
96 |
97 |
106 |
107 |
108 |
109 |
110 |
118 |
119 |
120 |
121 |
122 |
133 |
134 |
135 |
136 |
137 |
146 |
147 |
148 |
149 |
150 |
159 |
160 |
161 |
162 |
169 |
170 |
171 |
172 |
179 |
180 |
181 |
182 |
194 |
195 |
196 |
197 |
198 |
204 |
205 |
206 |
207 |
208 |
209 |
210 |
211 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
265 |
266 |
267 |
268 |
269 |
277 |
278 |
279 |
280 |
281 |
289 |
290 |
291 |
292 |
293 |
302 |
303 |
304 |
305 |
306 |
316 |
317 |
318 |
319 |
320 |
333 |
334 |
335 |
336 |
337 |
349 |
350 |
351 |
352 |
353 |
373 |
374 |
375 |
376 |
377 |
394 |
395 |
396 |
397 |
398 |
407 |
408 |
409 |
410 |
411 |
412 |
413 |
449 |
450 |
451 |
453 |
454 |
582 |
--------------------------------------------------------------------------------
/module1/4_constants.php:
--------------------------------------------------------------------------------
1 |
3 |
4 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
17 |
18 |
21 |
22 |
27 |
28 |
29 |
30 |
36 |
37 |
44 |
45 |
46 |
51 |
52 |
63 |
64 |
65 | function testFunction() {
66 | echo MY_CONST; // Works because constants are globally scoped
67 | // echo $myVar; // Might not work if $myVar is defined outside and not passed as an argument or globalized
68 | }
69 | ?> -->
70 |
71 |
72 |
81 |
82 |
83 |
89 |
90 |
91 |
92 |
93 |
94 |
--------------------------------------------------------------------------------
/module1/5_comments.php:
--------------------------------------------------------------------------------
1 |
8 |
9 |
17 |
18 |
19 |
34 |
35 |
81 |
--------------------------------------------------------------------------------
/module1/6_printing.php:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
8 |
9 |
14 |
15 |
16 |
19 |
20 |
23 |
24 |
25 |
27 |
28 |
35 |
36 |
37 |
38 |
43 |
44 |
55 |
56 |
63 |
64 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
77 |
78 |
80 |
81 |
85 |
86 |
87 |
88 |
95 |
96 |
97 |
98 |
102 |
103 |
104 |
105 |
106 |
111 |
112 |
114 |
115 |
121 |
122 |
123 |
124 |
128 |
129 |
130 |
131 |
132 |
139 |
140 |
141 |
142 |
143 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
--------------------------------------------------------------------------------
/module1/7_arithmetic_operation.php:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
13 |
14 |
15 |
17 |
18 |
19 |
23 |
24 |
25 |
27 |
28 |
33 |
34 |
35 |
37 |
38 |
39 |
43 |
44 |
45 |
47 |
48 |
49 |
53 |
54 |
55 |
59 |
60 |
61 |
68 |
69 | Associativity is often left-to-right for most arithmetic operators, meaning operators are evaluated from left to right. For example:
70 |
71 |
72 |
76 |
77 |
80 |
81 |
82 |
83 |
84 |
--------------------------------------------------------------------------------
/module1/8_number_system.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 |
9 |
10 |
17 |
18 | Example:
19 |
20 |
29 |
30 |
31 |
36 |
37 |
41 |
42 |
46 |
47 |
65 |
66 |
67 |
68 |
69 |
84 |
85 |
92 |
93 |
94 |
95 |
96 |
97 |
--------------------------------------------------------------------------------
/module1/9_printf.php:
--------------------------------------------------------------------------------
1 |
8 |
9 |
13 |
14 |
15 |
28 |
29 |
36 |
37 |
42 |
43 |
48 |
49 |
53 |
54 |
55 |
57 |
58 |
64 |
65 |
66 |
71 |
72 |
79 |
80 |
81 |
87 |
88 |
93 |
94 |
100 |
101 |
102 |
103 |
107 |
108 |
113 |
114 |
117 |
118 |
119 |
121 |
122 |
126 |
127 |
129 |
130 |
131 |
133 |
134 |
138 |
139 |
142 |
143 |
144 |
146 |
147 |
152 |
155 |
156 |
157 |
159 |
160 |
167 |
168 |
174 |
175 |
176 |
177 |
179 |
180 |
184 |
185 |
186 |
188 |
189 |
192 |
193 |
198 |
199 |
203 |
204 |
205 |
207 |
208 |
212 |
213 |
217 |
218 |
219 |
221 |
222 |
223 |
227 |
228 |
231 |
232 |
233 |
235 |
236 |
240 |
241 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 |
--------------------------------------------------------------------------------
/module1/index.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
39 |
40 |
41 |
42 |
43 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
69 |
70 |
71 |
72 |
77 |
78 |
79 |
80 |
87 |
88 |
89 |
90 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
110 |
111 |
112 |
113 |
116 |
117 |
118 |
125 |
126 |
127 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
145 |
146 |
147 |
148 |
152 |
153 |
154 |
155 |
156 |
161 |
162 |
163 |
164 |
165 |
169 |
170 |
171 |
173 |
174 |
175 |
179 |
180 |
181 |
182 |
183 |
190 |
191 |
195 |
196 |
197 |
210 |
211 |
218 |
219 |
220 |
221 |
222 |
223 |
231 |
232 |
233 |
234 |
235 |
245 |
246 |
274 |
275 |
285 |
286 |
295 |
296 |
300 |
301 |
310 |
311 |
312 |
314 |
315 |
323 |
324 |
325 |
326 |
327 |
334 |
335 |
336 |
337 |
338 |
368 |
369 |
370 |
371 |
372 |
373 |
374 |
--------------------------------------------------------------------------------
/module1/main.php:
--------------------------------------------------------------------------------
1 | = 18) {
177 | // echo "You are eligible to vote.";
178 | // }
179 |
180 |
181 |
182 |
183 |
184 |
185 | /*
186 | Comparison Operators:
187 |
188 | == : Equal to
189 | != : Not equal to
190 | < : Less than
191 | > : Greater than
192 | <= : Less than or equal to
193 | >= : Greater than or equal to
194 | Here are some ✅Examples using these operators:
195 |
196 |
197 | */
198 |
199 |
200 |
201 | // $number = 10;
202 |
203 | // if ($number == 10) {
204 | // echo "The number is 10.";
205 | // }
206 |
207 | // if ($number != 15) {
208 | // echo "The number is not 15.";
209 | // }
210 |
211 | // if ($number < 20) {
212 | // echo "The number is less than 20.";
213 | // }
214 |
215 | // if ($number > 5) {
216 | // echo "The number is greater than 5.";
217 | // }
218 |
219 | // if ($number <= 10) {
220 | // echo "The number is 10 or less.";
221 | // }
222 |
223 | // if ($number >= 9) {
224 | // echo "The number is 9 or more.";
225 | // }
226 |
227 |
228 | /*
229 | logical operators:
230 |
231 |
232 | && : AND
233 | || : OR
234 | ! : NOT
235 |
236 | */
237 |
238 | // ✅Example 15
239 |
240 |
241 | // $age = 20;
242 | // $hasID = true;
243 |
244 | // if ($age >= 18 && $hasID) {
245 | // echo "You can enter the club.";
246 | // }
247 |
248 |
249 |
250 | // ✅Example 16
251 |
252 | // $isWeekend = true;
253 | // $hasHoliday = false;
254 |
255 | // if ($isWeekend || $hasHoliday) {
256 | // echo "You don't have to go to work.";
257 | // }
258 |
259 |
260 |
261 |
262 | // ✅Example 17
263 |
264 | // $isRaining = false;
265 |
266 | // if (!$isRaining) {
267 | // echo "You don't need an umbrella.";
268 | // }
269 |
270 |
271 |
272 |
273 | // ✅Example 18
274 |
275 | // $age=19;
276 |
277 | // $status = ($age < 18) ? "minor" : "adult";
278 | // echo $status;
279 |
280 |
281 |
282 | // ✅Example 19
283 |
284 | // $day = 3; // Wednesday
285 |
286 | // switch ($day) {
287 | // case 1:
288 | // echo "Monday";
289 | // break;
290 | // case 2:
291 | // echo "Tuesday";
292 | // break;
293 | // case 3:
294 | // echo "Wednesday";
295 | // break;
296 | // case 4:
297 | // echo "Thursday";
298 | // break;
299 | // case 5:
300 | // echo "Friday";
301 | // break;
302 | // case 6:
303 | // echo "Saturday";
304 | // break;
305 | // case 7:
306 | // echo "Sunday";
307 | // break;
308 | // default:
309 | // echo "Invalid day number!";
310 | // }
311 |
312 |
313 |
314 | /*
315 | Formatting Output Using Escape Characters:
316 | Escape characters are used to represent certain special characters in strings. These characters are prefixed with a backslash \.
317 |
318 | Some commonly used escape characters in PHP are:
319 |
320 | \n: Newline character
321 | \t: Tab character
322 | \": Double quote
323 | \': Single quote
324 | \\: Backslash
325 | Here's an ✅Example demonstrating the use of these escape characters:
326 | */
327 |
328 | // ✅Example 20
329 |
330 | // echo "Hello\tWorld!\n I love bangladesh"; // Outputs: Hello World! (with a tab space and then moves to the next line)
331 |
332 | // echo "She said, \"Hello!\""; // Outputs: She said, "Hello!"
333 |
334 | // echo 'It\'s a beautiful day!'; // Outputs: It's a beautiful day!
335 |
336 |
337 |
338 |
339 |
340 |
341 |
342 |
343 |
344 |
345 |
346 |
347 |
348 |
349 |
--------------------------------------------------------------------------------
/module1/project/calculator/index.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Basic Calculator
8 |
11 |
12 |
13 |
14 |
Basic Calculator
15 |
29 |
30 |
69 |
70 |
71 |
72 |
--------------------------------------------------------------------------------
/module1/project/calculator/styles.css:
--------------------------------------------------------------------------------
1 | body {
2 | font-family: Arial, sans-serif;
3 | text-align: center;
4 | }
5 |
6 | .container {
7 | margin: 50px auto;
8 | width: 300px;
9 | border: 1px solid #ccc;
10 | padding: 20px;
11 | border-radius: 5px;
12 | box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
13 | }
14 |
15 | input[type="number"], select {
16 | width: 100%;
17 | padding: 8px;
18 | margin-bottom: 10px;
19 | border: 1px solid #ccc;
20 | border-radius: 3px;
21 | }
22 |
23 | button {
24 | background-color: #007BFF;
25 | color: white;
26 | border: none;
27 | padding: 10px 20px;
28 | border-radius: 3px;
29 | cursor: pointer;
30 | }
31 |
32 | #result {
33 | margin-top: 20px;
34 | font-size: 18px;
35 | }
--------------------------------------------------------------------------------
/module2/1_agenda.js:
--------------------------------------------------------------------------------
1 | // Module: PHP Looping, Functions, and Recursion
2 | // 1. Understanding Loops in PHP
3 | // Introduction to loops in PHP
4 | // Different types of loops in PHP:
5 | // For loop
6 | // While loop
7 | // Do-while loop
8 | // Foreach loop (brief introduction)
9 | // Nested loops: Concept and usage
10 |
11 | // 2. Advanced Loop Controls
12 | // Multiple stepping in For loops: Definition and implementation
13 | // The continue statement: Usage and examples
14 | // The break statement: Usage and examples
15 | // Hands-on: Printing the Fibonacci series using loops
16 |
17 | // 3. Functions in PHP
18 | // Introduction to functions in PHP
19 | // Declaring and executing functions: Syntax and examples
20 | // Parameters and arguments in functions:
21 | // Definition and distinction
22 | // Setting default parameter values
23 | // Function return types: Specification and enforcement
24 | // Type hinting in functions: Utilization and benefits
25 |
26 | // 4. Advanced Function Handling
27 | // Variadic functions: Accepting unlimited arguments in functions
28 | // Decomposing large functions:
29 | // Benefits of dividing a large function into smaller functions
30 | // Strategies for breaking down complex functions
31 | // Recursive functions:
32 | // Understanding recursion and its applications
33 | // Hands-on: Printing the Fibonacci series using recursive functions
34 |
35 | // 5. Variable Scope in PHP Functions
36 | // Local vs global variable scope
37 | // Understanding the global keyword
38 | // The static keyword: Usage and implications
39 |
40 | // 6. Project and Recap
41 | // Recap of the concepts learned
42 | // Project: Creating a comprehensive script that incorporates loops and functions
43 | // Q&A session and feedback gathering
44 | // Additional Resources
45 | // Reading materials
46 | // Online forums and communities for PHP developers
47 | // Recommended practices for coding in PHP
48 | // I would recommend incorporating practical examples, coding exercises, and perhaps a small project at the end of the module to help consolidate the learning. It will provide the learners with an opportunity to apply the knowledge they have acquired throughout the module. Make sure to include various examples and possibly create quizzes or assignments to evaluate understanding progressively.
--------------------------------------------------------------------------------
/module2/2_loop.js:
--------------------------------------------------------------------------------
1 | // Introduction to Loops in PHP
2 | // In PHP, a loop is a control structure used to execute a block of code repeatedly as long as a specified condition is true. Loops are essential in programming as they help you to execute a set of statements multiple times, which helps in reducing code redundancy.
3 |
4 | //✅ Different Types of Loops in PHP
5 | // For Loop
6 | // A for loop is used when you know in advance how many times you want to execute a statement or a block of statements.
7 |
8 | // Here is the syntax for the for loop:
9 |
10 |
11 | // for (initialization; condition; updater) {
12 | // // code to be executed;
13 | // }
14 | // Example:
15 |
16 |
17 | // for ($i = 0; $i < 5; $i++) {
18 | // echo $i . " ";
19 | // }
20 | // In the above example:
21 |
22 | // Initialization: $i = 0 (We start counting from 0)
23 | // Condition: $i < 5 (The loop will continue as long as $i is less than 5)
24 | // Updater: $i++ (After each loop, $i is incremented by 1)
25 | // While Loop
26 | // A while loop will execute a block of code as long as the specified condition is true.
27 |
28 | // Here is the syntax for the while loop:
29 |
30 |
31 | // ✅while (condition) {
32 | // // code to be executed;
33 | // }
34 | // Example:
35 |
36 |
37 | // $i = 0;
38 | // while ($i < 5) {
39 | // echo $i . " ";
40 | // $i++;
41 | // }
42 |
43 | //✅ Do-While Loop
44 | // A do-while loop is similar to a while loop, but it will execute the block of code at least once before checking the condition.
45 |
46 | // Here is the syntax for the do-while loop:
47 |
48 |
49 | // do {
50 | // // code to be executed;
51 | // } while (condition);
52 | // Example:
53 |
54 |
55 | // $i = 0;
56 | // do {
57 | // echo $i . " ";
58 | // $i++;
59 | // } while ($i < 5);
60 |
61 | // ✅Foreach Loop
62 | // The foreach loop works only on arrays and is used to loop through each key/value pair in an array.
63 |
64 | // Here is the syntax for the foreach loop:
65 |
66 |
67 | // foreach ($array as $value) {
68 | // // code to be executed;
69 | // }
70 | // Example:
71 |
72 |
73 | // $arr = array(1, 2, 3, 4, 5);
74 | // foreach ($arr as $value) {
75 | // echo $value . " ";
76 | // }
77 | // Nested Loops
78 | // Nested loops are loops within loops. It means you can use one loop inside another loop.
79 |
80 | // Here is the syntax for the nested loops:
81 |
82 |
83 | // for (initialization; condition; updater) {
84 | // for (initialization; condition; updater) {
85 | // // code to be executed;
86 | // }
87 | // }
88 | // Example:
89 |
90 |
91 | // for ($i = 0; $i < 3; $i++) {
92 | // for ($j = 0; $j < 3; $j++) {
93 | // echo "$i, $j ";
94 | // }
95 | // }
96 |
97 | // In this nested loop example, we have a for loop inside another for loop, creating a grid-like pattern of output values.
--------------------------------------------------------------------------------
/module2/3_forloop.js:
--------------------------------------------------------------------------------
1 | //👉 PHP for Loop Syntax
2 |
3 | // The general syntax for a for loop in PHP is as follows:
4 |
5 |
6 | //✅ for (initialize; condition; iteration) {
7 | // // code to be executed
8 | // }
9 | // Initialize: This is where you set your loop counter to its initial value.
10 | // Condition: This is a Boolean expression that the PHP engine checks before each iteration of the loop. If this expression evaluates to true, the loop continues; if it evaluates to false, the loop ends.
11 | // Iteration: This is where you define how your loop counter should be modified after each iteration of the loop.
12 | // PHP for Loop Examples
13 |
14 |
15 | //✅ Example 1: Simple for Loop
16 | // Here is a simple example of a for loop that prints numbers from 1 to 10:
17 |
18 |
19 | // for ($i = 1; $i <= 10; $i++) {
20 | // echo $i . " ";
21 | // }
22 |
23 |
24 | // In this loop:
25 |
26 | // We initialize the variable $i to 1.
27 | // We specify the condition $i <= 10, so the loop will continue as long as $i is less than or equal to 10.
28 | // We increase the value of $i by 1 each time the loop iterates, using the ++ operator (which is a shorthand for $i = $i + 1).
29 |
30 |
31 | // ✅Example 2: Looping Through an Array
32 | // Here we will loop through each element in an array and print it:
33 |
34 |
35 | // $array = array("apple", "banana", "cherry", "date", "elderberry");
36 |
37 | // for ($i = 0; $i < count($array); $i++) {
38 | // echo $array[$i] . " ";
39 | // }
40 |
41 |
42 | // In this loop:
43 |
44 | // We initialize the variable $i to 0 because arrays in PHP are zero-indexed, meaning that the first element in the array has an index of 0.
45 | // We use the count() function to get the number of elements in the array, and continue the loop as long as $i is less than the number of elements in the array.
46 | // We use the $i variable to access each element in the array using its index.
47 |
48 |
49 | // ✅Example 3: Skipping Iterations with continue
50 | // In this example, we'll use a for loop to print numbers from 1 to 10, but we'll skip even numbers using a continue statement:
51 |
52 |
53 | // for ($i = 1; $i <= 10; $i++) {
54 | // if ($i % 2 == 0) {
55 | // continue;
56 | // }
57 | // echo $i . " ";
58 | // }
59 |
60 |
61 | // In this loop:
62 |
63 | // We use an if statement and the modulus operator (%) to check if $i is even. If it is, we use the continue statement to skip the rest of the current iteration and move to the next iteration of the loop.
64 | // If $i is not even, we print its value.
65 | // I hope this provides a clear introduction to for loops in PHP! Try creating your own for loops to further understand how they work. Let me know if you have any questions.
66 |
67 |
68 |
69 |
70 |
71 |
72 | //✅ Example 1: Multiplication Table
73 | // Creating a multiplication table using nested for loops.
74 |
75 |
76 | // echo "
";
274 |
275 | // ✅Example 5: Outputting a Calendar
276 | // Creating a calendar where you loop through the days of the month to output a visual calendar.
277 |
278 |
279 | // $daysInMonth = date('t');
280 | // echo "